from tkinter import *
from tkinter.messagebox import *
from threading import Thread as ths
import sys,time
buttons = [[None for i in range(18)] for j in range(18)]
strings = [[None for i in range(18)] for j in range(18)] #对应的字符串
command = [[lambda x=j, y=i: draw(x, y) for i in range(18)] for j in range(18)]
now_type = 0
last_opts = []
tot = -1
window = Tk()
window.resizable(False, False)
window.geometry("800x600+100+120")
window.title("五子棋——tkinter")
huiqi_state = StringVar(window, "")
white_win = 0
black_win = 0
state = StringVar(window, "")
def clear():
for i in range(15):
for j in range(15):
x = i * 30 + 40
y = j * 30 + 70
buttons[i][j].destroy()
buttons[i][j] = Button(window, bg = "orange", textvariable = strings[i][j], state = 'disabled')
buttons[i][j].place(x = x, y = y, width = 30, height = 30)
def check():
for i in range(15):
for j in range(15):
if i + 4 < 15:
if strings[i + 1][j].get() == strings[i + 2][j].get() == strings[i + 3][j].get() == strings[i + 4][j].get() == strings[i][j].get():
if strings[i][j].get() == '':
continue
clear()
if strings[i][j].get() == 'X':
showinfo("获胜提示", "黑子获胜")
else:
showinfo("获胜提示", "白子获胜")
for k in range(5):
x = (i + k) * 30 + 40
y = j * 30 + 70
buttons[i + k][j].destroy()
buttons[i + k][j] = Button(window, bg = "red", textvariable = strings[i + k][j], state = 'disabled')
buttons[i + k][j].place(x = x, y = y, width = 30, height = 30)
state.set("游戏结束")
return
if j + 4 < 15:
if strings[i][j + 1].get() == strings[i][j + 2].get() == strings[i][j + 3].get() == strings[i][j + 4].get() == strings[i][j].get():
if strings[i][j].get() == '':
continue
clear()
if strings[i][j].get() == 'X':
showinfo("获胜提示", "黑子获胜")
else:
showinfo("获胜提示", "白子获胜")
for k in range(5):
x = i * 30 + 40
y = (j + k) * 30 + 70
buttons[i][j + k].destroy()
buttons[i][j + k] = Button(window, bg = "red", textvariable = strings[i][j + k], state = 'disabled')
buttons[i][j + k].place(x = x, y = y, width = 30, height = 30)
state.set("游戏结束")
return
if j + 4 < 15 and i + 4 < 15:
if strings[i + 1][j + 1].get() == strings[i + 2][j + 2].get() == strings[i + 3][j + 3].get() == strings[i + 4][j + 4].get() == strings[i][j].get():
if strings[i][j].get() == '':
continue
clear()
if strings[i][j].get() == 'X':
showinfo("获胜提示", "黑子获胜")
else:
showinfo("获胜提示", "白子获胜")
for k in range(5):
x = (i + k) * 30 + 40
y = (j + k) * 30 + 70
buttons[i + k][j + k].destroy()
buttons[i + k][j + k] = Button(window, bg = "red", textvariable = strings[i + k][j + k], state = 'disabled')
buttons[i + k][j + k].place(x = x, y = y, width = 30, height = 30)
state.set("游戏结束")
return
if j + 4 < 15 and i - 4 >= 0:
if strings[i - 1][j + 1].get() == strings[i - 2][j + 2].get() == strings[i - 3][j + 3].get() == strings[i - 4][j + 4].get() == strings[i][j].get():
if strings[i][j].get() == '':
continue
clear()
if strings[i][j].get() == 'X':
showinfo("获胜提示", "黑子获胜")
else:
showinfo("获胜提示", "白子获胜")
for k in range(5):
x = (i - k) * 30 + 40
y = (j + k) * 30 + 70
buttons[i - k][j + k].destroy()
buttons[i - k][j + k] = Button(window, bg = "red", textvariable = strings[i - k][j + k], state = 'disabled')
buttons[i - k][j + k].place(x = x, y = y, width = 30, height = 30)
state.set("游戏结束")
return
def draw(x, y):
global now_type, window, buttons, strings, commands, tot
if now_type == 0:
strings[x][y].set("X")
now_type = 1
state.set("请白手落子")
else:
strings[x][y].set("O")
now_type = 0
state.set("请黑手落子")
buttons[x][y].destroy()
buttons[x][y] = Button(window, textvariable = strings[x][y], bg = "orange", fg = "white" if now_type == 0 else "black")
buttons[x][y].place(x = x * 30 + 40, y = y * 30 + 70, width = 30, height = 30)
tot += 1
last_opts.append((x, y))
huiqi_state.set(f"")
check()
def back():
if state.get() == "游戏结束":
return
global now_type, window, buttons, strings, commands, tot, huiqi_state
if tot == -1:
huiqi_state.set(f"无效悔棋。")
return
x = last_opts[tot][0]
y = last_opts[tot][1]
if now_type == 1:
strings[x][y] = StringVar(window, "")
now_type = 0
state.set("请黑手落子")
else:
state.set("请白手落子")
strings[x][y] = StringVar(window, "")
now_type = 1
buttons[x][y].destroy()
buttons[x][y] = Button(window, textvariable = strings[x][y], command = command[x][y], bg = "orange")
buttons[x][y].place(x = x * 30 + 40, y = y * 30 + 70, width = 30, height = 30)
huiqi_state.set(f"已撤销 {'白子' if now_type == 1 else '黑子'} \n 在 ({x}, {y}) 的操作。")
tot -= 1
last_opts.pop()
def init():
global tot, now_type, last_opts
tot = -1
now_type = 0
state.set("请黑手落子")
huiqi_state = StringVar(window, "")
last_opts = []
for i in range(15):
for j in range(15):
strings[i][j] = StringVar(window, "")
x = i * 30 + 40
y = j * 30 + 70
if buttons[i][j] != None:
buttons[i][j].destroy()
buttons[i][j] = Button(window, bg = "orange", textvariable = strings[i][j], command = command[i][j])
buttons[i][j].place(x = x, y = y, width = 30, height = 30)
def main():
global now_type, window, buttons, strings, commands
Label(window, text = "五子棋", font = ('等线', 20)).place(x = 350, y = 20)
Label(window, textvariable = huiqi_state, font = ('等线', 13)).place(x = 630, y = 500)
Button(window, text = "悔棋", command = back).place(x = 550, y = 500, width = 70, height = 40)
Button(window, text = "重置", command = init).place(x = 550, y = 400, width = 70, height = 40)
Label(window, textvariable = state, font = ('等线', 14)).place(x = 550, y = 300)
init()
Label(window, textvariable = huiqi_state, font = ('等线', 13)).place(x = 630, y = 500)
main()
mainloop()