-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasses.py
313 lines (273 loc) · 10.4 KB
/
Classes.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
import pygame as pg
pg.init()
COLOR_INACTIVE = pg.Color(50,50,50)
COLOR_ACTIVE = pg.Color(0,0,0)
FONT = pg.font.Font('freesansbold.ttf', 32)
class Board:
'''
handles all board operations
'''
def __init__(self):
print('initialized')
self.board = self.build_grid()
def build_grid(something):
'''
Creates a 2 dimensional array.
'''
grid = []
for row in range(8):
# Add an empty array that will hold each cell in this row
grid.append([])
for column in range(8):
grid[row].append(0) # Append a cell
grid[3][3] = 1
grid[3][4] = 2
grid[4][3] = 2
grid[4][4] = 1
return grid
def show_valid(self, player):
'''
Displays valid moves
'''
for row,line in enumerate(self.board):
for column,cell in enumerate(line):
if cell == 3:
self.board[row][column] = 0
for row,level in enumerate(self.board):
for column,cell in enumerate(level):
if cell == player:
self.check_avaliable(player,row,column,'left','down')
self.check_avaliable(player,row,column,'left','up')
self.check_avaliable(player,row,column,'right','down')
self.check_avaliable(player,row,column,'right','up')
self.check_avaliable(player,row,column,None,'down')
self.check_avaliable(player,row,column,None,'up')
self.check_avaliable(player,row,column,'left')
self.check_avaliable(player,row,column,'right')
# for row in self.board:
# print(row)
def check_avaliable(self,player,row,column,hor=None,vert=None):
'''
allows to check every direction in grid and set
'''
check = 5
count = 0
while check != player or 0 or 3:
if hor == 'left' and vert == 'down':
row += 1
column -= 1
if hor == 'left' and vert == 'up':
row -= 1
column -= 1
if hor == 'right' and vert == 'down':
row += 1
column += 1
if hor == 'right' and vert == 'up':
row -= 1
column += 1
if hor == 'left' and vert == None:
column -= 1
if hor == 'right' and vert == None:
column += 1
if hor == None and vert == 'down':
row += 1
if hor == None and vert == 'up':
row -= 1
if 0 > row or row > 7 or 0 > column or column > 7:
break
print('check: ',row,column)
check = self.board[row][column]
if check != player and check != 0 and check != 3:
count += 1
if check == 3:
break
if count > 0 and check == 0:
print('count:',count)
print('possible pts',row,column,'player',player)
self.board[row][column] = 3
break
if count == 0 and check == 0:
break
def check_valid(self,player,row,column):
'''
checks click if valid move
'''
if 0 > row or row > 7 or 0 > column or column > 7:
return False
if self.board[row][column] == 3:
if player == 1:
self.board[row][column] = 1
elif player == 2:
self.board[row][column] = 2
return True
else:
print('not valid')
return False
def tile_flip(self,player,row,column):
'''
flips the required tiles after a move
'''
self.set_tile_flip(player,row,column,'left','down')
self.set_tile_flip(player,row,column,'left','up')
self.set_tile_flip(player,row,column,'right','down')
self.set_tile_flip(player,row,column,'right','up')
self.set_tile_flip(player,row,column,None,'down')
self.set_tile_flip(player,row,column,None,'up')
self.set_tile_flip(player,row,column,'left')
self.set_tile_flip(player,row,column,'right')
def set_tile_flip(self,player,row,column,hor=None,vert=None):
'''
checks every direction if tile flip is required
'''
check = 5
points = []
while check != player or 0 or 3:
if hor == 'left' and vert == 'down':
row += 1
column -= 1
if hor == 'left' and vert == 'up':
row -= 1
column -= 1
if hor == 'right' and vert == 'down':
row += 1
column += 1
if hor == 'right' and vert == 'up':
row -= 1
column += 1
if hor == 'left' and vert == None:
column -= 1
if hor == 'right' and vert == None:
column += 1
if hor == None and vert == 'down':
row += 1
if hor == None and vert == 'up':
row -= 1
if 0 > row or row > 7 or 0 > column or column > 7:
# print('index break')
break
# print(row,column)
check = self.board[row][column]
# print('check',check)
# print('player',player)
if check == player or check == 0 or check == 3:
points.append((row,column))
# print('check broke')
break
else:
points.append((row,column))
if points:
# print('thingy',points)
# print('last point',points[-1])
# print('board tile',self.board[points[-1][0]][points[-1][1]])
if self.board[points[-1][0]][points[-1][1]] == player:
for point in points:
# print(point)
row = point[0]
column = point[1]
# print(row,column)
# print('tile color', player)
self.board[row][column] = player
def update_score(self):
'''
updates scoreboard
'''
white = 0
black = 0
for row in self.board:
for cell in row:
if cell == 1:
black += 1
elif cell == 2:
white += 1
return black, white
def check_win(self):
'''
checks for Game Over
'''
count = 0
white = 0
black = 0
for row in self.board:
for cell in row:
if cell == 0 or 3:
count += 1
if cell == 1:
black += 1
elif cell == 2:
white == 1
if count > 0:
return False
else:
if white < black:
return 1
else:
return 2
class InputBox:
'''
Handles drawing input boxes
'''
def __init__(self, x, y, w, h, text=''):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.active = False
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pg.KEYDOWN:
# print(self.text)
if self.active:
if event.key == pg.K_RETURN:
text = self.text
self.text = ''
self.txt_surface = FONT.render(self.text, True, self.color)
return (text)
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, self.color)
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 3)
class Button:
'''
Handles drawing buttons
'''
def __init__(self, x, y, w, h, s, text='BUTTON'):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = (pg.font.Font('freesansbold.ttf', s)).render(text, True, (255,255,255))
self.active = False
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the button rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
return self.active
def draw(self, screen):
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 0)
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+7))