Your First iOS & SwiftUI App: Polishing the App

Mar 1 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Swift Coding Challenges

18. Challenge: Start Over

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 17. Challenge: Bonus Points Next episode: 19. Draw the Rings
Transcript: 18. Challenge: Start Over

For the next item on our “nice to haves” on our programming to-do list, we want to add the ability to restart the game.

To do this, we’re going to need a new method on our Game object that we can call to restart the game. Let’s use Test-Driven Development to start by writing a test for this first.

Add to BullsEyeTests.swift:

func testRestart() {
  game.startNewRound(points: 100)
  XCTAssertNotEqual(game.score, 0)
  XCTAssertNotEqual(game.round, 1)
  game.restart()
  XCTAssertEqual(game.score, 0)
  XCTAssertEqual(game.round, 1)
}

And now it’s time for your next Swift Coding challenge!

Your challenge is to pause the video, and implement this method so the tests pass.

Then, modify Bull’s Eye so that it calls this method when the player taps the start over button. Do you remember where you can find that button in your app?

Give it a shot, and if you get stuck, no worries - you can watch the solution and get a good refresher about an area you might be confused about. Good luck!

Add to Game.swift:

mutating func restart() {
  score = 0
  round = 1
  target = Int.random(in: 1...100)
}

Cmd+U to run tests.

Open BackgroundView.swift, and add to TopView’s counterclockwise button:

game.restart()

Build and run to test.