0

I am writing a puzzle game for an IOS. In my code I need to fill an array with some random (and non-random numbers) that will represent the main data structure.

func Random(r :  Range<Int>) -> Int {
    return Int(arc4random_uniform(UInt32(r.endIndex  - r.startIndex))) + r.startIndex
} // function to generate random number

var arrWithColors = [Int]() // array that will hold the numbers

//function to that check if an array contains a number in a range already

func checkForNumberInArrayWithRange(r: Range <Int>, n: Int ) -> Bool {
    for i in r.startIndex..<r.endIndex {
        if arrWithColors[i] == n { return true }
    }
    return false
}

// here is the main function where i keep getting stuck. Out of let's say five times it would only work 3 or even less. 


func randHexWithTag() {
    var rNumber : Int = Random(0...5)
        for i in 0...5 {
            while (true) {
                rNumber = Random(0...5)
                if !checkForNumberInArrayWithRange(0...5, n: rNumber) { 
                    break
                }
            }
            arrWithColors[i] = rNumber
        }

        arrWithColors[10] = arrWithColors[1]

        for i in 6...11 {   
            while(true) {
                rNumber = Random(0...5)
                if ((rNumber == arrWithColors[0] && i == 9) || (rNumber == arrWithColors[2] && i == 11)) {
                    continue
                }

                if !checkForNumberInArrayWithRange(6...11, n: rNumber) {
                    break
                }
            }

            if (i != 10) {
                arrWithColors[i] = rNumber
            }

        }

        arrWithColors[13] = arrWithColors[4]

        for i in 12...17 {

            while(true) {
                rNumber = Random(0...5)

                if (rNumber == arrWithColors[3] && i == 12) || (rNumber == arrWithColors[5] && i == 14) {
                    continue
                }
                if !checkForNumberInArrayWithRange(12...17, n: rNumber) {
                    break
                } 
            }

            if (i != 13) {
                arrWithColors[i] = rNumber
            }

        }
}
JeanLuc
  • 4,783
  • 1
  • 33
  • 47
M. Levin
  • 163
  • 1
  • 11
  • Note that there is a far better (faster, simpler, safer) method to create a random permutation of integers: [How do I shuffle an array in Swift?](http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift). – Martin R Sep 07 '15 at 17:58
  • 2
    Concerning your problem: If the program "freezes", **use the debugger**. Pause execution, single-step through your code, inspect variables... – Martin R Sep 07 '15 at 18:01
  • thanks. But I do not need to shuffle the array – M. Levin Sep 07 '15 at 23:29

1 Answers1

0

The above code will ALWAYS fail during the first call to checkForNumberInArrayWithRange on this line:

if arrWithColors[i] == n { return true }

That is because arrWithColors is empty, and index i is out of range

Vladimir Kofman
  • 1,983
  • 21
  • 21