Instructions
In this homework, you are required to complete the problems described in section 2.
The starter code for these problems is provided in hw06.py
.
Submission: As instructed before, you need to submit your work with Ok by python ok --submit
.
You may submit more than once before the deadline,
and your score of this assignment will be the highest one of all your submissions.
Readings: You might find the following references to the textbook useful:
Required Problems
In this section, you are required to complete the problems below and submit your code to OJ website.
Remember, you can use ok
to test your code:
$ python ok # test all functions
$ python ok -q <func> # test single function
Problem 1: Encryption (200pts)
In this part and jff of this homework, you will implement a simple encryption system. You will create a class-based structure that can encrypt and decrypt strings consisting of lowercase letters. The encryption system should consist of several classes and methods, which implement different encryption strategies.
The encryption system includes 2 parts: Encryption Methods and Encryption Extensions.
Problem 1.1: Encryption Methods (100pts)
There are three main subclasses for encryption, each implementing a different technique:
- Shift Cipher: The constructor takes an integer representing the shift amount. The
encrypt
method shifts each letter of the string by the given number of positions in the alphabet. - Dictionary Cipher: The constructor takes a dictionary that maps characters to other characters. The
encrypt
method replaces each letter in the string with its corresponding character from the dictionary. - Fence Cipher: The constructor takes an integer representing the number of rails. The
encrypt
method uses the rail fence cipher to rearrange the string based on the number of rails.
Hint 1: You might find some built-in functions that convert between single character strings and unicode (a character encoding that is more general than ascii code) integers helpful, like
ord
,chr
, etc.Hint 2: You might find some built-in string methods helpful, like
join
, etc.Hint 3: How the Fence Cipher Works:
- Set the Number of Rails (n): Choose an integer
n
that represents the number of rails (or rows).- Fill the Rails: Write the characters of the plaintext column by column.
- Read the Rails: Read the characters row by row to get the ciphertext.
Let's walk through an example where the plaintext is
"HELLO"
and we choosen = 3
(3 rails):Plaintext: h e l l o Rails: 3 Step 1: Fill the Rails: Rail 1: h l Rail 2: e o Rail 3: l Step 2: Read the characters row by row: Ciphertext: hleol
Both subclasses of the encryption methods have an encrypt
method that takes a string and returns the encrypted version of the string.
class methods:
"""The base class for encryption methods."""
def encrypt(self, message):
"""Encrypt the message."""
pass
class shiftcipher(methods):
"""A class for shift cipher encryption method.
>>> cipher = shiftcipher(3)
>>> cipher.encrypt('hello')
'khoor'
>>> cipher.encrypt('world')
'zruog'
"""
def __init__(self, shift):
"""
Initialize the shift cipher with a shift.
If shift is greater than 26, shift = shift % 26.
"""
"*** YOUR CODE HERE ***"
def encrypt(self, message):
"""Encrypt the message by shifting each character by the shift."""
"*** YOUR CODE HERE ***"
class dictionarycipher(methods):
"""A class for dictionary cipher encryption method.
>>> cipher = dictionarycipher({'h': 'a', 'e': 'b', 'l': 'c', 'o': 'd', 'w': 'e', 'r': 'f', 'd': 'g'})
>>> cipher.encrypt('hello')
'abccd'
>>> cipher.encrypt('world')
'edfcg'
"""
def __init__(self, dictionary):
"""
Initialize the dictionary cipher with a dictionary.
If a key is not in the dictionary, the value is the key itself.
We promise that any letter will only appear once as a value in the dictionary.
"""
"*** YOUR CODE HERE ***"
def encrypt(self, message):
"""Encrypt the message using the dictionary cipher."""
"*** YOUR CODE HERE ***"
class fencecipher(methods):
"""A class for fence cipher encryption method.
>>> cipher = fencecipher(3)
>>> cipher.encrypt('hello')
'hleol'
>>> cipher.encrypt('world')
'wlodr'
"""
def __init__(self, rails):
"""Initialize the fence cipher with a number of rails."""
"*** YOUR CODE HERE ***"
def encrypt(self, message):
"""Encrypt the message using the fence cipher."""
"*** YOUR CODE HERE ***"
Problem 1.2: Encryption Extensions (100pts)
There are two subclasses that extend the encryption methods:
- Multiple Encryption: The constructor takes an integer
n
, and theencrypt
method applies the chosen encryption methodn
times on the input string. - Split Encryption: The constructor takes a number
x
and theencrypt
method, then extracts characters from positions that are multiples ofx
(excluding 0), concatenates them, and appends them to the original string before applying the encryption.
Both subclasses of the encryption extensions have a decorator
method that takes a encrypt
function and a string, applies the function to the string in the above manner, and returns the result.
class extensions:
"""The base class for encryption extensions."""
def decorator(self, function, message):
"""Apply the function to the message."""
pass
class multipleencryption(extensions):
"""A class for multiple encryption extension.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> extension.decorator(cipher.encrypt, 'hello')
'nkrru'
>>> extension.decorator(cipher.encrypt, 'world')
'cuxrj'
"""
def __init__(self, counts=1):
"""Initialize the multiple encryption extension with a number of times."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message multiple times."""
"*** YOUR CODE HERE ***"
class splitencryption(extensions):
"""A class for split encryption extension.
It extracts characters from positions that are multiples of `x` (excluding 0),
concatenates them, and appends them to the original string before applying the encryption method.
>>> cipher = shiftcipher(3)
>>> extension = splitencryption(2)
>>> extension.decorator(cipher.encrypt, 'hello')
'khoor'
>>> extension.decorator(cipher.encrypt, 'world')
'zroug'
"""
def __init__(self, x):
"""Initialize the split encryption extension with a number x."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message after splitting the message."""
"*** YOUR CODE HERE ***"
Now we can finally finish the encryption system by implementing the classes above. Congratulations!
class encryption:
"""A class for encryption.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> encrypt = encryption(cipher, extension)
>>> encrypt.encrypt('hello')
'nkrru'
>>> encrypt.encrypt('world')
'cuxrj'
"""
def __init__(self, method, extension):
"""Initialize the encryption with a method and an extension."""
"*** YOUR CODE HERE ***"
def encrypt(self, message):
"""Encrypt the message using the method and extension."""
"*** YOUR CODE HERE ***"
Problem 2: The Lambda-ing (350pts)
In the next part of this homework, we will be implementing a card game!
You can start the game by typing:
python cardgame.py
This game doesn't work yet. If we run this right now, the code will error, since we haven't implemented anything yet. When it's working, you can exit the game and return to the command line with Ctrl-C
or Ctrl-D
.
This game uses several different files.
- Code for all the questions in this homework can be found in
hw06.py
. - Some utility for the game can be found in
cardgame.py
, but you won't need to open or read this file. This file doesn't actually mutate any instances directly - instead, it calls methods of the different classes, maintaining a strict abstraction barrier. - If you want to modify your game later to add your own custom cards and decks, you can look in
cards.py
to see all the standard cards and the default deck; here, you can add more cards and change what decks you and your opponent use. The cards were not created with balance in mind, so feel free to modify the stats and add/remove cards as desired.
Rules of the Game This game is a little involved, though not nearly as much as its namesake. Here's how it goes:
There are two players. Each player has a hand of cards and a deck, and at the start of each round, each player draws a card from their deck. If a player's deck is empty when they try to draw, they will automatically lose the game. Cards have a name, an attack stat, and a defense stat. Each round, each player chooses one card to play from their own hands. The card with the higher power wins the round. Each played card's power value is calculated as follows:
(player card's attack) - (opponent card's defense) / 2
For example, let's say Player 1 plays a card with 2000 ATK/1000 DEF and Player 2 plays a card with 1500 ATK/3000 DEF. Their cards' powers are calculated as:
P1: 2000 - 3000/2 = 2000 - 1500 = 500
P2: 1500 - 1000/2 = 1500 - 500 = 1000
so Player 2 would win this round.
The first player to win 8 rounds wins the match!
However, there are a few effects we can add (in the optional questions section) to make this game a bit more interesting. Cards are split into Tutor, TA, and Professor types, and each type has a different effect when they're played. All effects are applied before power is calculated during that round:
- A Tutor will cause the opponent to discard and re-draw the first 3 cards in their hand.
- A TA will swap the opponent card's attack and defense.
- A Professor adds the opponent card's attack and defense to all cards in their deck and then remove all cards in the opponent's deck that share its attack or defense!
These are a lot of rules to remember, so refer back here if you need to review them, and let's start making the game!
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)
Problem 2.2: Making a Player (100pts)
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.
Call
deck.draw()
when implementingPlayer.__init__
andPlayer.draw
. Don't worry about how this function works - leave it all to the abstraction!
class Player:
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 ***"
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 ***"
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 ***"
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)))
Problem 2.3: Add effects (150pts)
The following code-writing question will be in hw06.py
.
For the following sections, do not overwrite any lines already provided in the code. Additionally, make sure to uncomment any calls to print
once you have implemented each method. These are used to display information to the user, and changing them may cause you to fail tests that you would otherwise pass.
Tutors: Flummox
To really make this card game interesting, our cards should have effects! We'll do this with the effect
function for cards, which takes in the opponent card, the current player, and the opponent player.
Implement the effect
method for Tutors, which causes the opponent to discard the first 3 cards in their hand and then draw 3 new cards. Assume there at least 3 cards in the opponent's hand and at least 3 cards in the opponent's deck.
Remember to uncomment the call to print
once you're done!
class TutorCard(Card):
cardtype = 'Tutor'
def effect(self, other_card, player, opponent):
"""
Discard the first 3 cards in the opponent's hand and have
them draw the same number of cards from their deck.
>>> from cards import *
>>> player1, player2 = Player(player_deck, 'p1'), Player(opponent_deck, 'p2')
>>> other_card = Card('other', 500, 500)
>>> tutor_test = TutorCard('Tutor', 500, 500)
>>> initial_deck_length = len(player2.deck.cards)
>>> tutor_test.effect(other_card, player1, player2)
p2 discarded and re-drew 3 cards!
>>> len(player2.hand)
5
>>> len(player2.deck.cards) == initial_deck_length - 3
True
"""
"*** YOUR CODE HERE ***"
# Uncomment the line below when you've finished implementing this method!
# print('{} discarded and re-drew 3 cards!'.format(opponent.name))
def copy(self):
"""
Create a copy of this card.
"""
return TutorCard(self.name, self.attack, self.defense)
TAs: Shift
Let's add an effect for TAs now! Implement the effect
method for TAs, which swaps the attack and defense of the opponent's card.
class TACard(Card):
cardtype = 'TA'
def effect(self, other_card, player, opponent):
"""
Swap the attack and defense of an opponent's card.
>>> from cards import *
>>> player1, player2 = Player(player_deck, 'p1'), Player(opponent_deck, 'p2')
>>> other_card = Card('other', 300, 600)
>>> ta_test = TACard('TA', 500, 500)
>>> ta_test.effect(other_card, player1, player2)
>>> other_card.attack
600
>>> other_card.defense
300
"""
"*** YOUR CODE HERE ***"
def copy(self):
"""
Create a copy of this card.
"""
return TACard(self.name, self.attack, self.defense)
The Professor Arrives
A new challenger has appeared! Implement the effect
method for the Professor, who adds the opponent card's attack and defense to all cards in the player's deck and then removes all cards in the opponent's deck that have the same attack or defense as the opponent's card.
Note: You might run into trouble when you mutate a list as you're iterating through it. Try iterating through a copy instead! You can use slicing to copy a list:
>>> lst = [1, 2, 3, 4] >>> copy = lst[:] >>> copy [1, 2, 3, 4] >>> copy is lst False
class ProfessorCard(Card):
cardtype = 'Professor'
def effect(self, other_card, player, opponent):
"""
Adds the attack and defense of the opponent's card to
all cards in the player's deck, then removes all cards
in the opponent's deck that share an attack or defense
stat with the opponent's card.
>>> test_card = Card('card', 300, 300)
>>> professor_test = ProfessorCard('Professor', 500, 500)
>>> opponent_card = test_card.copy()
>>> test_deck = Deck([test_card.copy() for _ in range(8)])
>>> player1, player2 = Player(test_deck.copy(), 'p1'), Player(test_deck.copy(), 'p2')
>>> professor_test.effect(opponent_card, player1, player2)
3 cards were discarded from p2's deck!
>>> [(card.attack, card.defense) for card in player1.deck.cards]
[(600, 600), (600, 600), (600, 600)]
>>> len(player2.deck.cards)
0
"""
orig_opponent_deck_length = len(opponent.deck.cards)
"*** YOUR CODE HERE ***"
discarded = orig_opponent_deck_length - len(opponent.deck.cards)
if discarded:
# Uncomment the line below when you've finished implementing this method!
# print('{} cards were discarded from {}\'s deck!'.format(discarded, opponent.name))
return
def copy(self):
return ProfessorCard(self.name, self.attack, self.defense)
After you complete this problem, we'll have a fully functional game of Magic: The Lambda-ing! This doesn't have to be the end, though - we encourage you to get creative with more card types, effects, and even adding more custom cards to your deck!
Congratulations! You've finished all problems of the homework. Feel free to run doctest to verify your answer again.
Just for fun Problems
This section is out of scope for our course, so the problems below is optional. That is, the problems in this section don't count for your final score and don't have any deadline. Do it at any time if you want an extra challenge or some practice with OOP and inheritance!
At this time, we don't provide Online Judgement. The local doctest is enough for you to check your answer.
Problem 3: Decryption (0pts)
In fact, we can construct a decryption system using exactly the same method as the encryption system. Isn't that cool? Give it a try!
To test your code, you just need to randomly generate some test data using your already written encryption system, then test it in the decryption system in reverse. If you get the original message, then your decryption system works perfectly!
class dmethods:
"""The base class for decryption methods."""
def decrypt(self, message):
"""Decrypt the message."""
pass
class shiftdecipher(dmethods):
"""A class for shift cipher decryption method.
>>> cipher = shiftdecipher(3)
>>> cipher.decrypt('khoor')
'hello'
>>> cipher.decrypt('zruog')
'world'
"""
def __init__(self, shift):
"""
Initialize the shift cipher with a shift.
If shift is greater than 26, shift = shift % 26.
"""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message by shifting each character by the shift."""
"*** YOUR CODE HERE ***"
class dictionarydecipher(dmethods):
"""A class for dictionary cipher decryption method.
>>> cipher = dictionarydecipher({'h': 'a', 'e': 'b', 'l': 'c', 'o': 'd', 'w': 'e', 'r': 'f', 'd': 'g'})
>>> cipher.decrypt('abccd')
'hello'
>>> cipher.decrypt('edfcg')
'world'
"""
def __init__(self, dictionary):
"""
Initialize the dictionary cipher with a dictionary.
If a key is not in the dictionary, the value is the key itself.
We promise that any letter will only appear once as a value in the dictionary.
"""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the dictionary cipher."""
"*** YOUR CODE HERE ***"
class fencedecipher(dmethods):
"""A class for fence cipher decryption method.
>>> cipher = fencedecipher(3)
>>> cipher.decrypt('hleol')
'hello'
>>> cipher.decrypt('wlodr')
'world'
"""
def __init__(self, rails):
"""Initialize the fence cipher with a number of rails."""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the fence cipher."""
"*** YOUR CODE HERE ***"
class extensions:
"""The base class for encryption extensions."""
def decorator(self, function, message):
"""Apply the function to the message."""
pass
class multipledecryption(dextensions):
"""A class for multiple decryption extension.
>>> cipher = shiftdecipher(3)
>>> extension = multipledecryption(2)
>>> extension.decorator(cipher.decrypt, 'nkrru')
'hello'
>>> extension.decorator(cipher.decrypt, 'cuxrj')
'world'
"""
def __init__(self, counts=1):
"""Initialize the multiple decryption extension with a number of times."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message multiple times."""
"*** YOUR CODE HERE ***"
class splitdecryption(dextensions):
"""A class for split decryption extension.
It extracts characters from positions that are multiples of `x` (excluding 0),
concatenates them, and appends them to the original string before applying the decryption method.
>>> cipher = shiftdecipher(3)
>>> extension = splitdecryption(2)
>>> extension.decorator(cipher.decrypt, 'khoor')
'hello'
>>> extension.decorator(cipher.decrypt, 'zroug')
'world'
"""
def __init__(self, x):
"""Initialize the split decryption extension with a number x."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message after splitting the message."""
"*** YOUR CODE HERE ***"
class decryption:
"""A class for decryption.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> encrypt = encryption(cipher, extension)
>>> decrypt = decryption(encrypt)
>>> decrypt.decrypt('nkrru')
'hello'
>>> decrypt.decrypt('cuxrj')
'world'
"""
def __init__(self, encryption_instance):
"""Initialize the decryption with an encryption method."""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the method and extension."""
"*** YOUR CODE HERE ***"
Problem 4: Next Fibonacci Object (0pts)
Implement the next
method of the Fib
class. For this class, the value
attribute is a Fibonacci number. The next
method returns a Fib
instance whose value
is the next Fibonacci number. The next
method should take only constant time.
You can assume we will neither create Fib
instance with non-zero value
directly nor edit the value
of Fib
instance outside the class.
Hint: Keep track of the previous number by setting a new instance attribute inside next. You can create new instance attributes for objects at any point, even outside the
__init__
method.
class Fib:
"""A Fibonacci number.
>>> start = Fib()
>>> start.value
0
>>> start.next().value
1
>>> start.next().next().value
1
>>> start.next().next().next().value
2
>>> start.next().next().next().next().value
3
>>> start.next().next().next().next().next().value
5
>>> start.next().next().next().next().next().next().value
8
>>> start.value # Ensure start isn't changed
0
"""
def __init__(self, value=0):
self.value = value
def next(self):
"*** YOUR CODE HERE ***"