Problem 2.1: Making Cards (100pts)
To play a card game, we're going to need to have cards, so let's make some! We're gonna implement the basics of the Card
class first.
First, implement the Card
class constructor in hw06.py
. This constructor takes three arguments:
- the
name
of the card, a string - the
attack
stat of the card, an integer - the
defense
stat of the card, an integer
Each Card
instance should keep track of these values using instance attributes called name
, attack
, and defense
.
You should also implement the power
method in Card
, which takes in another card as an input and calculates the current card's power. Check the Rules section if you want a refresher on how power is calculated.
class Card:
cardtype = 'Staff'
def __init__(self, name, attack, defense):
"""
Create a Card object with a name, attack,
and defense.
>>> staff_member = Card('staff', 400, 300)
>>> staff_member.name
'staff'
>>> staff_member.attack
400
>>> staff_member.defense
300
>>> other_staff = Card('other', 300, 500)
>>> other_staff.attack
300
>>> other_staff.defense
500
"""
"*** YOUR CODE HERE ***"
def power(self, other_card):
"""
Calculate power as:
(player card's attack) - (opponent card's defense) / 2
where other_card is the opponent's card.
>>> staff_member = Card('staff', 400, 300)
>>> other_staff = Card('other', 300, 500)
>>> staff_member.power(other_staff)
150.0
>>> other_staff.power(staff_member)
150.0
>>> third_card = Card('third', 200, 400)
>>> staff_member.power(third_card)
200.0
>>> third_card.power(staff_member)
50.0
"""
"*** YOUR CODE HERE ***"
def effect(self, other_card, player, opponent):
"""
Cards have no default effect.
"""
return
def __repr__(self):
"""
Returns a string which is a readable version of
a card, in the form:
<cardname>: <cardtype>, [<attack>, <defense>]
"""
return '{}: {}, [{}, {}]'.format(self.name, self.cardtype, self.attack, self.defense)
def copy(self):
"""
Returns a copy of this card.
"""
return Card(self.name, self.attack, self.defense)