import turtle
import random
import my_ui
window=turtle.Screen()
window.bgpic("棋盘背景.png")
window.title("智能棋手")
# 决定先后手
coin = random.randint(0, 1)
chessList = list(range(9))
# 回合数
number = 0
while True:
# 依次下棋
number += 1
if coin == 0:
while True:
user = turtle.textinput("输入框", "玩家出子:")
if int(user) == chessList[int(user)]:
chessList[int(user)] = "x"
coin = 1
break
else:
print("电脑出子:")
while True:
index = random.randint(0, 8)
print(index)
if index == chessList[index]:
chessList[index] = "o"
coin = 0
break
# 显示棋盘效果
my_ui.show(chessList)
# 判断胜负
win1 = chessList[0] == chessList[1] == chessList[2]
win2 = chessList[3] == chessList[4] == chessList[5]
win3 = chessList[6] == chessList[7] == chessList[8]
win4 = chessList[0] == chessList[3] == chessList[6]
win5 = chessList[1] == chessList[4] == chessList[7]
win6 = chessList[2] == chessList[5] == chessList[8]
win7 = chessList[0] == chessList[4] == chessList[8]
win8 = chessList[2] == chessList[4] == chessList[6]
if win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8:
if coin == 1:
print("玩家获胜!")
else:
print("电脑获胜!")
break
if number == 9:
print("和棋")
break
turtle.done()