用户:jacksondzl查看:2 回复:1 评论:2 创建时间:2022-08-15T21:27:02
【作品展示】

【作品介绍】
这是一个人机下井字棋的游戏
【作品源代码】
import random
quit = 0
def draw_chess_pan(chess):
print('|'+chess[0]+'|'+chess[1]+'|'+chess[2]+'|')
print('_______')
print('|'+chess[3]+'|'+chess[4]+'|'+chess[5]+'|')
print('_______')
print('|'+chess[6]+'|'+chess[7]+'|'+chess[8]+'|')
print('_______')
print('开始游戏了、先看棋盘。')
chess = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
draw_chess_pan(chess)
def win(c):
return ((chess[0] == chess[1] and chess[1] == chess[2] and chess[2] == c) or
(chess[3] == chess[4] and chess[4] == chess[5] and chess[5] == c) or
(chess[6] == chess[7] and chess[7] == chess[8] and chess[8] == c) or
(chess[0] == chess[4] and chess[4] == chess[8] and chess[8] == c) or
(chess[2] == chess[4] and chess[4] == chess[6] and chess[6] == c) or
(chess[0] == chess[3] and chess[3] == chess[6] and chess[6] == c) or
(chess[1] == chess[4] and chess[4] == chess[7] and chess[7] == c) or
(chess[2] == chess[5] and chess[5] == chess[8] and chess[8] == c))
def input_chess_zi():
chess_zi = input('你要什么子?(输入x或o)')
if chess_zi != 'x' and chess_zi != 'o':
print('输入错误。')
input_chess_zi()
else:
return chess_zi
chess_zi = input_chess_zi()
def __x__():
data = random.randint(0,8)
if chess[data] == ' ':
chess[data] = 'X'
else:
__x__()
def __o__():
data = random.randint(0,8)
if chess[data] == ' ':
chess[data] = 'O'
else:
__o__()
def o():
data = int(input('在那个地方落子?'))
if chess[data] == ' ':
chess[data] = 'O'
else:
print('error')
o()
def x():
data = int(input('在那个地方落子?'))
if chess[data] == ' ':
chess[data] = 'X'
else:
print('error')
x()
if chess_zi == 'o':
while True:
o()
if win('O') == True:
print('you win')
break
if win('X') == True:
print('coputer win')
draw_chess_pan(chess)
break
quit = 0
for i in chess:
if i == ' ':
quit = 1
if quit:
__x__()
else:
print('平局')
break
draw_chess_pan(chess)
if win('O') == True:
print('you win')
break
if win('X') == True:
print('coputer win')
break
elif chess_zi == 'x':
while True:
__o__()
if win('X') == True:
print('you win')
break
if win('O') == True:
print('coputer win')
draw_chess_pan(chess)
break
draw_chess_pan(chess)
print('计算机已落子、请落子。')
quit = 0
for i in chess:
if i == ' ':
quit = 1
if quit:
x()
else:
print('平局')
break
draw_chess_pan(chess)
if win('O') == True:
print('coputer win')
break
if win('X') == True:
print('you win')
break
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn