CSCI 1100 — Computer Science 1 Homework 8
CS1 Multiverse: Classes
Overview
This homework is worth 100 points toward your overall homework grade, and is due Thursday,
April 25, 2019 at 11:59:59 pm. Please download hw08_files.zip. and unzip it into the directory
for your HW 8. You will find multiple data files to be used for tests.
The goal of this assignment is to work with classes. You will be asked to write a simulation engine
and use classes to encapsulate data and functionality. You will have a lot of design choices to make.
While we have done simulations before, this one will be more complex. It is especially important
that you start slowly, build a program that works for simple cases, test it and then add more
complexity. We will give lots of partial credit even if you do not get all the answers right. We will
provide test cases of increasing difficulty. Make sure you develop slowly and test thoroughly.
Submission Instructions
In this homework, for the first time, you will be submitting multiple files to Submitty that together
comprise a single program.
Please follow these instructions carefully.
You must submit three files. A file called Person.py that contains your Person class, a file called
Universe.py that contains your Universe class and a file called hw8.py that contains your main
program.
As always, make sure you follow the program structure guidelines. You will be graded on program
correctness as well as good program structure.
Remember as well that we will be continuing to test homework assignments for similarity. So,
follow our guidelines for the acceptable levels of collaboration. You can download the guidelines
from the Course Materials section of Submitty if you need a refresher. Note that guidelines also
forbid using someone else’s code from a previous semester. Make sure the code you submit is truly
your own.
Enter the multiverse: Universes
Many TV shows and movies make use of the theory of a multiverse. According to this theory many
universes very similar to ours exist at the same time. In fact, different versions of us may exist
in these universes as well. These alternate versions are very similar to us but may differ in very
fundamental ways. For example see regular and evil Spock from the original Star Trek (left), the
Council of Ricks from Rick and Morty (middle) and the recent Council of Wells from Flash (right).
Other examples include evil Willow and Xander. We can go on and on, but you get the point.
Same individual but with a few fundamental differences that arise because they are from a different
universe.
In this homework, you will have multiple universes each identified by their name. Assume each
universe has the same dimensions, a rectangle between coordinates (0,0) at the upper left corner
and (1000,1000) at the lower right corner. Each universe has some attributes:
The name of the universe given by a string.
A list of rewards in that universe.
Each reward has the following information: x, y, points, and name,
where x,y is the location the reward is located in this universe, it has the point value (points)
and the (name) which describes the reward.
A list of portals, each can transport you to a different universe.
Each portal has the following information: from_x, from_y, to_name, to_x, to_y
which means that the portal is at location from_x,from_y in the current universe and it
transports you to location to_x,to_y in universe with name to_name.
In the early tests for the homework, we will assume that there is a single universe with rewards,
but no portals. As the plot thickens, we will add the portals and other universes.
You must implement a universe class to hold the above information and store it in a file called
Universe.py. At a minimum, your Universe class must have a constructor (__init__) function
and a string representation (__str__) function.
As you implement the main program, you may find other useful methods for this class that will
simplify your program.
Here is an example universe from a test case:
Universe: EasyCS1 (4 rewards and 0 portals)
Rewards:
at (40,60) for 10 points: instant set knowledge
at (100,200) for 40 points: bonus 5 points on one homework
at (200,400) for 30 points: instant knowledge of list comprehension
at (600,800) for 50 points: good variable name generation ability
Portals:
None
Multiverse: Individuals
In your simulation, you will track individuals moving along the universe. Each individual will have
the following attributes:
name, radius, home_universe, x, y, dx, dy, current_universe, rewards
Each individual is from a specific universe (though we only use this to print where they are from).
They are represented as a circle with a given radius. Their current location is given by x, y and
the current_universe they are on. Individuals have a speed given by their movement along x and
y axis, stored in (dx, dy). Eventually individuals may stop or slow down, we will see how later.
You will be given an initial location and speed for each individual. They will all start moving in
their current universe, but may move to other universes through portals. They may also pick up
rewards over time, which you want to store. Initially, rewards will be empty. We will be interested
in the awards the individual picked up as well as their total point value.
Implement a person class to hold the necessary data for each person. Store this in a file called
Person.py.
As in universes, you may find that implementing some methods for each person class may signifi-
cantly simplify your main code.
Here are some example individuals from one of our test cases.
Scientist of EasyCS1 in universe EasyCS1
at (20,30) speed (20,30) with 0 rewards and 0 points
Engineer of EasyCS1 in universe EasyCS1
at (600,800) speed (-40,-10) with 0 rewards and 0 points
CS1 Multiverse: The Main Idea
Here is the main idea of the simulation: In this simulation, you have (potentially) many universes
and many individuals. Each individual is initially in their own universe at a specified location.
At each step of the simulation, each individual moves one time (i.e., x += dx, y += dy). Then,
we check a number of conditions. Each condition is checked in the same order the individuals are
given from the input file:
- If an individual passes near a location with treasure, she picks it up. As she carries more
items, her speed goes down under the weight. The speed change will impact either dx or dy
as they shift left to right.
If the magnitude (absolute value) of a person’s speed drops below 10 in either the x or y
directions, she stops moving. Stopped individuals will no longer move in later steps. - If an individual reaches the edge of the board, then she stops moving. Check for the center
of the individual being passed or at the border. - If two individuals hit each other while moving, they each drop the first reward they picked up
(if they have any). The reward returns to its original location. Note that dropping a reward
increases a person’s speed. After a collision, both individuals begin moving in the opposite
direction with their new speed. If an individual comes near the location of a portal, then she moves to a different universe
that this portal points to. In the next simulation step, she will continue her journey in that
new universe.
The simulation ends either at 100 steps or when there is no individual left moving. At the end of
the simulation, the individual with the largest amount of collected treasure wins the game.
Whenever we are testing whether an individual is close to a reward, we check if the distance between
the individual’s location (x1, y1) and the reward’s location (rx,ry) is less than or equal to the
individual’s radius (radius1): sqrt((x1 - rx)2 + (y1 - ry)2) <= radius1
To check whether two individual collide, we will look at the distance between their location
(x1,y1 and x2, y2) being less than or equal to the sum of their radius radius1 and radius2:
sqrt((x1 - x2)2 + (y1 - y2)2) <= radius1 + radius2
Note that, you will be given multiple test cases that only include steps (1) and (2) above. First
implement these and test them. Then we will include test cases with collisions but no portals.
Finally, we will have test cases with portals with or without collisions as universe expands.
For simplicity, we will give you all the relevant information about the program in a single JSON
file. The file contains a single list, each item in the list is a universe.
Each universe is a dictionary with keys: universe_name, rewards, portals, and individuals as
shown below:
Universe Dictionary Field Data Type
universe_name String
rewards List of tuples with 4 values: x, y, points, description
portals List of tuples with 5 values: fromx, fromy, to_universe, to_x, to_y
individuals List of tuples with 6 values: name, radius, x, y, dx, dy
Individuals are listed for a specific universe only. This is their home universe. When the simulation
starts, the individual is also located in this universe in the initial x, y coordinates.
The details of the simulation are given below. We recommend you implement slowly, reading each
step and implementing it first. Think where the implementation should fall? A member function
for universe or person classes, a function in your main program or simple code? Give yourself plenty
of time to make changes to your program as needed.
Happy implementation!
CS1 Multiverse: Detailed Problem Description
Create the class files Universe.py and Person.py containing class descriptions as described above.
Write a program stored in file hw8.py and import both classes into this file:
from Person import *
from Universe import *
Then, ask the user a single file name to read all universe and individual information:
Input file => file1.txt
file1.txt
You can read the whole info using a single line of code as before:
data = json.loads(open(fname).read())
Using the data provided, create and store people and universe information in your program. Print
out the main information for each universe and each individual first.All universes
Universe: EasyCS1 (4 rewards and 0 portals)
Rewards:
at (40,60) for 10 points: instant set knowledge
at (100,200) for 40 points: bonus 5 points on one homework
at (200,400) for 30 points: instant knowledge of list comprehension
at (600,800) for 50 points: good variable name generation ability
Portals:
NoneAll individuals
Scientist of EasyCS1 in universe EasyCS1
at (20,30) speed (20,30) with 0 rewards and 0 points
Engineer of EasyCS1 in universe EasyCS1
at (600,800) speed (-40,-10) with 0 rewards and 0 points
Note: There are 40 dashes in the underline and a 4 space indent when printing individuals.
Now, start simulation and repeat each step below until 100 steps are reached, or no individuals are
moving. At each step:- Increment the simulation counter.
- Move all individuals in the order they are given in the input file by adding their dx, dy to
their current location. - If an individual stops because their center is at or past the edge of the board, print a message.
Archie stopped at simulation step 33 at location (1005.0,340.0) - For each individual, check if they are able to reach a reward (i.e., the distance between their
current location and the location of a reward in their current universe is less than or equal to
the radius of the individual).
If so, individual picks up the reward and the reward is no longer available to anyone else.
Furthermore, the speed of the individual decreases according to the formula:
dx = dx - (n % 2) (n / 6) dx
dy = dy - ((n + 1) % 2) (n / 6) dy
where n is the current number of rewards the individual has.
Finally, print a message to show the reward that is picked and the individual’s current info.
Scientist picked up "good variable name generation ability" at simulation step 33
Scientist of EasyCS1 in universe EasyCS1
at (596.7,563.3) speed (8.3,13.3) with 3 rewards and 90 points
Remember that if the magnitude (absolute value) of a person’s speed drops below 10 in either
the x or y directions, she stops moving. - If the distance between two individuals is less than or equal to the sum of their radii, then
they crash. In this case:
Each individual drops the first reward in their list (if they have any rewards). The reward
goes back to its original location in the universe in which it originated.
The speed of the individual dropping a reward increases because their load is reversed and
reverses direction, given by:
dx = -(dx + (n % 2) (n / 6) dx)
dy = -(dy + ((n + 1) % 2) (n / 6) dy)
where n is the total number of current rewards for the individual after dropping the reward.
Print a message indicating the event.
Scientist and Archie crashed at simulation step 5 in universe IntersectionalCS1
Scientist dropped "instant set knowledge", reward returned to IntersectionalCS1 at
(80,80)
Scientist of IntersectionalCS1 in universe IntersectionalCS1
at (126.7,180.0) speed (-16.7,-30.0) with 0 rewards and 0 points
Archie of IntersectionalCS1 in universe IntersectionalCS1
at (120.0,225.0) speed (-20.0,-15.0) with 0 rewards and 0 points - Finally, if an individual is able to reach a portal (i.e., the distance between the individual’s
current location and the location of a portal is less than or equal to their radius), then the
individual passes through the portal and moves to the universe pointed by the portal. Print
an appropriate message.
Scientist passed through a portal at simulation step 9
Scientist of MediumCS1 in universe EvilCS1
at (200.0,200.0) speed (16.7,30.0) with 1 rewards and 10 points
When the simulation ends, print the step the simulation ended, the number of individuals still
moving at the end of the simulation, who is still moving, and the individual(s) with the highest
number of points and the rewards that they have.
Simulation stopped at step 48 - individuals still moving
Winners:
Scientist of MediumCS1 in universe EvilCS1
at (850.0,1000.0) speed (16.7,20.0) with 2 rewards and 40 points
Rewards:
instant set knowledge
ability to create black hole in Python
When you have fully tested your program, submit it as described above.
For this homework, we will be giving you both input files and the output created by them (instead
of posting in PDF) so that you can test your code. Happy hunting for rewards!
To match the output: any indentation is 4 spaces. The line of hyphens is 40 characters long. Note
that you must print your input file name as always.