-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
334 lines (277 loc) · 11.1 KB
/
board.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import pygame
from const import *
from board_dim import BoardDim
class Board:
def __init__(self, dims=None, linewidth=15, ultimate=False, max=False):
self.squares = [[0, 0, 0] for row in range(DIM)]
self.dims = dims
if not dims:
self.dims = BoardDim(WIDTH, 0, 0)
self.linewidth = linewidth
self.offset = self.dims.sqsize * 0.2
self.radius = (self.dims.sqsize // 2) * 0.7
self.max = max
if ultimate:
self.create_ultimate()
self.active = True
def __str__(self):
s = ""
for row in range(DIM):
for col in range(DIM):
square = self.squares[row][col]
s += str(square)
return s
def create_ultimate(self):
for row in range(DIM):
for col in range(DIM):
size = self.dims.sqsize
xcor, ycor = self.dims.xcor + (
col * self.dims.sqsize
), self.dims.ycor + (row * self.dims.sqsize)
dims = BoardDim(size=size, xcor=xcor, ycor=ycor)
linewidth = self.linewidth - 7
ultimate = self.max
self.squares[row][col] = Board(
dims=dims, linewidth=linewidth, ultimate=ultimate, max=False
)
def render(self, surface):
for row in range(DIM):
for col in range(DIM):
square = self.squares[row][col]
if isinstance(square, Board):
square.render(surface)
# Vertical lines
pygame.draw.line(
surface,
LINE_COLOR,
(self.dims.xcor + self.dims.sqsize, self.dims.ycor),
(self.dims.xcor + self.dims.sqsize, self.dims.ycor + self.dims.size),
self.linewidth,
)
pygame.draw.line(
surface,
LINE_COLOR,
(self.dims.xcor + self.dims.size - self.dims.sqsize, self.dims.ycor),
(
self.dims.xcor + self.dims.size - self.dims.sqsize,
self.dims.ycor + self.dims.size,
),
self.linewidth,
)
# Horizontal lines
pygame.draw.line(
surface,
LINE_COLOR,
(self.dims.xcor, self.dims.ycor + self.dims.sqsize),
(self.dims.xcor + self.dims.size, self.dims.ycor + self.dims.sqsize),
self.linewidth,
)
pygame.draw.line(
surface,
LINE_COLOR,
(self.dims.xcor, self.dims.ycor + self.dims.size - self.dims.sqsize),
(
self.dims.xcor + self.dims.size,
self.dims.ycor + self.dims.size - self.dims.sqsize,
),
self.linewidth,
)
def valid_square(self, xclick, yclick):
row = yclick // self.dims.sqsize
col = xclick // self.dims.sqsize
if row > 2:
row %= DIM
if col > 2:
col %= DIM
square = self.squares[row][col]
# Base case
if not isinstance(square, Board):
return square == 0 and self.active
# Recursive step
return square.valid_square(xclick, yclick)
def mark_square(self, xclick, yclick, player):
row = yclick // self.dims.sqsize
col = xclick // self.dims.sqsize
if row > 2:
row %= DIM
if col > 2:
col %= DIM
square = self.squares[row][col]
print("marking -> (", row, col, ")")
# Base case
if not isinstance(square, Board):
self.squares[row][col] = player
return
# Recursive step
square.mark_square(xclick, yclick, player)
def draw_fig(self, surface, xclick, yclick):
row = yclick // self.dims.sqsize
col = xclick // self.dims.sqsize
if row > 2:
row %= DIM
if col > 2:
col %= DIM
square = self.squares[row][col]
# Base case
if not isinstance(square, Board):
# Cross
if square == 1:
# Descending line
ipos = (
self.dims.xcor + (col * self.dims.sqsize) + self.offset,
self.dims.ycor + (row * self.dims.sqsize) + self.offset,
)
fpos = (
self.dims.xcor + self.dims.sqsize * (1 + col) - self.offset,
self.dims.ycor + self.dims.sqsize * (1 + row) - self.offset,
)
pygame.draw.line(surface, CROSS_COLOR, ipos, fpos, self.linewidth)
# Ascending line
ipos = (
self.dims.xcor + (col * self.dims.sqsize) + self.offset,
self.dims.ycor + self.dims.sqsize * (1 + row) - self.offset,
)
fpos = (
self.dims.xcor + self.dims.sqsize * (1 + col) - self.offset,
self.dims.ycor + (row * self.dims.sqsize) + self.offset,
)
pygame.draw.line(surface, CROSS_COLOR, ipos, fpos, self.linewidth)
# Circle
elif square == 2:
center = (
self.dims.xcor + self.dims.sqsize * (0.5 + col),
self.dims.ycor + self.dims.sqsize * (0.5 + row),
)
pygame.draw.circle(
surface, CIRCLE_COLOR, center, self.radius, self.linewidth
)
return
# Recursive step
square.draw_fig(surface, xclick, yclick)
def manage_win(self, surface, winner, onmain=False):
# Transparent screen
transparent = pygame.Surface((self.dims.size, self.dims.size))
transparent.set_alpha(ALPHA)
transparent.fill(FADE)
if onmain:
surface.blit(transparent, (self.dims.xcor, self.dims.ycor))
surface.blit(transparent, (self.dims.xcor, self.dims.ycor))
surface.blit(transparent, (self.dims.xcor, self.dims.ycor))
# Draw win
if not onmain:
# Cross
if winner == 1:
# Descending line
ipos = (self.dims.xcor + self.offset, self.dims.ycor + self.offset)
fpos = (
self.dims.xcor + self.dims.size - self.offset,
self.dims.ycor + self.dims.size - self.offset,
)
pygame.draw.line(surface, CROSS_COLOR, ipos, fpos, self.linewidth + 7)
# Ascending line
ipos = (
self.dims.xcor + self.offset,
self.dims.ycor + self.dims.size - self.offset,
)
fpos = (
self.dims.xcor + self.dims.size - self.offset,
self.dims.ycor + self.offset,
)
pygame.draw.line(surface, CROSS_COLOR, ipos, fpos, self.linewidth + 7)
# Circle
if winner == 2:
center = (
self.dims.xcor + self.dims.size * 0.5,
self.dims.ycor + self.dims.size * 0.5,
)
pygame.draw.circle(
surface,
CIRCLE_COLOR,
center,
self.dims.size * 0.4,
self.linewidth + 7,
)
# Inactive board
self.active = False
def check_draw_win(
self,
surface,
):
isfull = True
for row in range(DIM):
for col in range(DIM):
# base case square should have numbers
square = self.squares[row][col]
if isinstance(square, Board) and square.active:
# other board win
winner = square.check_draw_win(surface)
if winner: # recursive step
self.squares[row][col] = winner
square.manage_win(surface, winner)
# main
# vertical wins
for c in range(DIM):
if (
self.squares[0][c]
== self.squares[1][c]
== self.squares[2][c]
!= 0
):
color = CROSS_COLOR if self.squares[0][c] == 1 else CIRCLE_COLOR
# draw win
ipos = (
self.dims.xcor + self.dims.sqsize * (0.5 + c),
self.dims.ycor + self.offset,
)
fpos = (
self.dims.xcor + self.dims.sqsize * (0.5 + c),
self.dims.ycor + self.dims.size - self.offset,
)
pygame.draw.line(surface, color, ipos, fpos, self.linewidth)
return self.squares[0][c]
# horizontal wins
for r in range(DIM):
if (
self.squares[r][0]
== self.squares[r][1]
== self.squares[r][2]
!= 0
):
color = CROSS_COLOR if self.squares[r][0] == 1 else CIRCLE_COLOR
# draw win
ipos = (
self.dims.xcor + self.offset,
self.dims.ycor + self.dims.sqsize * (r + 0.5),
)
fpos = (
self.dims.xcor + self.dims.size - self.offset,
self.dims.ycor + self.dims.sqsize * (r + 0.5),
)
pygame.draw.line(surface, color, ipos, fpos, self.linewidth)
return self.squares[r][0]
# diagonal wins
# desc
if self.squares[0][0] == self.squares[1][1] == self.squares[2][2] != 0:
color = CROSS_COLOR if self.squares[1][1] == 1 else CIRCLE_COLOR
# draw win
ipos = (self.dims.xcor + self.offset, self.dims.ycor + self.offset)
fpos = (
self.dims.xcor + self.dims.size - self.offset,
self.dims.ycor + self.dims.size - self.offset,
)
pygame.draw.line(surface, color, ipos, fpos, self.linewidth)
return self.squares[1][1]
# asc
if self.squares[2][0] == self.squares[1][1] == self.squares[0][2] != 0:
color = CROSS_COLOR if self.squares[1][1] == 1 else CIRCLE_COLOR
# draw win
ipos = (
self.dims.xcor + self.offset,
self.dims.ycor + self.dims.size - self.offset,
)
fpos = (
self.dims.xcor + self.dims.size - self.offset,
self.dims.ycor + self.offset,
)
pygame.draw.line(surface, color, ipos, fpos, self.linewidth)
return self.squares[1][1]