-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalahGUI.py
428 lines (345 loc) · 13.5 KB
/
kalahGUI.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/python
import pygame
from copy import copy
import os, time, sys
from random import randint
import pickle
#where the graphics are
def Prepare_window(): #create the game window
#os.environ['SDL_VIDEO_CENTERED'] = '1' #should center pygame window on the screen
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((850,500))
pygame.display.set_caption('Python Kalah')
return screen
def LoadImages(): #loads the sprites
board = [1,1]
board[0] = pygame.image.load(os.path.join("assets","board.png")).convert()
board[0] = pygame.transform.scale(board[0], (800, 500))
board[1] = (0,0)
return [board]
def Draw(screen, assets): #draws all the surfaces in assets
for element in assets:
screen.blit(element[0], element[1]) #this method is old, and is kinda same of box's one
def initText(): #does stuff so i can draw over the screen.
pygame.font.init()
font_path = "assets/Font/FantasqueSansMono-Regular.ttf"
font_size = 32
return pygame.font.Font(font_path, font_size)
def printText(text, screen, fontObj, x, y):
display = fontObj.render(text, 1, (0, 0, 0))
screen.blit(display, (x, y))
def textPrinter(text, screen, fontObj, x, y, delay): #almost same as printText but do some more stuff
printText(text, screen, fontObj, x, y) #delay is the milliseconds. Integer
print text
pygame.display.flip()
pygame.time.delay(delay) #i know this is dirty im' sorry
#i'm really sorry it's disgusting i doesn't even really does what i want
def checkRules(box, game, houses, boxes, screen, fontObj): #this is going to check if kalah rules apply.
freeTurn = False
if(isinstance(box, Box)):
seeds = 0
print "last box sowed : " + str(box.number)
if(box.seeds == 1): #the box have been sowed just now. you empty the opposite one
if(game.turn == 1 and box.number < 6):
print "the opposite box is : " + str(getOppositeBox(box))
if(boxes[getOppositeBox(box)].seeds > 0):
seeds = seeds + box.removeAllSeeds() #the removeAllSeeds functions returns the seeds removed
seeds = seeds + boxes[getOppositeBox(box)].removeAllSeeds()
houses[1].addSeeds(seeds)
if(game.turn == 2 and box.number > 5):
print "the opposite box is : " + str(getOppositeBox(box))
if(boxes[getOppositeBox(box)].seeds > 0):
seeds = seeds + box.removeAllSeeds() #the removeAllSeeds functions returns the seeds removed
seeds = seeds + boxes[getOppositeBox(box)].removeAllSeeds()
houses[0].addSeeds(seeds)
if(canCurrentPlayerPlay(boxes, game) == False):
emptyAllBoxes(boxes, houses)
print "game finished !"
result = whoWon(houses)
if(result == 0):
game.turn = 0
textPrinter("player 1 won !!!", screen, fontObj, 148, 60, 3000)
if(result == 1):
game.turn = 0
textPrinter("player 2 won !!!", screen, fontObj, 148, 60, 3000)
if(result == 3):
game.turn = 0
textPrinter("it's a draw.", screen, fontObj, 148, 60, 3000)
###BUGGED
#First you check if the player can play again
#if he does he plays again
if(isinstance(box, House)): #check if the last play filled a player's house (and give him another turn)
if(box.number == houses[0].number and game.turn == 2): #if the last filled house's number is the same as the player two's house (and therefore is the same house) AND it's player two's turn
print "player two gets another turn"
freeTurn = True
if(box.number == houses[1].number and game.turn == 1):
print "player one gets another turn"
freeTurn = True
#if he can't you change turn
if(freeTurn == False and game.turn != 0): #handle which player's turn it is.
if(game.turn == 1):
game.turn = 2
else:
game.turn = 1
###
def getOppositeBox(box): #takes a box as a parameter and returns the number|the box opposed to it
return (11 - box.number)
def areAllBoxesEmpty(boxes): #checks if all the boxes are empty
seeds = 0
for element in boxes:
seeds = seeds + element.getSeeds()
if(seeds == 0):
return True
else:
return False
def whoWon(houses):
if(houses[0].getSeeds() > houses[1].getSeeds()):
return 1 #player 2 won
elif(houses[1].getSeeds() > houses[0].getSeeds()):
return 0 #player 1 won
elif(houses[0].getSeeds() == houses[1].getSeeds()):
return 3 #it's a draw !!!
def canCurrentPlayerPlay(boxes, kalah): #says if the player of the current turn can play. Takes the boxes (to see if they're empty) and the board (to get the player) as param
seeds = 0
if(kalah.turn == 1):
for element in boxes:
if element.number < 6:
seeds = seeds + element.getSeeds()
if(seeds > 0):
print "the player can play; he has : " + str(seeds) + " seeds"
return True
else:
print "the player can't play; he has : " + str(seeds) + " seeds"
return False
if(kalah.turn == 2):
for element in boxes:
if element.number > 5:
seeds = seeds + element.getSeeds()
if(seeds > 0):
print "the player can play; he has : " + str(seeds)
return True
else:
print "the player can play; he has : " + str(seeds) + " seeds"
return False
if(kalah.turn == 0 or kalah.turn == 3):
return False
def emptyAllBoxes(boxes, houses): #to be called when a player can't play anymore. it puts the remaining seeds in the corresponding house
#seeds0 = 0
#seeds1 = 0
for element in boxes:
if(element.number < 6):
#seeds1 = seeds1 + element.getSeeds() #faulty line
houses[1].addSeeds(element.getSeeds())
element.removeAllSeeds()
if(element.number > 5):
#seeds0 = seeds0 + element.getSeeds()
houses[0].addSeeds(element.getSeeds())
element.removeAllSeeds()
def printGameState(game, boxes, houses):
print " "
print "player's turn : " + str(game.turn)
print "left house's seeds : " + str(houses[0].seeds) + " right house's seeds : " + str(houses[1].seeds)
for element in boxes:
print "seeds in the box number " + str(element.number) + " : " + str(element.seeds)
def loadSave(game, boxes, houses, saveFile):
print "loading the save..."
try:
almightyObj = pickle.load(saveFile)
game.turn = almightyObj[0][1]
for element in almightyObj:
if element[0] != "turn":
if element[0] <= 11:
boxes[element[0]].seeds = element[1]
else:
houses[element[0]].seeds = element[1]
except:
print("error loading file !")
os.remove("save.dump")
class Kalah: #Kalah object. the game board
def __init__(self):
self.houses = [0, 0] #represent the houses
self.turn = 1 #represent the player's turn. 1 for you, and 2 for anyone someone. Or whatever it doesn't matter
def getBoard(self):
return self.board
def setTurn(self, number):
self.turn = number
class House: #houses are where the seeds end up
def __init__(self, number): #houses are really like boxes, but they can't move their dots out and stuff like that
self.seeds = 0
self.dots = list()
self.house = [1,1]
self.number = number ###Useful to check which house is it exactly (since they're all the sames)
def addSeed(self):
self.seeds = self.seeds + 1
def addSeeds(self, number):
self.seeds = self.seeds + number
def getSeeds(self):
return self.seeds
def removeSeed(self):
if self.seeds > 0:
self.seeds = self.seeds - 1
def getCoords(self):
return [ self.house[1][0], self.house[1][1], self.house[0].get_width(), self.house[0].get_height() ] #returns data in this order : box's x, box's y, box's width, box's height
def createRect(self, x, y): #create the house
self.house[0] = pygame.image.load(os.path.join("assets","house.png")).convert_alpha() #the rect object #change this to convert() to see the house
self.house[0] = pygame.transform.scale(self.house[0], (100, 480))
self.house[1] = (x,y) #the coordinates
def Draw(self, screen):
screen.blit(self.house[0] , self.house[1])
def populate(self, screen): #create the seeds in a box
i = 0 #the seeds will be distributed by rows of four
coord = self.getCoords()
x = coord[0] + 10
y = coord[1] + 5
while(i < self.seeds):
if(i % 4 == 0):
x = coord[0] + 5
y = y + 10
self.dots.append(Dot(i, self))
self.dots[i].createRect(x, y)
self.dots[i].DrawDot(screen)
x = x + 10
i = i+1
class Box: #boxes are where the seeds are put
def __init__(self, seeds, number):
self.seeds = seeds
self.number = number #number is used to keep track of the boxes (like, to know which one is it)
self.dots = list() #contains the dots stored in the box
self.box = [1,1] #contains the surface object, and its coordinates (in the box[1])
self.position = [1,1]
def isEmpty(self):
if(self.getSeeds() == 0):
return True
else:
return False
def getSeeds(self):
return self.seeds
def getCoords(self):
return [ self.box[1][0], self.box[1][1], self.box[0].get_width(), self.box[0].get_height() ] #returns data in this order : box's x, box's y, box's width, box's height
def addSeed(self):
self.seeds = self.seeds + 1
def removeSeed(self):
if self.seeds > 0:
self.seeds = self.seeds - 1
def removeAllSeeds(self): #remove all the seeds of a box and return how much were removed
seeds = self.seeds
self.seeds = 0 #used for when you sow an empty box, with the opposite one non-empty
return seeds
def createRect(self, x, y): #create the box
self.box[0] = pygame.image.load(os.path.join("assets","box.png")).convert() #the rect object
self.box[0] = pygame.transform.scale(self.box[0], (80, 80))
self.box[1] = (x,y) #the coordinates
def populate(self, screen): #create the seeds in a box
i = 0 #the seeds will be distributed by rows of four
coord = self.getCoords()
x = coord[0] + 10
y = coord[1] + 5
while(i < self.seeds):
if(i % 4 == 0):
x = coord[0] + 5
y = y + 10
self.dots.append(Dot(i, self))
self.dots[i].createRect(x, y)
self.dots[i].DrawDot(screen)
x = x + 10
i = i+1
def Draw(self, screen):
screen.blit(self.box[0] , self.box[1])
def isClicked(self, x, y): #this function will determine if a box is being clicked or not
#if(&&): #on each click event, all the squares with this method will be called
boxCoords = self.getCoords() #if a box is being clicked, this function will return true
if(x >= boxCoords[0] and x <=boxCoords[0] + 80 and y >= boxCoords[1] and y <= boxCoords[1] + 80): #box's height and width are harcoded. could be changed in the future
'''print self
print self.number'''
return True
else:
return False
def distributeSeeds(self, boxes, houses, game, screen, fontObj): #distribute the seeds in the boxes and houses. annoying as fuck
lastBox = None #this will contain the last object we put a seed in
i = self.number
while(self.seeds > 0 ):
if(self.seeds > 0): #can be removed
if(i != 11 and i != 5):#this is broken (or not in the end. it's not)
i = i +1
boxes[i].addSeed()
if(self.seeds == 1):
lastBox = boxes[i]
#checkRules(lastBox)
elif(i == 5): #if we're at the fifth cell, there's special stuff to do
i = 6
houses[1].addSeed()
self.removeSeed()
lastBox = houses[1]
if(self.seeds > 0):
boxes[6].addSeed()
if(self.seeds == 1):
lastBox = boxes[6]
#checkRules(lastBox)
elif(i == 11):
i = 0
houses[0].addSeed()
self.removeSeed()
lastBox = houses[0]
if(self.seeds > 0):
boxes[0].addSeed()
if(self.seeds == 1):
lastBox = boxes[0]
#checkRules(lastBox)
self.removeSeed()
if(isinstance(lastBox, Box) or isinstance(lastBox, House)):
#print "the last box is : " + str(lastBox)
checkRules(lastBox, game, houses, boxes, screen, fontObj)
class Dot: #dots are the seeds
def __init__(self, num, parentBox): #parentbox can be removed, but no time for this
self.num = num
self.dot = [1,1]
self.position = [1,1]
#print "dot created ! " + str(self.num)
def createRect(self, x, y):
self.dot[0] = pygame.image.load(os.path.join("assets", "seed.png")).convert_alpha()
self.dot[0] = pygame.transform.scale(self.dot[0], (5, 5))
self.dot[1] = (x, y)
def DrawDot(self, screen):
screen.blit(self.dot[0], self.dot[1])
class Button:
def __init__(self, text, num):
self.num = num
self.text = text
self.rect = [1,1]
def createRect(self, x, y, name): #name is the file name of the sprite
self.rect[0] = pygame.image.load(os.path.join("assets", str(name)+".png")).convert_alpha()
self.rect[1] = (x, y)
def Draw(self, screen):
screen.blit(self.rect[0] , self.rect[1])
def getCoords(self):
return [ self.rect[1][0], self.rect[1][1], self.rect[0].get_width(), self.rect[0].get_height() ] #returns data in this order : box's x, box's y, box's width, box's height
def isClicked(self, x, y): #this function will determine if a box is being clicked or not
#on each click event, all the squares with this method will be called
boxCoords = self.getCoords() #if a box is being clicked, this function will return true
if(x >= boxCoords[0] and x <=boxCoords[0] + 80 and y >= boxCoords[1] and y <= boxCoords[1] + 80): #box's height and width are harcoded. could be changed in the future
return True
else:
return False
class ReloadGame(Button):
def reload(self, game, boxes, houses):
print "reloading the game..."
game.turn = 1
for element in boxes:
element.seeds = 4
for element in houses:
element.seeds = 0
class SaveGame(Button):
def save(self, game, boxes, houses, screen, fontObj, buttons, saveFile):
print "saving the game..."
try:
os.remove("save.dump")
except Exception:
print "file already deleted..."
saveFile = open("save.dump", "w+")
almightyObj = [["turn", game.turn]]
for element in boxes:
almightyObj.append([element.number, element.seeds])
for element in houses:
almightyObj.append([element.number, element.seeds])
pickle.dump(almightyObj, saveFile)