Your First iOS & SwiftUI App: Polishing the App

Mar 1 2022 Swift 5.5, iOS 15, Xcode 13

Part 2: Swift Coding Challenges

17. Challenge: Bonus Points

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 16. The Xcode Debugger Next episode: 18. Challenge: Start Over

This video was last updated on Mar 1 2022

I have an idea for how to make Bull’s Eye a bit more fun.

What if we modified Bull’s Eye to give the player an additional 100 bonus points when they get a perfect score?

This would encourage players to really try their best to place the bull’s eye right on the target. Otherwise, there isn’t much difference between 100 points for a perfect score and 98 or 95 points if you’re close but not quite there.

While we’re at it, we can give the player 50 bonus points if they’re just one off.

Implementing this will be your next Swift coding challenge! Like last time, let’s write the test together,and then your job will be to pause the video, and wrote the code so that the test passes.

Add to BullseyeTests.swift:

func testScoreExact() {
  let guess = game.target
  let score = game.points(sliderValue: guess)
  XCTAssertEqual(score, 200)
}

func testScoreClose() {
  let guess = game.target + 2
  let score = game.points(sliderValue: guess)
  XCTAssertEqual(score, 98 + 50)
}

OK! Now pause the video, and modify your points(sliderValue:) method so that the unit tests all pass.

Don’t forget - since your points(sliderValue:) method will now have multiple lines, you should add the return keyword back into the final line.

That’s it - go ahead and pause the video and give it a shot, and stay tuned for the solution.

func points(sliderValue: Int) -> Int {
  let difference = abs(target - sliderValue)
  let bonus: Int
  if difference == 0 {
    bonus = 100
  } else if difference <= 2 {
    bonus = 50
  } else {
    bonus = 0
  }
  return 100 - difference + bonus
}

Build & run