-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
235 lines (209 loc) · 9.51 KB
/
engine.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
import pyglet
import constants
import shapes
class Game:
def __init__(self, players, activePlayers, boardSize):
self.players = players
self.activePlayers = activePlayers
self.alivePlayers = [i for i in activePlayers]
self.playerTurn = 0
self.round = 1
self.ongoing = True
self.board = Board(boardSize, self)
self.turnPops = 0
# self.turnPopAnims = 0
def takeTurn(self, row, column):
# Make sure tile is free
if self.board.checkFree(row, column, self.getCurrPlayer().color):
self.turnPops = 0
constants.playerSounds[self.getCurrPlayer().id].play()
self.board.animNewOrb(row, column, self.getCurrPlayer().color)
self.board.addToTileCount(row, column, self.getCurrPlayer().color)
self.nextPlayer()
# Eliminate players who lost all tiles
while (not self.board.colorIsLeft(self.getCurrPlayer().color)) and self.round > 1:
print("Removing Player: " + self.getCurrPlayer().name)
self.alivePlayers.pop(self.playerTurn)
if self.playerTurn == len(self.alivePlayers):
self.playerTurn = 0
# End game if 1 player left
if len(self.alivePlayers) == 1:
self.ongoing = False
self.savePlayerData()
return True, True, self.players[self.alivePlayers[0]]
return True, False, self.getCurrPlayer()
else:
print("Invalid move! Tile occupied.")
return False, -1
def nextPlayer(self):
self.playerTurn += 1
if self.playerTurn == len(self.alivePlayers):
self.round += 1
print("New Round! Round #" + str(self.round))
self.playerTurn = 0
def getCurrPlayer(self):
return self.players[self.alivePlayers[self.playerTurn]]
def savePlayerData(self):
# add wins and losses
for i in range(len(self.activePlayers)):
if self.activePlayers[i] == self.alivePlayers[0]:
self.players[self.activePlayers[i]].wins += 1
else:
self.players[self.activePlayers[i]].losses += 1
print("Wins and losses updated.")
# write to file
playerData = open("playerdata.txt", "w")
for player in self.players:
playerData.write(str(player.id) + "|" + player.name + "|" + player.color + "|" + str(player.wins)
+ "|" + str(player.losses))
if self.players.index(player) < len(self.players) - 1:
playerData.write("\n")
playerData.close()
print("Data saved to file.")
class Player:
def __init__(self, id, name, color, wins, losses):
self.id = id
self.name = name
self.color = color
self.wins = wins
self.losses = losses
class Board:
def __init__(self, boardSize, game):
self.game = game
self.boxSize = 80
self.orbSize = 60
self.boxPad = 5
self.boardSize = boardSize
self.gridBatch = pyglet.graphics.Batch()
self.orbBatch = pyglet.graphics.Batch()
self.transferring = 0
# Drawing Background Rectangle
bgVertices = (0, 0,
self.boxPad + (self.boardSize * (self.boxSize + self.boxPad)), 0,
self.boxPad + (self.boardSize * (self.boxSize + self.boxPad)),
self.boxPad + (self.boardSize * (self.boxSize + self.boxPad)),
0, self.boxPad + (self.boardSize * (self.boxSize + self.boxPad)))
bgColors = constants.rgbColors[self.game.getCurrPlayer().color]
self.bgRectangle = shapes.Rectangle(bgVertices, bgColors, self.gridBatch)
# Drawing Tiles
self.grid = []
for i in range(boardSize):
newRow = []
for j in range(boardSize):
# Corner and side tiles have different max
if (i == 0 or i == boardSize-1) and (j == 0 or j == boardSize-1):
maxHold = 1
elif (i == 0 or i == boardSize-1) and (0 < j < boardSize-1) or (0 < i < boardSize-1) and (j == 0 or j == boardSize-1):
maxHold = 2
else:
maxHold = 3
vertices = (
(i * (self.boxSize + self.boxPad)) + self.boxPad, (j * (self.boxSize + self.boxPad)) + self.boxPad,
(i * (self.boxSize + self.boxPad)) + self.boxPad + self.boxSize,
(j * (self.boxSize + self.boxPad)) + self.boxPad,
(i * (self.boxSize + self.boxPad)) + self.boxPad + self.boxSize,
(j * (self.boxSize + self.boxPad)) + self.boxPad + self.boxSize,
(i * (self.boxSize + self.boxPad)) + self.boxPad,
(j * (self.boxSize + self.boxPad)) + self.boxPad + self.boxSize)
newRow.append(shapes.Tile(vertices, constants.rgbColors["white"], self.gridBatch, i, j, maxHold))
self.grid.append(newRow)
def checkFree(self, row, column, color):
if self.grid[row][column].color == -1 or self.grid[row][column].color == color:
return True
return False
def colorIsLeft(self, color):
for i in range(self.boardSize):
for j in range(self.boardSize):
if self.grid[i][j].color == color:
print("Elim check: " + color + " tile at " + str(i) + ", " + str(j))
return True
return False
# adding by clicking
def addToTileCount(self, row, column, color):
canHold = self.grid[row][column].addCount(color)
self.grid[row][column].updateOrbs()
if not canHold:
self.popTile(row, column, color)
self.animPopTile(row, column)
# adding from another tile popping
def addByPop(self, row, column, color):
canHold = self.grid[row][column].addCount(color)
# to prevent infinite loops
if not canHold and self.game.turnPops < (self.boardSize ** 2):
self.grid[row][column].schedPopAnim = True
self.popTile(row, column, color)
def popTile(self, row, column, color):
self.game.turnPops += 1
# empty popped tile
self.grid[row][column].emptyCount()
print("Popping: " + str(row) + ", " + str(column) + "! #" + str(self.game.turnPops))
# addByPop above, below, left, right, if possible
if row-1 >= 0:
self.addByPop(row-1, column, color)
if row+1 < self.boardSize:
self.addByPop(row+1, column, color)
if column-1 >= 0:
self.addByPop(row, column-1, color)
if column+1 < self.boardSize:
self.addByPop(row, column+1, color)
# Call tile to animate new orb
def animNewOrb(self, row, column, color):
self.grid[row][column].addOrb(color, self.orbSize, self.orbBatch)
def animPopTile(self, row, column):
# remove pop animation schedule
constants.popsound.play()
self.grid[row][column].schedPopAnim = False
print("Starting pop animation at " + str(row) + ", " + str(column) + ".")
# addToTile above, below, left, right, if possible
if row - 1 >= 0:
self.transferOrb(self.grid[row][column], self.grid[row - 1][column], "left")
if row + 1 < self.boardSize:
self.transferOrb(self.grid[row][column], self.grid[row + 1][column], "right")
if column - 1 >= 0:
self.transferOrb(self.grid[row][column], self.grid[row][column - 1], "down")
if column + 1 < self.boardSize:
self.transferOrb(self.grid[row][column], self.grid[row][column + 1], "up")
def transferOrb(self, giver, receiver, direction):
orb = giver.orbs[0]
if direction == "up":
orb.ydir = direction
orb.xdir = -1
orb.dx = 0
orb.dy = 200
elif direction == "down":
orb.ydir = direction
orb.xdir = -1
orb.dx = 0
orb.dy = -200
elif direction == "left":
orb.ydir = -1
orb.xdir = direction
orb.dx = -200
orb.dy = 0
elif direction == "right":
orb.ydir = -1
orb.xdir = direction
orb.dx = 200
orb.dy = 0
# define new orb bounds
orb.leftBound = receiver.leftBound
orb.rightBound = receiver.rightBound - orb.size
orb.lowerBound = receiver.lowerBound
orb.upperBound = receiver.upperBound - orb.size
if not orb.transfer:
self.transferring += 1
orb.transfer = True
receiver.orbs.append(giver.orbs.pop(0))
# move all orbs
def update(self, dt):
for i in range(self.boardSize):
for j in range(self.boardSize):
for orb in self.grid[i][j].orbs:
gotThere = orb.move(dt)
if gotThere:
# only trigger events once orb arrives in new tile
self.transferring -= 1
self.grid[i][j].updateOrbs()
# only pop once scheduled, AND all orbs are appended to lists
if len(self.grid[i][j].orbs) > self.grid[i][j].maxHold:
self.animPopTile(i, j)