以下是一个简单的Python井字游戏的源码实现:
# 定义棋盘大小
board = [' ' for _ in range(9)]
def print_board():
print()
print(board[0] + ' | ' + board[1] + ' | ' + board[2])
print('---------')
print(board[3] + ' | ' + board[4] + ' | ' + board[5])
print('---------')
print(board[6] + ' | ' + board[7] + ' | ' + board[8])
print()
def is_winner(player):
# 横向
if (board[0] == player and board[1] == player and board[2] == player) or \
(board[3] == player and board[4] == player and board[5] == player) or \
(board[6] == player and board[7] == player and board[8] == player) or \
# 纵向
(board[0] == player and board[3] == player and board[6] == player) or \
(board[1] == player and board[4] == player and board[7] == player) or \
(board[2] == player and board[5] == player and board[8] == player) or \
# 斜向
(board[0] == player and board[4] == player and board[8] == player) or \
(board[2] == player and board[4] == player and board[6] == player):
return True
else:
return False
def is_board_full():
if ' ' in board:
return False
else:
return True
def play_game():
current_player = 'X'
while True:
print_board()
position = int(input("轮到'" + current_player + "'玩家,请输入位置(1-9): "))
if board[position - 1] == ' ':
board[position - 1] = current_player
else:
print("该位置已经被占据,请重新输入。")
continue
if is_winner(current_player):
print_board()
print("玩家 '" + current_player + "' 赢了!")
break
elif is_board_full():
print_board()
print("平局!")
break
# 交换玩家
if current_player == 'X':
current_player = 'O'
else:
current_player = 'X'
play_game()
玩家通过输入位置来下棋,按照1-9的顺序依次对应棋盘上的位置。在每次下棋之后,程序会判断当前玩家是否获胜或者棋盘是否已满,如果是则游戏结束,否则交换玩家继续下棋。
没有合适的资源?快使用搜索试试~ 我知道了~
玩家通过输入位置来下棋,按照1-9的顺序依次对应棋盘上的位置。在每次下棋之后,程序会判断当前玩家是否获胜或者棋盘是否已满,如果是则游戏结束,否则交换玩家继续下棋。
资源推荐
资源详情
资源评论
收起资源包目录
python井字游戏源码.zip (1个子文件)
python井字游戏源码.py 2KB
共 1 条
- 1
资源评论
凛鼕将至
- 粉丝: 5159
- 资源: 302
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于MATLAB的钢板表面缺陷检测系统
- MS SQL里生成行政区域县区信息表和相应数据
- delphi实现DBGrid全选和反选功能
- 25C11F41-2B2A-4D1A-AAA8-7C654526B129.pdf
- Android Studio Jellyfish(android-studio-2023.3.1.18-cros.deb)
- MVC+EF框架+EasyUI实现权限管理源码程序
- python第66-75天,Day66-75.rar
- python后端服务project-of-tornado.rar
- python测验,hello-tornado.rar
- 基于SpringBoot+Vue3快速开发平台、自研工作流引擎源码设计.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功