没有合适的资源?快使用搜索试试~ 我知道了~
用python实现的一个井字棋游戏,供大家参考,具体内容如下 #Tic-Tac-Toe 井字棋游戏 #全局常量 X="X" O="O" EMPTY=" " #询问是否继续 def ask_yes_no(question): response=None; while response not in("y","n"): response=input(question).lower() return response #输入位置数字 def ask_number(question ,low,high): response=None while response not in rang
资源详情
资源评论
资源推荐
python实现简单井字棋小游戏实现简单井字棋小游戏
用python实现的一个井字棋游戏,供大家参考,具体内容如下
#Tic-Tac-Toe 井字棋游戏
#全局常量
X="X"
O="O"
EMPTY=" "
#询问是否继续
def ask_yes_no(question):
response=None;
while response not in("y","n"):
response=input(question).lower()
return response
#输入位置数字
def ask_number(question ,low,high):
response=None
while response not in range(low,high):
response=int(input(question))
return response
#询问谁先走,先走方为X,后走方为O
#函数返回计算机方,玩家的角色代号
def pieces():
go_first=ask_yes_no("玩家你是否先走(y/n):")
if go_first=="y":
print("玩家你先走.")
human=X
computer=O
else:
print("计算机先走.")
computer=X
human=O
return computer,human
#产生新的棋盘
def new_board():
board=[] for square in range(9):
board.append(EMPTY)
return board
#显示棋盘
def display_board(board):
board2=board[:] for i in range(len(board)):
if board[i]==EMPTY:
board2[i]=i
print(" ",board2[0],"|",board2[1],"|",board2[2])
print(" ","----------")
print(" ",board2[3],"|",board2[4],"|",board2[5])
print(" ", "----------")
print(" ",board2[6],"|",board2[7],"|",board2[8],"")
#产生可以合法走棋位置序列(也就是还未下过子位置)
def legal_moves(board):
moves=[] for square in range(9):
if board[square]==EMPTY:
moves.append(square)
return moves
#判断输赢
def winner(board):
# 所有赢得可能情况,例如(0,1,2)就是第一行,(0,4,8,),(2,4,6)就是对角线
WAYS_TO_WIN=((0,1,2,),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
for row in WAYS_TO_WIN:
if board[row[0]]==board[row[1]]==board[row[2]]!=EMPTY:
winner=board[row[0]] return winner
#棋盘没有空位置
if EMPTY not in board:
return "TIE"
return False
#人走棋
def human_move (board,human):
legal =legal_moves(board)
move =None
while move not in legal:
move=ask_number("你走那个位置?(0-8):",0,9)
if move not in legal:
print("此位置已经落过子了")
#print("Fine...")
return move
#计算机走棋
def computer_move(board,computer ,human):
weixin_38693528
- 粉丝: 2
- 资源: 913
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0