0

I try to make a Mastermind in swift but I can't get each digit of the random number using an index to check if I found a number of the code. I tried this but it's not works.. :

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var textView: UITextView!

    var code = String(arc4random_uniform(9000) + 1000)

    override func viewDidLoad() {
        super.viewDidLoad()
        println(code)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }

    @IBAction func sendDidPressed(sender: AnyObject) {
        var find = 0

        for (var i = 0; i < 4; i++) {

            var number = textField.text
            var numberInCode = map(code) { String($0) }

            if contains(numberInCode, String(find)) {
                find++
            }

            self.textView.text = "\(self.textField.text): Bien placées: \(String(find))"
        }
    }

    @IBAction func resetCode(sender: AnyObject) {

    }
}

I need help

Rob Napier
  • 286,113
  • 34
  • 456
  • 610

2 Answers2

0

If you're trying to get each digit of the random number individually:

class ViewController: UIViewController {

    var code = [String]()
    let textFromField = "1" // Text from your textField

    override func viewDidLoad() {
        let number = String(arc4random_uniform(9000) + 1000)
        code = map(number) { String($0) }
    }

    @IBAction func sendDidPressed(sender: AnyObject) {
        for index in 0..<code.count {
            if textFromField == code[index] {
                println("Match found at index: \(index + 1)")
            }
        }
    }
}

code is now an Array with the numbers separated as Strings

You should change the way you convert the random number to Int because according to this post:

Int(arc4random()) will crash 50% of the time it's executed on a 32-bit platform because a UInt32 won't fit in an Int32

EDIT: Simplified things

Community
  • 1
  • 1
Eendje
  • 8,815
  • 1
  • 29
  • 31
  • I'm not entirely sure what you want, but you should be able to get what you need with this example – Eendje May 14 '15 at 16:35
  • Can you explain me what you did ? – theabstractdev May 14 '15 at 16:46
  • First, you generate a random number, convert the number to `String` and store it in `code`. `map()` converts `code` to an array of type `String` and store it in `separated`. Now you have an array and can try to check if the array contains `find` using `contains()`. – Eendje May 14 '15 at 16:51
  • Now it will iterate through the array of "numbers" and check if the "number" of your textfield matches with any of them. – Eendje May 14 '15 at 17:32
0

Turn the random number into an array of characters,

var code = (arc4random_uniform() % 9000 + 1000)  //e.g. 1245
var codeAsArray = Array(String(code))    //e.g. [1, 2, 4, 5]

for (var i = 0; i < 4; i++) {
   // var number = textField.text

    println(codeAsArray[i]) //is a swift Character
}
Will M.
  • 1,864
  • 17
  • 28