Skip to content

Instantly share code, notes, and snippets.

@gusennan
Last active June 27, 2016 21:39
Show Gist options
  • Save gusennan/4bb8babc659414d3e5f5a4432bc19694 to your computer and use it in GitHub Desktop.
Save gusennan/4bb8babc659414d3e5f5a4432bc19694 to your computer and use it in GitHub Desktop.
Readings:
The Swift Programming Language (Swift 2.2) (Links to an external site.) - https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11 (Links to an external site.)
Pages 1-271
You will want to download that to iBooks and eventually read the entire thing. The Swift Tour on Pages 3-33 you should know like the back of your hand. Everything up through properties you should be familiar with starting next week.
Optional reading (Playgrounds which follow the book):
https://github.com/nettlep/learn-swift (Links to an external site.)
Highly recommend exploring portions of the book which don't seem clear in the noted playgrounds. These implement all of the examples from the book so that you don't have to type them in yourself.
Assignment
Problem 1 (20 points)
Create a single view Project called Assignment2.
Rename the ViewController.swift file to be MainController.swift and rename the class inside of the file to be MainController, changing the class name of the view controller in Main.storyboard to MainController in the identity inspector
install a navigation controller in the storyboard as demonstrated last wednesday in class
In the Main.storyboard for LifeEngine on the view of the MainController place 3 buttons labeled Problem 2, Problem 3, Problem 4, respectively
Drag out 3 UIViewControllers onto the storyboard
Create 3 UIViewController subclasses named Problem2ViewController, Problem3ViewController, Problem4ViewController, respectively
In the viewDidLoad function for each view controller set the title for the navigation bar to be Problem 2, 3, 4 respectively
Assign the ViewController subclasses to be the ViewController for its respective InterfaceBuilder representation in Identity Inspector
Give Problem 2's view a green background with alpha of 0.1, Problem 3's a yellow background with an alpha of 0.1 and Problem 4's a blue background with an alpha of 0.1
Connect each button on the main page to its respective view controller via a Show segue
Verify that navigation to each scene and back works correctly
On each scene place a UIButton labelled "Run" and a UITextView for the output of the run
On each ViewController create an IBOutlet for the TextView and an IBAction for the button
Verify that hitting the button causes the action to fire by putting some text in the TextView
Make sure that layout constraints are applied to the (any, any) size class that will allow the app to run on any device.
Problem 2 (50 Points)
In the action method for the button in the Problem2ViewController implement the rules of Conway’s Game of Life via a for-in loop with a switch statement in the for-loop’s body based on the number of neighbors of the cell that are alive.
As a reminder the rules are:
1.Any live cell with fewer than two live neighbors dies or stays, as if caused by under-population.
2.Any live cell with two or three live neighbors lives on to the next generation.
3.Any live cell with more than three live neighbors dies, as if by overcrowding.
4.Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
For purposes of this assignment the array "wraps". i.e.; in a 10x10 two-dimensional array: the neighbors of cell (0,0) are (9,9), (9,0), (9,1) (0,9), (0,1), (1,9),(1,0), (1,1) and so on for all cells located at a boundary.
You will want to:
create a 2-dimensional array of Bool's called before to hold the alive dead state and initialize that to some random value with arc4random upon entry to the IBAction. Initially, 1/3rd of the cells should be alive, which can be done by using a statement like:
if arc4random_uniform(3) == 1 {
// set current cell to alive
}
create another 2-d array of bools called after to hold the next state of the game
Count and print the number of living cells in before to the UITextView
Loop over the before 2D array and count the living neighbors of each cell using a switch statement, observing the wrapping rules. Place the result of applying the rules above into the corresponding cell in the "after" 2D array.
Count and print the number of living cells in after to the UITextView
Problem 3 (15 points)
Extract the cell logic in Problem 2 to a top level function called step() which accepts a 2D array of bools as input and returns a 2D array of bools as output. Place that function in a separate swift file called Engine.swift. Repeat Problem 2 only invoking step() instead of embedding the logic in the IBAction
Problem 4 (15 points)
Extract the logic for computing neighbors of a cell according to the wrapping rules into a top-level function called neighbors() which accepts a tuple of row-column coordinates and returns an array of row-column tuples of coordinates where each member of the returned array is a different neighbor of the input coordinate. Repeat Problem 3 only invoking creating a function called step2() which invokes neighbors() instead of directly embedding that functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment