-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
141 lines (126 loc) · 5.98 KB
/
Board.py
File metadata and controls
141 lines (126 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import tkinter as tk
class Board:
WIDTH = 300
HEIGHT = 300
def __init__(self, root, rows, cols) -> None:
self.root = root
self.rows = rows
self.cols = cols
self.canvas = tk.Canvas(root, width=self.WIDTH, height=self.HEIGHT, bg="gray")
self.canvas.pack(fill="both", expand=True)
self.canvas.bind("<Configure>", self.create_grid)
self.board = [[" " for _ in range(self.cols)] for _ in range(self.rows)]
self.available = [(row, col) for row in range(self.rows) for col in range(self.cols)]
self.root.resizable(False, False)
def create_grid(self, event=None):
self.canvas.delete("all")
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
cell_size = min(width // self.cols, height // self.rows)
for x in range(0, width, cell_size):
self.canvas.create_line(x, 0, x, height, fill="white")
for y in range(0, height, cell_size):
self.canvas.create_line(0, y, width, y, fill="white")
def draw_circle(self, row, col):
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
cell_size = min(width // self.cols, height // self.rows)
diameter = cell_size * 0.6
x = col * cell_size
y = row * cell_size
start_x = x + (cell_size - diameter) / 2
start_y = y + (cell_size - diameter) / 2
end_x = start_x + diameter
end_y = start_y + diameter
self.canvas.create_oval(start_x, start_y, end_x, end_y, outline="blue", width=5)
def draw_cross(self, row, col):
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
cell_size = min(width // self.cols, height // self.rows)
line_length = cell_size * 0.6
x = col * cell_size
y = row * cell_size
start_x = x + (cell_size - line_length) / 2
end_x = start_x + line_length
start_y = y + (cell_size - line_length) / 2
end_y = start_y + line_length
self.canvas.create_line(start_x, start_y, end_x, end_y, fill="red", width=5)
self.canvas.create_line(start_x, end_y, end_x, start_y, fill="red", width=5)
def draw(self, row, col, player):
if self.board[row][col] != " ":
self.canvas.create_text(self.WIDTH // 2, self.HEIGHT // 2, text="Invalid move", fill="yellow", font=("Arial", 24), tag="invalid")
self.canvas.update()
self.canvas.after(1000, self.canvas.delete, "invalid")
return
if (row, col) in self.available:
if player == "X":
self.draw_cross(row, col)
elif player == "O":
self.draw_circle(row, col)
self.board[row][col] = player
self.available.remove((row, col))
def get_cell(self, x, y):
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
cell_size = min(width // self.cols, height // self.rows)
col = x // cell_size
row = y // cell_size
return row, col
def evaluate(self):
for i in range(3):
if self.board[i][0] == self.board[i][1] == self.board[i][2] != " ":
return -1 if self.board[i][0] == "X" else 1
if self.board[0][i] == self.board[1][i] == self.board[2][i] != " ":
return -1 if self.board[0][i] == "X" else 1
if self.board[0][0] == self.board[1][1] == self.board[2][2] != " " or \
self.board[0][2] == self.board[1][1] == self.board[2][0] != " ":
return -1 if self.board[1][1] == "X" else 1
if all(cell != " " for row in self.board for cell in row):
return 0
return None
def is_full(self):
return len(self.available) == 0
def is_winner(self, state, player):
if isinstance(state, Board):
state = state.board
for i in range(3):
if state[i][0] == state[i][1] == state[i][2] == player.symbol:
return True
if state[0][i] == state[1][i] == state[2][i] == player.symbol:
return True
if state[0][0] == state[1][1] == state[2][2] == player.symbol or \
state[0][2] == state[1][1] == state[2][0] == player.symbol:
return True
return False
def print_board(self):
for row in self.board:
print(row)
def reset(self):
self.board = [[" " for _ in range(self.cols)] for _ in range(self.rows)]
self.available = [(row, col) for row in range(self.rows) for col in range(self.cols)]
self.canvas.delete("winner")
self.canvas.delete("tie")
self.create_grid()
def clear_board(self):
self.board = [[" " for _ in range(self.cols)] for _ in range(self.rows)]
self.available = [(row, col) for row in range(self.rows) for col in range(self.cols)]
self.canvas.delete("all")
self.create_grid()
def highlight_winner(self):
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
cell_size = min(width // self.cols, height // self.rows)
for row in range(self.rows):
if self.board[row][0] == self.board[row][1] == self.board[row][2] != " ":
y = row * cell_size + cell_size // 2
self.canvas.create_line(0, y, width, y, fill="green", width=5, tag="winner")
break
for col in range(self.cols):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != " ":
x = col * cell_size + cell_size // 2
self.canvas.create_line(x, 0, x, height, fill="green", width=5, tag="winner")
break
if self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
self.canvas.create_line(0, 0, width, height, fill="green", width=5, tag="winner")
if self.board[0][2] == self.board[1][1] == self.board[2][0] != " ":
self.canvas.create_line(0, height, width, 0, fill="green", width=5, tag="winner")