I am relatively new to Swift and am making a game. I have four integers titled "pos1," "pos2," "pos3," and "pos4." They each have to contain a number from 1 - 4, but each number has to contain a different number (ex. if pos1 = 3, none of the other integers can equal 3). I made a function that does this, but it is not very efficient. Is there a better way of doing this?
My code:
func buildWallType1() {
var stay = 1
var pos1 = Int(arc4random_uniform(4) + 1)
var pos2 = Int(arc4random_uniform(4) + 1)
var pos3 = Int(arc4random_uniform(4) + 1)
var pos4 = Int(arc4random_uniform(4) + 1)
while stay == 1 {
if pos2 == pos1 || pos2 == pos3 || pos2 == pos4 {
pos2 = Int(arc4random_uniform(4) + 1)
}
else if pos2 != pos1 && pos2 != pos3 && pos2 != pos4 {
stay = 2
}
}
while stay == 2 {
if pos3 == pos1 || pos3 == pos2 || pos3 == pos4 {
pos2 = Int(arc4random_uniform(4) + 1)
}
else if pos3 != pos1 && pos3 != pos2 && pos3 != pos4 {
stay = 3
}
}
while stay == 3 {
if pos4 == pos1 || pos4 == pos2 || pos4 == pos3 {
pos2 = Int(arc4random_uniform(4) + 1)
}
else if pos4 != pos1 && pos4 != pos3 && pos4 != pos2 {
stay = 4
}
}
}
Aucun commentaire:
Enregistrer un commentaire