forked from ornicar/vindinium-starter-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
97 lines (78 loc) · 2.63 KB
/
game.py
File metadata and controls
97 lines (78 loc) · 2.63 KB
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
import re
TAVERN = 0
AIR = -1
WALL = -2
PLAYER1 = 1
PLAYER2 = 2
PLAYER3 = 3
PLAYER4 = 4
AIM = {'North': (-1, 0),
'East': (0, 1),
'South': (1, 0),
'West': (0, -1)}
class HeroTile:
def __init__(self, id):
self.id = id
class MineTile:
def __init__(self, heroId = None):
self.heroId = heroId
class Game:
def __init__(self, state):
self.state = state
self.board = Board(state['game']['board'])
self.heroes = [Hero(state['game']['heroes'][i]) for i in range(len(state['game']['heroes']))]
self.mines_locs = {}
self.heroes_locs = {}
self.taverns_locs = set([])
for row in range(len(self.board.tiles)):
for col in range(len(self.board.tiles[row])):
obj = self.board.tiles[row][col]
if isinstance(obj, MineTile):
self.mines_locs[(row, col)] = obj.heroId
elif isinstance(obj, HeroTile):
self.heroes_locs[(row, col)] = obj.id
elif (obj == TAVERN):
self.taverns_locs.add((row, col))
class Board:
def __parseTile(self, str):
if (str == ' '):
return AIR
if (str == '##'):
return WALL
if (str == '[]'):
return TAVERN
match = re.match('\$([-0-9])', str)
if (match):
return MineTile(match.group(1))
match = re.match('\@([0-9])', str)
if (match):
return HeroTile(match.group(1))
def __parseTiles(self, tiles):
vector = [tiles[i:i+2] for i in range(0, len(tiles), 2)]
matrix = [vector[i:i+self.size] for i in range(0, len(vector), self.size)]
return [[self.__parseTile(x) for x in xs] for xs in matrix]
def __init__(self, board):
self.size = board['size']
self.tiles = self.__parseTiles(board['tiles'])
def passable(self, loc):
'true if can not walk through'
x, y = loc
pos = self.tiles[x][y]
return (pos != WALL) and (pos != TAVERN) and not isinstance(pos, MineTile)
def to(self, loc, direction):
'calculate a new location given the direction'
row, col = loc
d_row, d_col = AIM[direction]
n_row = row + d_row
if (n_row < 0): n_row = 0
if (n_row > self.size): n_row = self.size
n_col = col + d_col
if (n_col < 0): n_col = 0
if (n_col > self.size): n_col = self.size
return (n_row, n_col)
class Hero:
def __init__(self, hero):
self.name = hero['name']
self.pos = hero['pos']
self.life = hero['life']
self.gold = hero['gold']