Project Description
For this assignment you will create a Command-Line racing game using Swift. This will provide you further experience in Swift as well as introduce object-oriented concepts that will be critical for future app development.
Basic Requirements
To create this racing game, you will:
- Create a class called Automobile and define the following private properties: make, model, and speed, and static property currentTrack.
- Create an array racetracks of race track names. You can name them whatever you'd like, but there should be at least 3.
- Define methods:
- init takes arguments for each property. Initializes all properties to their passed-in value.
- pickTrack is a class-level method which randomly selects a race track from racetracks and assigns it to currentTrack.
- get and set instance methods for each private property.
- increaseSpeed is an instance method that increases the vehicle's speed by a random number between 3 and 12 (mph).
- description is an instance method which returns a string "Make: [make], Model: [model], Speed: [speed]"
- In main.swift, create 3 Automobile objects of the following make and model respectively: Honda Accord, Toyota Corolla, MINI Cooper. Their speeds will be initialized to 0.
- Select a track using pickTrack. At the start of the race print "Race on [Racetrack] is beginning."
- Call increaseSpeed on each vehicle for ten "rounds" (imagine this is a race to see who can go farthest in 10 seconds).
- During each round, print out "[Make] [Model] is in the lead." based on which vehicle has currently covered the most distance. If there is a tie for first place, print "Two or more cars are in the lead." To do this calculation, assume each round is 1 second and that changes in speed are instantaneous between rounds. Note that you may want to create additional helper methods and/or structures to help you calculate and/or track car position based on speed. It is not sufficient to check the final speed to determine the race's winner -- your answer will not necessarily be correct based on distance traveled.
- During each round, also call description for each car, so we can see each car's current speed.
- After the race finishes, call each vehicle's description method followed by a line declaring the winner: "[Make] [Model] won the race on [Racetrack]!" or in the case of a tie, "There was a tie!".
Getting Started
To create a command-line application:
- Open Xcode and got to File -> New -> Project
- Select OSX Command Line Tool project (shown below)
- Set Product Name to yourid_assignment2 and Organization Identifier to cs329e. Your Team should automatically be set to your Developer ID, and the language should be set to Swift by default.
- main.swift is created automatically, but you will need to create a separate file for the Automobile class.
What to turn in
You will submit a zip of the project folder, yourid_assignment2, that contains the files main.swift, automobile.swift and yourid_assignment2.xcodeproj via Canvas.