-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap_card.py
More file actions
79 lines (64 loc) · 2.77 KB
/
map_card.py
File metadata and controls
79 lines (64 loc) · 2.77 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
from card import CardStatus
from config import global_config as config
from player import PlayerTeam
import random
# Each map card has 1 bomb, 7 neutral, 8 lagging color, 9 starting color
class MapCard(object):
''' A MapCard that holds the information the Spymaster sees.
- Bomb locations, neutral locations, blue locations, red locations
Note: the map is visually represented as an n x n grid, but is
programatically represented as a 1D array.
'''
def __init__(self):
self.starting_color = random.choice([PlayerTeam.RED, PlayerTeam.BLUE])
self.map = self._gen_map_card()
self.bomb_location = self.map.index(CardStatus.BOMB)
def _gen_map_card(self):
''' Generate a MapCard by random placing the desired entities on a grid
of the desired size, as passed in from config.
'''
# TODO: This generation logic needs to be changed
# It is very hard to remember a random set of 8 or 9 squares on a 25 square grid
# Rather, it is significantly easier to remember when the squares are in contiguous blobs
# or have some pattern to them.
mapping = []
# Add bombs, reds, blues, and neutrals
for bomb in range(config.getNumBombs()):
mapping.append(CardStatus.BOMB)
for red in range(config.getNumReds()):
mapping.append(CardStatus.RED)
for blue in range(config.getNumBlues()):
mapping.append(CardStatus.BLUE)
for neutral in range(config.getNumNeutrals()):
mapping.append(CardStatus.NEUTRAL)
# Randomize ordering
random.shuffle(mapping)
return mapping
def get_starting_color(self):
''' Get the color of the team whose turn it is first'''
return self.starting_color
def get_bomb_location(self):
''' Get the location of the bomb on the map'''
return self.bomb_location
def get_card_type_at_position(self, position):
''' Get the type of card at a given position on the map '''
return self.map[position]
def get_num_card_by_type(self, card_type):
''' Get the number of a certain type of card on the map '''
return self.map.count(card_type)
# Serialize the map card into JSON
def serialize(self):
''' Serialize the data in the MapCard '''
return {
"starting_color": self.starting_color.value,
"map": [str(status.name) for status in self.map],
"bomb_location": self.bomb_location
}
def __repr__(self):
output = ""
max_len = 15
spacer = lambda x: x.name + " "*(max_len - len(x.name))
for x in range(5):
output += "".join(map(spacer, self.map[5*x:5*x+5]))
output += "\n"
return output