You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
8.0 KiB
216 lines
8.0 KiB
import tkinter as tk
|
|
from tkinter import messagebox
|
|
import pickle
|
|
|
|
window = tk.Tk()
|
|
window.title("井中井")
|
|
|
|
# Create board
|
|
def create_board():
|
|
for i in range(9):
|
|
for j in range(9):
|
|
if i<=2 and j<=2:
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="red", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (i<=2) and (2<j<=5):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="yellow", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (i<=2) and (5<j<=8):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="blue", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (2<i<=5) and (j<=2):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="yellowgreen", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (2<i<=5) and (2<j<=5):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="orange", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (2<i<=5) and (5<j<=8):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="purple", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (5<i<=8) and (j<=2):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="teal", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (5<i<=8) and (2<j<=5):
|
|
button =tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="cyan", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
elif (5<i<=8) and (5<j<=8):
|
|
button = tk.Button(window, text="", font=("Arial", 10), height=2, width=6, bg="navy", command=lambda row=i, col=j: handle_click(row, col))
|
|
button.grid(row=i, column=j, sticky="nsew")
|
|
|
|
|
|
create_board()
|
|
|
|
# Initialize variables
|
|
board = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
current_player = 1
|
|
# Handle button clicks
|
|
def handle_click(row, col):
|
|
global current_player
|
|
|
|
if board[row][col] == 0:
|
|
if current_player == 1:
|
|
board[row][col] = "X"
|
|
current_player = 2
|
|
else:
|
|
board[row][col] = "O"
|
|
current_player = 1
|
|
|
|
button = window.grid_slaves(row=row, column=col)[0]
|
|
button.config(text=board[row][col])
|
|
|
|
check_for_winner()
|
|
|
|
# Check for a winner or a tie
|
|
def check_for_winner():
|
|
winner = None
|
|
global a
|
|
a=0
|
|
global b
|
|
b=0
|
|
global c
|
|
c=None
|
|
with open('hzc.txt', 'r') as file:
|
|
a = int(file.read())
|
|
with open('hzc1.txt', 'r') as file:
|
|
b = int(file.read())
|
|
for row in range(len(board)):
|
|
if board[row][0] == board[row][1] == board[row][2] and board[row][0] != 0:
|
|
winner = board[row][0]
|
|
|
|
break
|
|
if board[row][3] == board[row][4] == board[row][5] and board[row][3] != 0:
|
|
winner = board[row][3]
|
|
|
|
break
|
|
if board[row][6] == board[row][7] == board[row][8] and board[row][6] != 0:
|
|
winner = board[row][6]
|
|
|
|
break
|
|
# Check columns
|
|
for col in range(len(board)):
|
|
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != 0:
|
|
winner = board[0][col]
|
|
|
|
break
|
|
if board[3][col] == board[4][col] == board[5][col] and board[3][col] != 0:
|
|
winner = board[3][col]
|
|
|
|
break
|
|
if board[6][col] == board[7][col] == board[8][col] and board[6][col] != 0:
|
|
winner = board[6][col]
|
|
|
|
break
|
|
|
|
# Check diagonals
|
|
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != 0:
|
|
winner = board[0][0]
|
|
|
|
elif board[0][2] == board[1][1] == board[2][0] and board[0][2] != 0:
|
|
winner = board[0][2]
|
|
|
|
if board[0][3] == board[1][4] == board[2][5] and board[0][3] != 0:
|
|
winner = board[0][3]
|
|
|
|
elif board[0][5] == board[1][4] == board[2][3] and board[0][5] != 0:
|
|
winner = board[0][5]
|
|
|
|
if board[0][6] == board[1][7] == board[2][8] and board[0][6] != 0:
|
|
winner = board[0][6]
|
|
|
|
elif board[0][8] == board[1][7] == board[2][6] and board[0][8] != 0:
|
|
winner = board[0][8]
|
|
|
|
if board[3][0] == board[4][1] == board[5][2] and board[3][0] != 0:
|
|
winner = board[3][0]
|
|
|
|
elif board[3][2] == board[4][1] == board[5][0] and board[3][2] != 0:
|
|
winner = board[3][2]
|
|
|
|
if board[3][3] == board[4][4] == board[5][5] and board[3][3] != 0:
|
|
winner = board[3][3]
|
|
|
|
elif board[3][5] == board[4][4] == board[5][3] and board[3][5] != 0:
|
|
winner = board[3][5]
|
|
|
|
if board[3][6] == board[4][7] == board[5][8] and board[3][6] != 0:
|
|
winner = board[3][6]
|
|
|
|
elif board[3][8] == board[4][7] == board[5][6] and board[3][8] != 0:
|
|
winner = board[3][8]
|
|
|
|
if board[6][0] == board[7][1] == board[8][2] and board[6][0] != 0:
|
|
winner = board[6][0]
|
|
|
|
elif board[6][2] == board[7][1] == board[8][0] and board[6][2] != 0:
|
|
winner = board[6][2]
|
|
|
|
if board[6][3] == board[7][4] == board[8][5] and board[6][3] != 0:
|
|
winner = board[6][3]
|
|
|
|
elif board[6][5] == board[7][4] == board[8][3] and board[6][5] != 0:
|
|
winner = board[6][5]
|
|
|
|
if board[6][6] == board[7][7] == board[8][8] and board[6][6] != 0:
|
|
winner = board[6][6]
|
|
|
|
elif board[6][8] == board[7][7] == board[8][6] and board[6][8] != 0:
|
|
winner = board[6][8]
|
|
if winner=='X':
|
|
a+=1
|
|
c=a
|
|
elif winner=="O":
|
|
b+=1
|
|
c=b
|
|
|
|
|
|
if all([all(row) for row in board]) and winner is None:
|
|
winner = "tie"
|
|
if winner:
|
|
|
|
declare_winner(winner)
|
|
|
|
# Declare the winner and ask to restart the game
|
|
def declare_winner(winner):
|
|
if winner == "tie":
|
|
message = "It's a tie!"
|
|
else:
|
|
message = f"Player {winner} wins{c}!"
|
|
|
|
|
|
answer = messagebox.askyesno("Game Over", message + "Do you want to continue the next game?")
|
|
if answer:
|
|
with open('hzc.txt', 'w') as file:
|
|
file.write(str(a))
|
|
with open('hzc1.txt', 'w') as file:
|
|
file.write(str(b))
|
|
|
|
global board
|
|
board = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
|
|
|
for i in range(9):
|
|
for j in range(9):
|
|
button = window.grid_slaves(row=i, column=j)[0]
|
|
button.config(text="")
|
|
|
|
global current_player
|
|
current_player = 1
|
|
else:
|
|
window.quit()
|
|
|
|
window.mainloop() |