-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
289 lines (244 loc) · 11 KB
/
model.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
# -*- coding: utf-8 -*-
from dataclasses import dataclass, field
from typing import Callable, Iterator
from utils import pairwise, adjacent_coords_cw
import random
import json
@dataclass(frozen = True)
class Treasure:
"""Represents the treasures and treasure objectives of the game.
Input : str, str
No output"""
filepath: str # Path to .png texture file of the treasure.
name: str
@dataclass
class Pawn:
"""Represents the pawns (assigned to players) and contains their lists of objectives.
Input : str, str, list[Treasure]
No output"""
color: str
name: str
objectives: list[Treasure]
def current_objective(self) -> Treasure:
"""Returns the current objective of the pawn.
Input : None
Output : Treasure"""
return self.objectives[0]
def collect(self) -> Treasure:
"""Removes the current objective from the list and returns it.
Input : None
Output : Treasure"""
return self.objectives.pop(0)
def __str__(self) -> str:
"""Returns a string representation of the pawn.
Input : None
Output : str"""
return f"{self.color.upper()}/{self.name!r}"
@dataclass
class Tile:
"""Represents the tiles that compose the labyrinth.
Input : str, list[bool], int, Treasure, list[Pawn]
No output"""
filepath: str # Path to .json file with init data.
sides: list[bool] # Represents the open/closed nature of the four sides.
orientation: int = 0
treasure: Treasure | None = None
pawns: list[Pawn] = field(default_factory=list)
@dataclass
class FixedTile(Tile):
"""These tiles are the ones that are fixed to the board and cannot move.
Input : str, list[bool], int, Treasure, tuple[int, int]
No output"""
fixed_position: tuple[int, int] = (-1, -1)
@dataclass
class MovingTile(Tile):
"""Tiles that can be moved by sliding and rotating.
Input : str, list[bool], int, Treasure
No output"""
def rotate_cw(self) -> None:
"""Rotates the tile clockwise.
Input : None
Output : None"""
self.orientation = (self.orientation+1)%4
def rotate_ccw(self) -> None:
"""Rotates the tile counterclockwise.
Input : None
Output : None"""
self.orientation = (self.orientation-1)%4
@dataclass
class Board:
"""Represents the game board containing all tiles.
Input : list[FixedTile], list[MovingTile]
No output"""
grid: dict[tuple[int, int], Tile]
slideout_position: tuple[int, int] | None = None
def __init__(self, fixed_tiles: list[FixedTile], moving_tiles: list[MovingTile]):
"""
Initializes the grid, then places base tiles according to their fixed positions, then randomly fills the rest of the grid with the moving tiles.
Input : list[FixedTile], list[MovingTile]
Output : None"""
self.grid={}
for ftile in fixed_tiles:
self.grid[ftile.fixed_position] = ftile
for i in range(7):
for j in range(7):
if (i, j) in self.grid: continue
else:
tile = moving_tiles.pop(random.randint(0, len(moving_tiles)-1))
for _ in range(random.randint(0, 3)):
tile.rotate_cw()
self.grid[(i, j)] = tile
def __getitem__(self, pos: tuple[int, int]) -> Tile:
"""Returns the tile at the given position.
Input : tuple[int, int]
Output : Tile"""
return self.grid[pos]
def __setitem__(self, pos: tuple[int, int], tile: Tile) -> None:
"""Sets the tile at the given position.
Input : tuple[int, int], Tile
Output : None"""
if isinstance(self.grid[pos], FixedTile):
raise ValueError("You can't move fixed tiles!")
elif pos == self.slideout_position:
raise ValueError("You can't insert your tile at the same place it came from!")
else:
self.grid[pos] = tile
def slide_tile(self, insertpos: tuple[int, int], tile: MovingTile) -> MovingTile:
"""
Applies the desired slide (if valid), and returns the tile that slid out.
Input : tuple[int, int], MovingTile
Output : MovingTile
"""
match insertpos:
case self.slideout_position:
raise ValueError("Can't cancel previous move.")
case ((0 | 6) as row, (1 | 3 | 5) as col):
first = row
last, step = (6, -1) if row == 0 else (0, 1)
r = range(last, first+step, step)
self.slideout_position = (last, col)
slideout_tile = self.grid.pop(self.slideout_position)
for i_current, i_next in pairwise(r):
self.grid[(i_current, col)] = self.grid.pop((i_next, col))
self.grid[(first, col)] = tile
#remove the pawns from the removed tile
while len(slideout_tile.pawns):
self.grid[(first, col)].pawns.append(slideout_tile.pawns.pop())
case ((1 | 3 | 5) as row, (0 | 6) as col):
first = col
last, step = (6, -1) if col == 0 else (0, 1)
r = range(last, first+step, step)
self.slideout_position = (row, last)
slideout_tile = self.grid.pop(self.slideout_position)
for j_current, j_next in pairwise(r):
self.grid[(row, j_current)] = self.grid.pop((row, j_next))
self.grid[(row, first)] = tile
#remove the pawns from the removed tile
while len(slideout_tile.pawns):
self.grid[(row, first)].pawns.append(slideout_tile.pawns.pop())
case (a, b) if (a is int) and (b is int):
raise ValueError("Invalid insert position")
case _:
raise ValueError("Invalid insertpos type passed.")
assert isinstance(slideout_tile, MovingTile), "Tile that slid out was a fixed one."
return slideout_tile
def connected_tiles(self, origin_pos: tuple[int, int]) -> Iterator[tuple[int, int]]:
"""Returns an iterator over the positions of the tiles adjacent to the given one.
Input : tuple[int, int]
Output : Iterator[tuple[int, int]]"""
origin = self.grid[origin_pos]
for idx, side in enumerate(origin.sides):
if side:
idx = (origin.orientation + idx)%4
neighb_pos = adjacent_coords_cw(origin_pos, idx)
neighb = self.grid.get(neighb_pos)
if neighb is None: continue
opp = (-neighb.orientation + idx + 2)%4
if not neighb.sides[opp]: continue
yield neighb_pos
@dataclass
class GameData:
"""Encapsulates all data related to an individual game's state and can provide insight into it to external callers.
Input : str, list[str]
No output"""
queue: list[Pawn] # Rotating queue for playing order
board: Board
hand: MovingTile # Tile that last slid out of the board, returned by Board.slide_tile method
def __init__(self, playernames: list[str]):
'''Initializes the game objects.
Input : str, list[str]
Output : None'''
COLORS = ['blue', 'red', 'green', 'yellow']
STARTING_POSITIONS = [(0, 0), (0, 6), (6, 0), (6, 6)]
# Get the treasures
with open("./treasures.json" , 'r', encoding ='utf-8') as treasures_file:
data_treasures = json.load(treasures_file)
treasures = {name: Treasure(fpath, name) for name, fpath in data_treasures[0].items()}
# Get players
self.queue = list()
random_treasures = list(treasures.values())
random.shuffle(random_treasures)
for name, color in zip(playernames, COLORS):
objectives = list()
for _ in range(int(24/len(playernames))):
objectives.append(random_treasures.pop())
self.queue.append(Pawn(color, name, objectives))
# Get the tiles
with open("./tiles.json" , 'r', encoding ='utf-8') as tiles:
data_tiles = json.load(tiles)
ftiles = list()
for tile_dict in data_tiles["fixed"]:
if tile_dict["treasure"]==None:
tile = FixedTile(filepath = tile_dict["filepath"], sides = tile_dict["sides"], orientation = tile_dict["orientation"], fixed_position = tuple(tile_dict["position"]))
else:
tile = FixedTile(filepath = tile_dict["filepath"], sides = tile_dict["sides"], orientation = tile_dict["orientation"],treasure = treasures[tile_dict["treasure"]], fixed_position=tuple(tile_dict["position"]))
ftiles.append(tile)
mtiles = list()
for tile_dict in data_tiles["moving"]:
if tile_dict["treasure"]==None:
tile = MovingTile(filepath = tile_dict["filepath"],
sides = tile_dict["sides"])
else:
tile = MovingTile(filepath = tile_dict["filepath"],
sides = tile_dict["sides"],
treasure = treasures[tile_dict["treasure"]])
mtiles.append(tile)
# Board creation
self.board = Board(ftiles, mtiles)
self.hand = mtiles.pop()
#place pawns
for pawn, pos in zip(self.queue, STARTING_POSITIONS):
self.board[pos].pawns.append(pawn)
def get_pawn_container(self, pawn: Pawn) -> tuple[tuple[int, int], Tile]:
"""Returns the position and tile of the given pawn.
Input : Pawn
Output : tuple[tuple[int, int], Tile]"""
for pos, tile in self.board.grid.items():
if pawn in tile.pawns:
return (pos, tile)
raise ValueError(f"{pawn} doesn't exist on the board.")
def get_adjacency_fn(self) -> Callable[[tuple[int, int]], Iterator[tuple[int, int]]]:
"""Returns a function that can be used to get the adjacent tiles to a given position.
Input : None
Output : Callable[[tuple[int, int]], Iterator[tuple[int, int]]]"""
return self.board.connected_tiles
def get_slideout_position(self) -> tuple[int, int] | None:
"""Returns the position from which the last tile slid out.
Input : None
Output : tuple[int, int] | None"""
return self.board.slideout_position
def get_active_player(self) -> Pawn:
"""Returns the active player.
Input : None
Output : Pawn"""
return self.queue[0]
def get_pawns_at_pos(self, pos: tuple[int, int]):
"""Returns the pawns at the given position on the board.
Input : tuple[int, int]
Output : list[Pawn]"""
return self.board[pos].pawns
def advance_queue(self):
"""Update the queue to place the first player at the end of the queue.
Input : None
Output : None"""
self.queue.append(self.queue.pop(0))