# 不谷尉
from tkinter import *
from tkinter import messagebox as msgbox
def luozi0():
global human, BOARD
B0['text'] = human
BOARD[0] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi1():
global human, BOARD
B1['text'] = human
BOARD[1] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi2():
global human, BOARD
B2['text'] = human
BOARD[2] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi3():
global human, BOARD
B3['text'] = human
BOARD[3] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi4():
global human, BOARD
B4['text'] = human
BOARD[4] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi5():
global human, BOARD
B5['text'] = human
BOARD[5] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi6():
global human, BOARD
B6['text'] = human
BOARD[6] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi7():
global human, BOARD
B7['text'] = human
BOARD[7] = human
Over = winner()
if not Over:
computer_move()
winner()
def luozi8():
global human, BOARD
B8['text'] = human
BOARD[8] = human
Over = winner()
if not Over:
computer_move()
winner()
def restart():
global BOARD, computer, human
computer = ' '
human = ' '
for i in range(9):
BOARD[i] = EMPTY
Bus[i]['text'] = EMPTY
winnerlabel['text'] = '未知'
if not askyesno():
computer_move()
def askyesno():
global human, computer
bl = msgbox.askyesno("YesNo", "是否选择先手?")
if bl:
human, computer = O, X
return True
else:
computer, human = O, X
return False
X = "X"
O = "O"
EMPTY = " "
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))
BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
BOARD = [] # 棋盘
for i in range(9):
BOARD.append(EMPTY)
root = Tk()
root.title('井字棋')
root.geometry('240x350')
Bus = []
computer = ' '
human = ' '
Label(root, text='赢家:', width=6, height=3).grid(row=4, column=1)
winnerlabel = Label(root, text='未知', width=6, height=3)
winnerlabel.grid(row=4, column=2)
winnerlabel.pack
reButton = Button(root, text='重新开始', width=6, height=2, bg='red', fg='blue', command=restart).grid(row=4, column=0)
B0 = Button(root, text=EMPTY, width=10, height=5, bg='green', fg='black', command=luozi0)
B1 = Button(root, text=EMPTY, width=10, height=5, bg='yellow', fg='black', command=luozi1)
B2 = Button(root, text=EMPTY, width=10, height=5, bg='green', fg='black', command=luozi2)
B3 = Button(root, text=EMPTY, width=10, height=5, bg='yellow', fg='black', command=luozi3)
B4 = Button(root, text=EMPTY, width=10, height=5, bg='green', fg='black', command=luozi4)
B5 = Button(root, text=EMPTY, width=10, height=5, bg='yellow', fg='black', command=luozi5)
B6 = Button(root, text=EMPTY, width=10, height=5, bg='green', fg='black', command=luozi6)
B7 = Button(root, text=EMPTY, width=10, height=5, bg='yellow', fg='black', command=luozi7)
B8 = Button(root, text=EMPTY, width=10, height=5, bg='green', fg='black', command=luozi8)
Bus.append(B0)
Bus.append(B1)
Bus.append(B2)
Bus.append(B3)
Bus.append(B4)
Bus.append(B5)
Bus.append(B6)
Bus.append(B7)
Bus.append(B8)
for Bi in range(0, 9):
Bus[Bi].grid(row=Bi//3, column=Bi % 3)
# 产生合法走棋位置序列,即未落子的位置序列
def legal_moves():
global BOARD
moves = []
for square in range(0, 9):
if BOARD[square] == EMPTY:
moves.append(square)
return moves
def winner():
global BOARD
for row in WAYS_TO_WIN:
if BOARD[row[0]] == BOARD[row[1]] == BOARD[row[2]] != EMPTY:
winner = BOARD[row[0]]
print("胜利者是" + winner)
if human == winner:
winnerlabel['text'] = '玩家'
elif computer == winner:
winnerlabel['text'] = '电脑'
return winner
if EMPTY not in BOARD:
print("平局")
winnerlabel['text'] = '平局'
return 'TIE'
def isFull():
for i in BOARD:
if i == EMPTY:
return False
return True
# 电脑下棋
def computer_move():
global computer, human
for move in legal_moves():
BOARD[move] = computer
Bus[move]['text'] = computer
if winner() == computer:
print("电脑下棋位置。。。", move)
return True
# 取消走棋方案
BOARD[move] = EMPTY
Bus[move]['text'] = EMPTY
for move in legal_moves():
BOARD[move] = human
Bus[move]['text'] = human
if winner() == human:
print("电脑下棋位置。。。", move)
BOARD[move] = computer
Bus[move]['text'] = computer
return True
# 取消走棋方案
BOARD[move] = EMPTY
Bus[move]['text'] = EMPTY
for move in BEST_MOVES:
if move in legal_moves():
BOARD[move] = computer
Bus[move]['text'] = computer
return True
if not askyesno():
computer_move()
root.mainloop()