There are 3 questions without parts and one question with 3 parts. And of crouse there is one bonus question involved with database a little bit (SQL).
Question 1: Ceasar Cypher (6 points)
Back in the days of the Roman Empire, it was not uncommon for military communications to be intercepted, since they were largely carried around by soldiers on horses. As a result, the Romans developed a way of encoding their military communications, so that they could only be read by other Romans who knew how the message had been encoded.
The code was this: Take every letter of the latin alphabet in the message, and replace it with the letter that was three letters after it in the alphabet. So, A became D, B became E, et cetera. This is known as the Ceasar Cypher, and it is one of the earliest forays humanity ever made into the fascinating science of cryptography.
Write a python function, ceasarian(x), which, given a string x, encodes all characters in the classical latin alphabet (given below), using the Ceasar Cypher described above.
The classical Latin alphabet:
A B C D E F G H I K L M N O P Q R S T V X Y Z
- You do not need to encode characters that do not appear in the classical Latin alphabet, just leave them undeciphered. This includes but is not limited to:
- spaces
- punctuation
- Alphabetic characters not appearing above, such as the modern letter
J, which didn’t exist at the time.
- You may assume that Romans shouted everything (i.e., all communications are ENTIRELY IN UPPERCASE)
In addition, give the function written above a default argument, decypher, with a default value of False, which, if True, performs the reverse operation (or decyphering) of encoded text.
Question 2: Just the Questions, Ma’am! (10 points)
Write a python function extractQs(filename, sep) which, given the name of a jupyter notebook filename, constructs a new, syntactically correct jupyter notebook which contains only the contents of all markdown cells in the original notebook, in a single markdown cell. The input sep should have a default value of "n", and will have functionality described below.
Jupyter notebooks are encoded as json files. If you wish to view the structure of a notebook, select the file in the jupter file browser and click the edit button in the toolbar.
You will only need to modify the cells field of the top-level dictionary. The cells field contains a list of the cells in a notebook. The rest of the metadata should copied over to the new notebook unchanged. A markdown cell (which is a cell containing formatted text, like the one you are currently reading) may be distinguished by the value of the cell_type field, which will be equal to the string "markdown". A markdown cell will have two other fields, metadata and source. The first of these is an empty dictionary, and the second is a list of strings, where each string is one line of text in the markdown cell.
When creating the new cell containing all the markdown text in the specified file, you must maintain the structure of the original, and give it a list of strings, where each string is a line in the file. Do not change the order in which the markdown text occurs (the first cell should come first, the second comes second, etc.) You must, however, place a separator between the contents of each markdown cell. The string to be used as a separator is specified by the sep input to the function (as seen above). If sep has been set to False, do not add a separator.
Your new notebook should be written as a file with the name Qs_4_<original file name>, where <original file name> is the string provided as an argument to the function
If opening the specified filename fails for any reason, we will assume that the user of the function forgot the file extension. Append .ipynb to the filename and try again. You will also need to append this to the name of the output file if it turns out that the file extension was forgotten. If this fails, create a MalformedQuery exception, without creating the above specified output file.
You must also provide a docstring for this function which describes it’s functionality. This must contain a description of the algorithm, as well as what the algorithm takes as input and produces as output.
Question 3: Calendar Alerts (6 points)
Write a python function alert(calendar) which, given a calendar dictionary mapping date objects to strings (alerts for the user), outputs the alert text of the nearest alert that will occur within 2 hours of the current time.
Inverse Price is Right rules! We want the nearest alert time without going under the current time. If there are no alerts within the specified time frame, output the string “No alerts presently!”
Question 4 : Nick Wars
Stick Wars was a popular series of browser games (remember when those were a thing?!) that have recently found a new audience on mobile platforms. You will be coding a stripped-down version of stick wars as a series of classes in python. (This game is very similar to The Battle Cats, incidentally).
DESCRIPTION OF THE GAME
The student is encouraged to try the game in order to get a feel for it (http://www.stickpage.com/stickwargameplay.shtml), however, please understand that the link is provided on a “do so at your own risk” basis.
In our version of stick wars (henceforth known as “Nick Wars”), two armies fight each other across a field. Each player has a pool of gold that gradually increases at a fixed rate. Each player may either purchase units to attack the other player, or increase their rate of gold production. The object is to destroy your opponent’s base, and in order to do so, one must destroy the opponent’s army.
In contrast to full version of the game available at the link above, the following simplifications will be made:
- We will not keep track of the position of units in the field. This saves us from complicating their AI with movement behaviour, and saves us from having to make attack range calculations.
- As a result of not tracking unit positions, we also will not worry about being able to instruct units to advance, retreat, or hold position.
- We will not have upgrades, or worry about multiple stages.
- We will not be coding an enemy AI for the player to fight against.
When in doubt, please refer to the descriptions provided. If there are any conflicts between the operation of the original game and the description provided, the description takes precedence.
QUESTION 4A – THE UNIT CLASS (8 POINTS)
“Units” are the soldiers that fight in our fictitious war. Units will be defined by a class in python called Unit. For Nick Wars, our units will have the following properties:
Team– A string indicating which team the unit belongs to. This will always either be “Red” or “Blue”. Must be initialized.HP– hitpoints / health / energy / pep. The amount of damage a unit can sustain before being killed. Units can not regain or recover HP. Must be initialized.ATT– attack / strength. The amount of damage a unit can do in a single hit. Must be initialized.attack_timeout– The amount of time that must pass between attacks, measured intick‘s (see methods below). Must be initialized.isDead– True if this unit is dead, False if it is living. Initializes to False.
Methods:
tick(self, opponents)– each timetickis called, we simulate one unit of time passing inside the game scenario. Any “automatic” behaviours of a unit are defined here (so, we are in essence programming the unit AI). The argumentopponentsis a list of all units which are valid attack targets. The following behaviours must be created ifisDeadis False. A dead unit performs none of these behaviours and immediately returns the integer value 0.- If the list of opponents has any living units, this unit attacks the unit with the lowest HP (see the attack method). In this case, this method returns the integer value 0. If multiple units have the same HP, go with the first unit with that HP in the list of opposing units.
- If the list of opponents is empty or contains no living units, return this unit’s attack value. This will be used later to calculate damage to the enemy’s base.
- The above two actions can only occur if it has been the specified number of ticks since the last attack by this unit. How to keep try of this is left as an exercise to the reader.
attack(self, other)– Called when this unit (self) attacks a defending unit (other). The defending unit has it’s HP reduced by this unit’s attack value. If this reduces the defending unit’s HP to or past zero, setisDeadto True.
NOTE: A unit is able to attack (with respect to the timeout) immediately upon creation.
QUESTION 4B – THE ARMY CLASS (10 POINTS)
Since a lot of code will be reused between the two opposing armies, it makes sense to encapsulate them as their own class.
Define an Army class with the following properties and methods:
Properties:
- Team – A string indicating which team the unit belongs to. This will always either be “Red” or “Blue”. Must be initialized.
- BaseHP – The amount of health the army’s base has remaining. Initializes to 1000
- GoldRate – The number of gold the army gains per tick. Initializes to 10.
- Gold – The amount of gold the team has for buying units and increasing
GoldRate. Initializes to 0. - Roster – A list of dictionaries specifying the properties of the units available for purchase. Must be initialized.
- Price – The amount of gold the unit costs
- HP – amount of health the unit starts with
- ATT – attack power of the unit
- attack_timeout – number of time the unit must wait between attacks.
- Units – A list of Unit objects which currently exist.
Methods:
- tick(self,opponents) – Aside from self, takes a list of the other team’s units
opponents. Invokes thetickmethods of every unit in the list of Units, and passes each the list of opponents. The return values of each Unit’s tick method indicate the amount of damage done to the opponent’s base. Compute the sum of these return values and return that sum from this tick method. In addition, the team’sGoldvalue is increased by the value ofGoldRate. - BuryDead(self) – Removes from the list of Units all Units for which
isDeadis True. - BuyGoldRate(self) – If the team has more than 250 gold, reduces the team’s gold by 250 and adds 2 to GoldRate.
- BuyUnit(self, x) – x is an integer indicating the index of the unit in the roster is to be purchased. If the team has an amount of gold greater than or equal to the
Pricevalue of the unit specified by x, a newUnitis initialized with the properties specified in the roster entry, and is added to theUnitslist. If the specified roster item doesn’t exist, raise aNoSuchUnitErrorexception. If there is not enough gold to purchase the unit, create aTooPoorErrorexception.
QUESTION 4C – THE NICKWARS CLASS (6 POINTS)
Now we can pull everything together, and define a NickWars class with the following properties and Methods.
Properties:
- Red – An “Army” object, with Team initialized to “Red”. Roster must be initialized.
- Blue – An “Army” object, with Team initialized to “Blue”. Roster must be initialized.
Methods:
- tick(self) – Invokes the tick method for both Red and Blue, supplying each the other’s list of units. The return value of Red’s tick method must be subtracted from the BaseHP of Blue, and vice versa. Every 10 ticks, both Army’s BuryDead methods should be invoked.
- victoryCheck (self) – Returns the winner of the game. If Blue’s BaseHP is zero or less, return “Red Wins!”. If Red’s BaseHP is zero or less, return “Blue Wins!”. If both army’s base hp is zero or less, return “Tie Game!”, and if neither’s base HP is zero or less, return “Still Playing!”
NOTE: Although they didn’t in WWII, the Red Army always moves first in Nick Wars.
BONUS QUESTION: Hackers don’t die, they respawn AKA are you a bad enough dude to save the president? (6 points)
The President of the Country has been captured by aliens! It’s up to you, Hacker McHackerson, part of the government’s elite anti-alien hacking unit, to determine the location of the President so that the military can send in the troops and save the President!
Fortunately, the aliens never took COMPSCI 1MD3, and have left their database of kidnapping victims vulnerable to SQL injection attacks! The source code for a python function the aliens wrote to query their database has been delivered to HQ. Many Bothans died to bring us this information.
You must create SQL code injections that produce the following
- Display the entire
prisonerstable - Display just the lattitude and longitude of the President (HINT: The President’s name is
"The President") - Design an attack query that destroys the alien’s prisoners table!


0 comments