Lab 8 - OOP
If you've ever used an object, this task is quite easy - made to practice manipulating data via OOP.
The first question is non conding - what would python do.
Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/lab/lab08/
Solution: https://github.com/tomthestrom/cs61a/blob/master/lab/lab08/classes.py
Q2: Making Cards
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 classes.py
. This constructor takes three arguments:
the
name
of the card, a stringthe
attack
stat of the card, an integerthe
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(object):
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 ***"
Q2: Solution
class Card(object):
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 ***"
self.name = name
self.attack = attack
self.defense = defense
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 ***"
return self.attack - other_card.defense / 2
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)t
Q3: Making a Player
Now that we have cards, we can make a deck, but we still need players to actually use them. We'll now fill in the implementation of the Player
class.
A Player
instance has three instance attributes:
name
is the player's name. When you play the game, you can enter your name, which will be converted into a string to be passed to the constructor.deck
is an instance of theDeck
class. You can draw from it using its.draw()
method.hand
is a list ofCard
instances. Each player should start with 5 cards in their hand, drawn from theirdeck
. Each card in the hand can be selected by its index in the list during the game. When a player draws a new card from the deck, it is added to the end of this list.
Complete the implementation of the constructor for Player
so that self.hand
is set to a list of 5 cards drawn from the player's deck
.
Next, implement the draw
and play
methods in the Player
class. The draw
method draws a card from the deck and adds it to the player's hand. The play
method removes and returns a card from the player's hand at the given index
class Player(object):
def __init__(self, deck, name):
"""Initialize a Player object.
A Player starts the game by drawing 5 cards from their deck. Each turn,
a Player draws another card from the deck and chooses one to play.
>>> test_card = Card('test', 100, 100)
>>> test_deck = Deck([test_card.copy() for _ in range(6)])
>>> test_player = Player(test_deck, 'tester')
>>> len(test_deck.cards)
1
>>> len(test_player.hand)
5
"""
self.deck = deck
self.name = name
"*** YOUR CODE HERE ***"
self.hand = [self.deck.draw() for card in range(5)]
def draw(self):
"""Draw a card from the player's deck and add it to their hand.
>>> test_card = Card('test', 100, 100)
>>> test_deck = Deck([test_card.copy() for _ in range(6)])
>>> test_player = Player(test_deck, 'tester')
>>> test_player.draw()
>>> len(test_deck.cards)
0
>>> len(test_player.hand)
6
"""
assert not self.deck.is_empty(), 'Deck is empty!'
"*** YOUR CODE HERE ***"
self.hand.append(self.deck.draw())
def play(self, card_index):
"""Remove and return a card from the player's hand at the given index.
>>> from cards import *
>>> test_player = Player(standard_deck, 'tester')
>>> ta1, ta2 = TACard("ta_1", 300, 400), TACard("ta_2", 500, 600)
>>> tutor1, tutor2 = TutorCard("t1", 200, 500), TutorCard("t2", 600, 400)
>>> test_player.hand = [ta1, ta2, tutor1, tutor2]
>>> test_player.play(0) is ta1
True
>>> test_player.play(2) is tutor2
True
>>> len(test_player.hand)
2
"""
"*** YOUR CODE HERE ***"
return self.hand.pop(card_index)
def display_hand(self):
"""
Display the player's current hand to the user.
"""
print('Your hand:')
for card_index, displayed_card in zip(range(len(self.hand)),[str(card) for card in self.hand]):
indent = ' '*(5 - len(str(card_index)))
print(card_index, indent + displayed_card)
def play_random(self):
"""
Play a random card from hand.
"""
return self.play(random.randrange(len(self.hand)))
Last updated