CSCI 1100 — Computer Science 1 Homework 8
Dictionaries and Classes
Homework Overview
Accomplished Automaton
Much like the last homework, this homework is a single program,
but we have staged the testing to allow you to build up your solution
and to get substantial credit even if you have problems getting the
entire program to work. Also, as promised, this assignment will
give you the opportunity to earn a little extra credit. Overall, this
assignment can earn 130 points, but the “total” on the homework
is only 110 points. Therefore, you can earn up to 20 bonus points
toward your homework grade if you get full credit. For those of you
intending to move on to Data Structures, getting the final scenario
correct is quite important because it gives you more practice with
classes. The assignment is due Thursday April 27, 2017 at
11:59:59 pm. Please review the rules about excess collaboration
from HW 3 to make sure you are turning in a program that is your
own.
Please check your available late days on the homework
submission server before planning your time on this assignment. Even if you have
three days remaining, you can only use two on this assignment.
As always, good coding style is expected and low quality code will be penalized.
Description
This assignment comes to you courtesy of one of our programming mentors who thought a project
on Magic the Gathering would be interesting and who pointed me at the card database. We will
again use JSON as our input file format. If you execute the following code:
import json
f = open(‘SomeCards.json’, encoding=”utf8″)
line = f.readline()
cards = json.loads(line)
You will find that cards is a dictionary of cards that has card names as keys and dictionaries of
card attributes as the values. For example, “Guttersnipe” is the name of a card in the cards
dictionary and executing:
print(cards[‘Guttersnipe’])
would print:
{’colors’: [’Red’], ’rarity’: ’Uncommon’, ’flavor’: ’He wants to watch Ravnica burn, one soul at a
time.’, ’colorIdentity’: [’R’], ’number’: ’162’, ’type’: ’Creature Goblin Shaman’, ’manaCost’: ’2R’,
’types’: [’Creature’], ’toughness’: ’2’, ’imageName’: ’guttersnipe’, ’artist’: ’Steve Prescott’, ’cmc’:
3, ’subtypes’: [’Goblin’, ’Shaman’], ’multiverseid’: 416919, ’text’: ’Whenever you cast an instant or
sorcery spell, Guttersnipe deals 2 damage to each opponent.’, ’name’: ’Guttersnipe’, ’power’: ’2’,
’id’: ’5eea2b9d464613ddfcc8f7ae0ccd8117aa200d5b’, ’layout’: ’normal’}
Note that opening the file with encoding=”utf8” is critical for our larger test on
Submitty, so be sure you use the encoding parameter in your file open call. Your code
will not run on Submitty without it.
For this homework we will be exploring the cards in the database in the context of creating a
card deck. To get full credit, you will need to have at least one class Deck which is used to
manage the cards once they have been added to the deck. Minimally, Deck must have an initializer
(__init__()), a string function (__str__()), and a method for adding new cards to the deck that
I will allow you to design and name.
Program Execution
When your program starts, it should:
1. Ask for the name of a JSON file.
2. Read the file, and create a dictionary of cards
3. Go through all the cards in the dictionary, and for each card pull out all the keys used by that
card. We will call these keys attributes. Print out all the attributes in sorted order along with
the number of cards that have that attribute and the type of data that the attribute stores.
Do not duplicate attributes when you print them out. You can assume that if an attribute
holds a specific data type for a specific card, that it always holds that data type. Use the
builtin type() function of python to get the data type. For example, if the dictionary is:
small[‘Spectral Grasp’] = {‘type’: ‘Enchantment Aura’, ‘cmc’: 2, ‘subtypes’:
[‘Aura’], ‘name’: ‘Spectral Grasp’, ‘colorIdentity’: [‘W’]}
small[‘Zealous Strike’] = {‘cmc’: 2, ‘type’: ‘Instant’, ‘name’: ‘Zealous Strike’,
‘manaCost’: ‘{1}{W}’}
Then your program should start by printing:
The card catalog has 2 different cards and 6 total attributes
cmc occurs in 2 cards and has type
colorIdentity occurs in 1 cards and has type
manaCost occurs in 1 cards and has type
name occurs in 2 cards and has type
subtypes occurs in 1 cards and has type
type occurs in 2 cards and has type
4. Now enter the main processing loop. At each iteration:
(a) Print out the current number of active cards. This is all the cards in the dictionary at
the start, but cards which are filtered out are removed from this group.
(b) Ask if the user wants to ”(A)dd, (F)ilter, (E)nd, (L)ist, (P)rint: ”
(c) Selecting ”A”, adds the active cards to the deck and resets the active cards to the
contents of the original dictionary. The cards to be added should be sorted, but once
cards are in the deck they should remain in the order they were added.
(d) Selecting ”F” brings up a secondary menu:
1: manaCost
2: name
3: power
4: text
5: toughness
6: type
Select an attribute to filter on (1-6) or 0 to return to main menu:
Followed by a secondary prompt for a filter string:
Enter filter string:
Note that all of the filter attributes have type
that does not contain the filter string in the selected attribute is no longer active.
(e) Selecting ”E” ends the program
(f) Selecting ”L” lists the active cards in sorted order
(g) Selecting ”P” prints the cards in the deck in the order they were added into the deck.
It should use the deck __str__() method.
Note:
i. The method __str__ () for a deck should start with an empty string,
ii. Then for each card add:
“CARD_NAME (CARD_TYPE): mana cost MANA_COST, power POWER, toughness:
TOUGHNESS\nTEXT”
where the capitalized fields are the specific values for the card.
iii. Put “\n\n” between two consecutive cards.
iv. Example: If your deck has the three cards: Dragonlair Spider, Hamletback Goliath
and Sphinx of Magosi, then the str function would return:
“Dragonlair Spider (Creature Spider): mana cost {2}{R}{R}{G}{G}, power
5, toughness 6\nReach\nWhenever an opponent casts a spell, create a
1/1 green Insect creature token.\n\nHamletback Goliath (Creature
Giant Warrior): mana cost {6}{R}, power 6, toughness 6\nWhenever
another creature enters the battlefield, you may put X +1/+1 counters
on Hamletback Goliath, where X is that creature’s power.\n\nSphinx of
Magosi (Creature Sphinx): mana cost {3}{U}{U}{U}, power 6, toughness
6\nFlying\n{2}{U}: Draw a card, then put a +1/+1 counter on Sphinx of
Magosi.”
5. Control then returns to the start of the main processing loop.
Be sure that you handle bad input and mismatched case. Look at the files scenario1.txt,
scenario2.txt, and scenario3.txt in hw8files.zip for the output from some example runs.
Grading
Autograding via Submitty will account for 100 of the 130 total points available on this homework.
TA grading of code organization, style, comments, and use of the deck class will account for the
remaining 30 points. Do not have all of your functionality in the main program. If you do, you
will lose organizational and style points. Instead use functions to simplify and organize your code.
We will execute test scenarios that allow you to get substantial credit even if you do not get the
program entirely working as follows:
1. (30/100) We will run the program and immediately exit via the ”E” option without filtering
the cards or adding cards to the deck.
2. (30/100) We will run the program and test out the filtering and list operations, but will not
add any cards to the deck, or print out the contents of the deck
3. (40/100) We will fully test the program using a mixture of filtering, adding, listing and
printing operations along with bad input to ensure that the program fully works. Getting
full points on this scenario will require that you have a Deck class.
Output from examples of these scenarios can be found in h8files.zip as scenario1_out.txt,
scenario2_out.txt, and scenario3_out.txt.
Submission Instructions
Please note that the card database and the sequence of operations we use on the submission server
are going to be different than the ones we have given you for your own testing.
Please follow these instructions carefully.
Submit two files. A file called Deck.py that contains your Deck class and a file called hw8.py
containing your main program.