from tkinter import *
import random
import time
tk = Tk()
tk.title('五子棋游戏')
tk.geometry("400x450")
tk.config(bg='black')
stateList = [[0 for i in range(8)] for j in range(8)]
canvas = Canvas(tk, width=400, height=400, bg='#FFEFD5')
canvas.pack(side=BOTTOM)
stateLabel = Label(tk, text='轮到您...', font=('consoles', 20), fg='white', bg='black')
stateLabel.place(x=150, y=10)
for i in range(8):
canvas.create_line(50, 50 + i * 42.7, 350, 50 + i * 42.7, fill='black')
canvas.create_line(50 + i * 42.7, 50, 50 + i * 42.7, 350, fill='black')
def cauculate_position(x,y,fill):
x1 = 50 + x * 42.7 + 1 - 20 / 2
y1 = 50 + y * 42.7 + 1 - 20 / 2
x2 = 50 + x * 42.7 + 1 + 20 / 2
y2 = 50 + y * 42.7 + 1 + 20 / 2
if fill == 1:
fill = 'black'
else:
fill = 'white'
canvas.create_oval(x1, y1, x2, y2, fill=fill)
stateLabel['text'] = '轮到您...'
canvas.bind("<Button-1>", on_canvas_click)
return 0
def win(player):
d={1:2,2:1}
for i in range(8):
for j in range(6):
if stateList[i][j] == player and stateList[i][j+1] == player and stateList[i][j+2] == player:
try:
a=stateList[i][j+4]
except:
return False
try:
a=stateList[i][j-1]
except:
return False
if stateList[i][j+4] == d[player]:
cauculate_position(i,j,2)
elif stateList[i][j-1] == d[player]:
cauculate_position(i,j,2)
return True
for j in range(8):
for i in range(6):
if stateList[i][j] == player and stateList[i+1][j] == player and stateList[i+2][j] == player:
try:
a=stateList[i+3][j]
except:
return False
try:
a=stateList[i-1][j]
except:
return False
if stateList[i+3][j] == d[player]:
cauculate_position(i,j,2)
elif stateList[i-1][j] == d[player]:
cauculate_position(i,j,2)
return True
for i in range(6):
for j in range(6):
if stateList[i][j] == player and stateList[i+1][j+1] == player and stateList[i+2][j+2] == player:
try:
a=stateList[i+3][j+3]
except:
return False
try:
a=stateList[i-1][j-1]
except:
return False
if stateList[i+3][j+3] == d[player]:
cauculate_position(i,j,2)
elif stateList[i-1][j-1] == d[player]:
cauculate_position(i,j,2)
return True
for i in range(7, 1, -1):
for j in range(6):
if stateList[i][j] == player and stateList[i-1][j+1] == player and stateList[i-2][j+2] == player:
try:
a=stateList[i-3][j+3]
except:
return False
try:
a=stateList[i+1][j-1]
except:
return False
if stateList[i-3][j+3] == d[player]:
cauculate_position(i,j,2)
elif stateList[i+1][j-1] == d[player]:
cauculate_position(i,j,2)
return True
return False
def check_win(player):
for i in range(8):
for j in range(5):
if stateList[i][j] == player and stateList[i][j+1] == player and stateList[i][j+2] == player and stateList[i][j+3] == player and stateList[i][j+4] == player:
return True
for i in range(5):
for j in range(8):
if stateList[i][j] == player and stateList[i+1][j] == player and stateList[i+2][j] == player and stateList[i+3][j] == player and stateList[i+4][j] == player:
return True
for i in range(5):
for j in range(5):
if stateList[i][j] == player and stateList[i+1][j+1] == player and stateList[i+2][j+2] == player and stateList[i+3][j+3] == player and stateList[i+4][j+4] == player:
return True
for i in range(5):
for j in range(3, 8):
if stateList[i][j] == player and stateList[i+1][j-1] == player and stateList[i+2][j-2] == player and stateList[i+3][j-3] == player and stateList[i+4][j-4] == player:
return True
cnt = 0
for i in stateList:
for j in i:
if j != 0:
cnt += 1
if cnt == 64:
stateLabel['text'] = '平局!'
canvas.unbind("<Button-1>")
return False
def on_canvas_click(event):
global stateList,x_index,y_index
x_index = int((event.x - 50) / 42.7)
y_index = int((event.y - 50) / 42.7)
if stateList[y_index][x_index]!= 0:
return
cauculate_position(x_index, y_index, 1)
stateLabel['text'] = '轮到电脑...'
canvas.update()
stateList[y_index][x_index] = 1
canvas.unbind("<Button-1>")
computer_move()
if check_win(1):
stateLabel['text'] = '您赢了!'
canvas.unbind("<Button-1>")
tk.update()
def computer_move():
global stateList
while True:
if not win(2):
angle = random.choice([[1, 0], [0, 1], [-1, 0], [0, -1]])
try:
if stateList[y_index + angle[0]][x_index + angle[1]] == 0:
stateList[y_index + angle[0]][x_index + angle[1]] = 2
cauculate_position(y_index + angle[0], x_index + angle[1], 2)
break
except :
continue
canvas.bind("<Button-1>", on_canvas_click)
tk.mainloop()
紧急!
windows 11 22631.4169
pycharm社区版2023.3.7
问题的视频在