用户:
尬聊boy查看:6 回复:6 评论:6 创建时间:2021-07-08T19:14:38
【作品展示】

【作品介绍】
大家好我是?e
这是一个井字棋的游戏,灵感来源于python乐园。
当你打开游戏,你将会和一个AI进行比拼,不过请你不要担心,你是决定不会赢的,看判断的代码已经可以知道了,这个AI智商已经被升级,并且判断了所有可以赢的方法,你可以打败它吗?我拭目以待。
可能几天后我会发一篇关于井字棋的教程,发井字棋的账号名称是wwwload,敬请期待吧!
【作品源代码】
import easygui
import random
print('井字棋游戏开始')
# 导包
# 开始准备
pieces = ['□', '□', '□', '□', '□', '□', '□', '□', '□'] # 棋子的布局
piece = 0 # 玩家所选的棋子
times = [0, 1, 2, 3, 4, 5, 6, 7, 8] # 可以下的地方的下标
time = 0 # 玩家下的次数
# 定义画出棋子的方法
def paint():
print(f'{pieces[0]} ▎{pieces[1]} ▎{pieces[2]}')
print(f'{pieces[3]} ▎{pieces[4]} ▎{pieces[5]}')
print(f'{pieces[6]} ▎{pieces[7]} ▎{pieces[8]}')
print()
paint()
# 定义玩家下棋的方法
def play():
global piece, time
piece = ''
piece = int(easygui.choicebox('请选择要下的地方', ' :: ', times))
pieces[piece] = '√'
time += 1
times.remove(piece)
paint()
# 定义机器人下棋的方法
def MachinePlay():
global piece
if time != 2:
piece = ''
piece = random.choice(times)
pieces[piece] = '×'
times.remove(piece)
paint()
else:
if pieces[0] == '√' and pieces[3] == '√':
piece = ''
piece = 6
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[3] == '√' and pieces[6] == '√':
piece = ''
piece = 0
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[1] == '√':
piece = ''
piece = 7
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[7] == '√':
piece = ''
piece = 1
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[2] == '√' and pieces[5] == '√':
piece = ''
piece = 8
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[5] == '√' and pieces[8] == '√':
piece = ''
piece = 2
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[0] == '√' and pieces[1] == '√':
piece = ''
piece = 2
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[1] == '√' and pieces[2] == '√':
piece = ''
piece = 0
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[3] == '√' and pieces[4] == '√':
piece = ''
piece = 5
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[5] == '√':
piece = ''
piece = 3
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[6] == '√' and pieces[7] == '√':
piece = ''
piece = 8
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[7] == '√' and pieces[8] == '√':
piece = ''
piece = 6
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[0] == '√' and pieces[4] == '√':
piece = ''
piece = 8
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[8] == '√':
piece = ''
piece = 0
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[2] == '√':
piece = ''
piece = 6
pieces[piece] = '×'
times.remove(piece)
paint()
elif pieces[4] == '√' and pieces[6] == '√':
piece = ''
piece = 2
pieces[piece] = '×'
times.remove(piece)
paint()
# 定义判断玩家是否胜利的方法
def If_win():
if pieces[0] == '√' and pieces[3] == '√' and pieces[6] == '√':
return True
elif pieces[1] == '√' and pieces[4] == '√' and pieces[7] == '√':
return True
elif pieces[2] == '√' and pieces[5] == '√' and pieces[8] == '√':
return True
elif pieces[0] == '√' and pieces[1] == '√' and pieces[2] == '√':
return True
elif pieces[3] == '√' and pieces[4] == '√' and pieces[5] == '√':
return True
elif pieces[6] == '√' and pieces[7] == '√' and pieces[8] == '√':
return True
elif pieces[0] == '√' and pieces[4] == '√' and pieces[8] == '√':
return True
elif pieces[4] == '√' and pieces[2] == '√' and pieces[6] == '√':
return True
# 开始游戏一共需要有三个回合
for i in range(3):
play()
MachinePlay()
# 判断玩家是否胜利,调用If_win方法
if time == 3:
if If_win():
print('玩家胜利')
print()
else:
print('玩家失败')
print()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn