mardi 3 décembre 2019

add numbers game . Swift

I have a game where I have to add 1 to four random numbers on the screen. Example. (3456 = 4567 = true) (3456 = 4556 = false) numbers only from 0 to 9 when I add 1 to each number, then there is a condition

  if customeInput == 0
            {
                customeInput = 10
            }

We also know that entering the number 10 is the correct answer for a given number 9, based on the principle of adding 1, which is the correct answer. Customeinput == digit + 1 In other words, when input is 0, we consciously switch input to 10 to get the right digit, because that's the right answer.

!10 = 9 + 1 0 9

Now I need to choose the right condition when we finish not 1 but 2 for example. 3490 = 5612 that is, when 9 and we finish 2, then it should be 9 + 2 = 1 9, 0, 1, etc.

this is an extension for random numbers in the Label

import Foundation

extension String
{

    static func randomNumber(length: Int) -> String
    {
        var result = ""

        for _ in 0..<length
        {

            let digit = Int.random(in: 0...9)
            result += "\(digit)"

        }
        return result

    }

    func integer(at n: Int)-> Int
    {
        let index = self.index(self.startIndex, offsetBy: n)
        return self[index].wholeNumberValue ?? 0
    }
}

This is the class of the implementation itself.

   import UIKit

    var segmentNumberLenght:Int!
    var segmentHowMutch:Int!

    class CustomeGameVC: UIViewController {

        @IBOutlet weak var customeScoreLabel: UILabel!
        @IBOutlet weak var customeTimeLabel: UILabel!
        @IBOutlet weak var numberLabel: UILabel!
        @IBOutlet weak var inputCustomeField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationController?.isNavigationBarHidden = true

        }

        var customeScore = 0
        var timer:Timer?
        var seconds = 500

        //input numb Field
        @IBAction func didChangField(_ sender: UITextField) {

            guard let customeNumberText = numberLabel?.text,
                let customeInputText = inputCustomeField?.text
                else{return}

            if customeInputText.count == segmentNumberLenght
            {}else {return}

            var isCustomeCorrect = true

            for n in 0..<segmentNumberLenght
            {
                var customeInput = customeInputText.integer(at: n)
                let customeNumber = customeNumberText.integer(at: n)

      //if 9 next 0 
      //if 0 next 1
    if customeInput == 0
                {
                    customeInput = 10
                }

                if customeInput != customeNumber + segmentHowMutch
                {
                    isCustomeCorrect = false
                    break
                }  
            }

            if isCustomeCorrect
            {
                customeScore += 1
            }

          }

        }

Aucun commentaire:

Enregistrer un commentaire