-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
340 lines (272 loc) · 11.4 KB
/
simulation.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
335
336
337
338
339
340
import copy
import sys
import pygame
import Screen
import time
from holes import create_holes
# from game import screen
pygame.font.init()
Screen.initialize()
font = pygame.font.SysFont("comicsans", 40)
font2 = pygame.font.SysFont("comicsans", 72)
PIT_WIDTH = 100
PADDING = 20
class board:
def __init__(self, board_size, pit_count):
self.board_list = [pit_count] * board_size
self.board_size = board_size
self.pit_count = pit_count
def isHalfEmpty(self):
'''
checks if one size of the board is empty or not
useful in determining if the game is over or not
'''
# check if any one side of the board leaves the player with no option to choose from
return self.generateOptions(0) == [] or self.generateOptions(1) == []
def clone(self):
'''
clones the original board
'''
new = board(len(self.board_list), self.pit_count)
new.board_list = copy.copy(self.board_list)
return new
def generateOptions(self, current_player):
'''
returns a list of indices the current player is eligable to choose from
the half the board returned represnts the side of the current player
'''
selection = []
side = []
size = int(len(self.board_list) / 2)
if current_player == 0:
for index in range(0, size):
if self.board_list[index] != 0:
selection.append(index)
else:
for index in range(size, size * 2):
if self.board_list[index] != 0:
selection.append(index)
return selection
def nextPit(self, pit):
return (pit + 1) % self.board_size
def move(self, start):
'''
the game is played beginning from the start index
the move ends when there the next pit is empty
the move function returns a score
'''
# scores, get coins to move around and set the start pit to zero coins
score = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
isGame = False
coins = self.board_list[start]
# Screen.screen.fill((0, 0, 0))
bg = pygame.image.load("bg.jpg").convert()
Screen.screen.blit(bg, (0, 0))
create_holes(Screen.screen)
size = int(self.board_size / 2)
# displaying board 0
new_list = self.board_list[::-1]
temp_list = new_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 180))
# displaying board 1
temp_list = self.board_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 320))
text = font.render(
str(Screen.player_score[0]), 1, (0, 0, 0))
Screen.screen.blit(text, (7*PIT_WIDTH + 8*PADDING + 40, 250))
text = font.render(str(Screen.player_score[1]), 1,(0, 0, 0))
Screen.screen.blit(text, (PADDING + 40, 250))
# Starting pit color changed to golden
text = font.render(str(self.board_list[start]), 1, (218, 165, 32))
size = self.board_size
if start < size // 2:
x = ( start + 1 )*PIT_WIDTH + ( start + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
else:
x = ( size - start )*PIT_WIDTH + ( size - start + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
Screen.screen.blit(text, (x, y))
pygame.display.update()
self.board_list[start] = 0
time.sleep(0.5)
text = font.render(str(self.board_list[start]), 1, (218, 165, 32))
size = self.board_size
if start < size // 2:
x = ( start + 1 )*PIT_WIDTH + ( start + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
color = (160, 160, 160)
else:
x = ( size - start )*PIT_WIDTH + ( size - start + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
color = (0, 153, 153)
Screen.screen.blit(text, (x, y))
pygame.display.update()
# move the coins to corresponding pits
for coin in range(coins):
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
isGame = False
start = self.nextPit(start)
self.board_list[start] = self.board_list[start] + 1
time.sleep(0.35)
create_holes(Screen.screen)
size = int(self.board_size / 2)
# displaying board 0
new_list = self.board_list[::-1]
temp_list = new_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 180))
# displaying board 1
temp_list = self.board_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 320))
text = font.render(
str(Screen.player_score[0]), 1, (0, 0, 0))
Screen.screen.blit(text, (7*PIT_WIDTH + 8*PADDING + 40, 250))
text = font.render(str(Screen.player_score[1]), 1,(0, 0, 0))
Screen.screen.blit(text, (PADDING + 40, 250))
text = font.render(str(self.board_list[start]), 1, (255, 255, 255))
size = self.board_size
if start < size // 2:
x = ( start + 1 )*PIT_WIDTH + ( start + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
else:
x = ( size - start )*PIT_WIDTH + ( size - start + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
Screen.screen.blit(text, (x, y))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
isGame = False
# next_pit = self.nextPit(start)
next_pit = start
time.sleep(0.5)
if self.board_list[next_pit] == 1:
text = font.render("",1,(93, 121, 125))
Screen.screen.blit(text, (PADDING, 500))
# (12 + 6)%12
# score_pit = self.nextPit(next_pit)
score_pit = (self.board_size - 1) - next_pit
text = font.render(str(self.board_list[score_pit]),1,(0,255,0))
size = self.board_size
if score_pit < size // 2:
x = ( score_pit + 1 )*PIT_WIDTH + ( score_pit + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
else:
x = ( size - score_pit )*PIT_WIDTH + ( size - score_pit + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
Screen.screen.blit(text,(x,y))
text = font.render(str(self.board_list[next_pit]),1,(255,0,0))
size = self.board_size
if next_pit < size // 2:
x = ( next_pit + 1 )*PIT_WIDTH + ( next_pit + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
else:
x = ( size - next_pit )*PIT_WIDTH + ( size - next_pit + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
Screen.screen.blit(text,(x,y))
pygame.display.update()
time.sleep(3)
# score_pit = self.nextPit(next_pit)
score = self.board_list[score_pit]
self.board_list[score_pit] = 0
create_holes(Screen.screen)
size = int(self.board_size / 2)
# displaying board 0
new_list = self.board_list[::-1]
temp_list = new_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 180))
# displaying board 1
temp_list = self.board_list[:size]
for i in range(size):
text = font.render(str(temp_list[i]), 1, (0, 0, 0))
Screen.screen.blit(text, (( i + 1 )*PIT_WIDTH + ( i + 2 )*PADDING + 40, 320))
text = font.render(
str(Screen.player_score[0]), 1, (0, 0, 0))
Screen.screen.blit(text, (7*PIT_WIDTH + 8*PADDING + 40, 250))
text = font.render(str(Screen.player_score[1]), 1,(0, 0, 0))
Screen.screen.blit(text, (PADDING + 40, 250))
text = font.render(str(self.board_list[score_pit]), 1, (255, 0, 0))
size = self.board_size
if start < size // 2:
x = ( start + 1 )*PIT_WIDTH + ( start + 2 )*PADDING + 40
# x = 80 * start + 42
y = 320
else:
x = ( size - start )*PIT_WIDTH + ( size - start + 1 )*PADDING + 40
# x = 80 * (size - start - 1) + 42
y = 180
# if score_pit < size // 2:
# x = 80 * score_pit + 42
# y = 375
# else:
# x = 80 * (size - score_pit - 1) + 42
# y = 275
Screen.screen.blit(text, (x, y))
pygame.display.update()
else:
score = self.move(next_pit)
return score
def move2(self, start):
'''
the game is played beginning from the start index
the move ends when there the next pit is empty
the move function returns a score
'''
# scores, get coins to move around and set the start pit to zero coins
score = 0
coins = self.board_list[start]
self.board_list[start] = 0
# move the coins to corresponding pits
for coin in range(coins):
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
isGame = False
start = self.nextPit(start)
start = self.nextPit(start)
self.board_list[start] = self.board_list[start] + 1
# next_pit = self.nextPit(start)
next_pit = start
# check if next pit after the move is empty or not
# if empty the move is over, take the coin from the consecutive pit to return as score
# if not empty, recursively move again starting from the next pit
if self.board_list[next_pit] == 1:
# score_pit = self.nextPit(next_pit)
score_pit = (self.board_size - 1) - next_pit
score = self.board_list[score_pit]
self.board_list[score_pit] = 0
else:
score = self.move2(next_pit)
return score