Skip to content

Instantly share code, notes, and snippets.

@reinder42
Last active September 24, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinder42/62030825423683d7151fbf59c7902dcb to your computer and use it in GitHub Desktop.
Save reinder42/62030825423683d7151fbf59c7902dcb to your computer and use it in GitHub Desktop.

Can you change the Add 1 game to include the number 9? Yes you can!

Original video (part 1) · Original tutorial

The game initially calculates whether a given input is OK by subtracting the input from the given numbers, and if the result is 1111, the input is correct. This is based on the idea that the difference between two sequences 0000-8888 is 1111 if you added 1 to each of the given numbers. This rules out the number 9 from the game, though!

Adding number 9 back into the game is fairly simple:

  1. First, we need to determine that, given we're working with single digits, 9 plus one = 0. So, the user needs to input 0 to add one to a 9.
  2. Then, we'll iterate over the digits of the input and the given number, and check that the difference between them is 1. This will go wrong when comparing 9 and 0, so we'll write an exception for that.
  3. And finally, the random number generator in the game needs to make random numbers from 0 to 9 inclusive.

Here's how. First, locate the score calculating code in textFieldDidChange(textField:). Replace the relevant bit of code with the code below. It's the stuff inside the first optional binding statement.

var isRight = true

for i in 0..<4
{
    var a:Int! = Int(String(input_text[input_text.index(input_text.startIndex, offsetBy: i)]))
    let b:Int! = Int(String(numbers_text[numbers_text.index(numbers_text.startIndex, offsetBy: i)]))
    
    if a == 0 {
        a = 10
    }
    
    print("input = \(a!), number = \(b!), result = \(a - b)")
    
    if a - b != 1 {
        isRight = false
    }
}

And make sure to use isRight in the conditional that processes the score, i.e:

if isRight
{
	print("Correct!")
	...

So what's happening here? We use a bit of Swift 4 magic to get to the integers in input_text and numbers_text, in a for loop. Using index(_:offsetBy:) you get the index of the character in the string, which is then used as a subscript on the string itself, which returns a Substring, which you convert to String, which you then convert to Int. I know, I know...

Then, when the input is 0 you change it into 10. This is a quick trick to make sure that 10 - 9 == 1.

Finally, you compare a and b and when the difference isn't 1 you set isRight to false. The isRight flag is true from the start, so when you haven't made a mistake it will remain true. When one of the inputs is wrong, it'll flip to false. And of course, in the conditional isRight is evaluated, and will show the appropriate thumbs up or down, increase or decrease the score, etc.

Last but not least, make sure that digit in generateRandomNumber() will also use the number 9. You can use the new random number generator API in Swift 4.2, like this:

let digit = Int.random(in: 0..<10)

And that's it!

@emrdgrmnci
Copy link

How we can do the square of every digits of the numbersTextfield? (For example : 9999 == 81818181 or 1234 == 14916) In the 9999 we need to make our inputfield's max character count 8 in the second example we need to make max count 5 etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment