Part 7: Rounds and Scores

Welcome, this is a thread for collaboration, questions, and feedback on Part 7 of the iOS Apprentice Email course.

“Plain English” Challenge Solution

I came up with something like this:

  • If the slider’s value is greater than the target value, then the difference is: slider value minus the target value.
  • However, if the target value is greater than the slider value, then the difference is: target value minus the slider value.
  • Otherwise, both values must be equal, and the difference is zero.

This will always lead to a difference that is a positive number, because you always subtract the smaller number from the larger one.

Do the math:

  • If the slider is at position 60 and the target value is 40, then onscreen the slider is to the right of the target value, and the difference is 60 - 40 = 20.

  • However, if the slider is at position 10 and the target is 30, then the slider is to the left of the target and has a smaller value. The difference here is 30 - 10 = also 20.

Downloads

Here’s a download for the project to use at the beginning of this email:

BullsEye-part6-final-v2.zip (29.2 KB)

Here’s a download for the project where it stands at the end of this email:

BullsEye-part7-final-v2.zip (29.1 KB)

The idea that hit me first was to use an absolute value so that you get the magnitude of the difference regardless of whether it is positive or negative:

  • find the difference between the slider value and target value
  • get the absolute value of the difference (this will always be positive or 0)
  • If the difference is 0, give the player maximum points for a perfect guess (say, 10 points)
  • if the difference is between 1 and 5, give the player reduced points (say, 8)
  • if the difference is between 6 and 10, give the player reduced points (say, 5)
  • Any greater difference gets 0 points

Using this method does not give the granularity to know whether the user overshot or undershot the random value (no plus/minus data), so any display in the alert would have to be more generic, “You were within 5 numbers of the target value, good job!” instead of “Your guess was 5 numbers less/more than the target value.”

I did google for a swift absolute value call. You can do it two ways, looks like. You can use the global function, abs(integer), or the Int object has a .magnitude method that should do the same thing. So, not sure it is the best solution but it is possible :slight_smile: