-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
66 lines (55 loc) · 2.25 KB
/
player.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
from card import Card
from constants import BOARD_HEIGHT
class Player:
"""
Players exist in the game and contain information about deploying units.
A Player has a color, a number of warp, and a palette of cards from which they can deploy units.
"""
def __init__(self, color, flipped=False):
"""
Return a new player of the specified color.
:param color: the color of the player, an int either WHITE or BLACK
:param flipped: a boolean that when True, flips the movement of all the player's units
:return:
"""
self.color = color # WHITE or BLACK
self.warp = 1 # amount of warp available to the player
self.palette = {} # key: unit_type -> item: card
self.flipped = flipped # exactly one player should be flipped so all units move away from the starting zone
def add_card(self, unit_type, amount):
"""
Add a card to the player's palette.
:param unit_type: an int corresponding to the unit type, eg: WARPLING_TYPE
:param amount: the total number of units that can be deployed from the card
:return: nothing
"""
self.palette[unit_type] = Card(unit_type, amount)
def remove_card(self, unit_type):
"""
Remove a card from the player's palette.
:param unit_type: the unit type to remove
:return: nothing
"""
del self.palette[unit_type]
def adjust(self, y):
"""
Return the distance from the players end of the board to the given y-coordinate.
:param y: y position on the board
:return: how far that position is from the calling player
"""
return y if not self.flipped else BOARD_HEIGHT - y - 1
def generate_dict(self):
"""
Create a dictionary object containing all of the player's data to be sent to the client.
:return: a dictionary formatted to be an argument for json.dumps
"""
palette = []
for unit_type in self.palette:
palette.append(self.palette[unit_type].generate_dict())
dictionary = {
'color': self.color,
'warp': self.warp,
'palette': palette,
'flipped': self.flipped
}
return dictionary