-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcard.py
84 lines (75 loc) · 2.87 KB
/
card.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
from typing import List
from card_action import *
from player import *
from resource import *
class Card:
def __init__(self, name: str, cost_resource: str, cost_amount: int,
actions: List[CardAction], description: str = None):
"""
Create a card.
:param name: Card name.
:param cost_resource: Resource deducted.
:param cost_amount: Amount of resource to deduct.
:param actions: List of CardAction(s) played by this card.
:param description: Optional description of card actions.
"""
self.name = name
self.cost_resource = cost_resource
self.cost_amount = cost_amount
assert len(actions) >= 1
self.actions = actions
self.description = description
def can_be_played(self, player: Player) -> bool:
"""
:param player: A player.
:return: True if that player can play this card.
"""
return player.resources[self.cost_resource] >= self.cost_amount
def play(self, you: Player, opponent: Player, is_by_you: bool) -> bool:
"""
Play the card, from your perspective.
:param you: Player representing you.
:param opponent: Player representing your opponent.
:param is_by_you: True if the card was played by you.
:return: True if the card was successfully played; False if the card
is not playable.
"""
if is_by_you:
return self._play(you, opponent)
else:
return self._play(opponent, you)
def _play(self, player: Player, opponent: Player) -> bool:
"""
Play the card, from the card player's perspective.
:param player: Player who played the card.
:param opponent: Player representing their opponent.
:return: True if the card was successfully played; False if the card
is not playable.
"""
# Can't afford
if not self.can_be_played(player):
return False
player.lose_resource(self.cost_resource, self.cost_amount)
for action in self.actions:
action.do(player, opponent)
return True
def cost_string(self) -> str:
"""
:return: String representing card resource cost and amount.
"""
return '{} {}'.format(self.cost_amount,
resource_long[self.cost_resource])
def action_string(self) -> str:
"""
:return: Card description if it had one; else use the card actions to
generate a description.
"""
if self.description:
return self.description
return ', '.join(map(str, self.actions))
def __str__(self) -> str:
"""
:return: String representation.
"""
return '{} ({}; {})'.format(self.name.title(), self.cost_string(),
self.action_string())