我正在尝试为一系列玩家创建一个蛇和梯子游戏。
//board set up
let finalSquare = 25
var playersLocation: Int = 0
var players: [String: Int] = ["a": 0, "b": 0, "c":0]
var won = false
var board = [Int](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
board
//roll the dice
func rollDice ()->Int {
let value = Int(arc4random_uniform(6)+1)
return value
}
var count: Int = 0
while(won == false){
for (player, location ) in players{
//this is the player first location
print("player \(player) is now at \(location)")
// roll the dice and move the player
var advance = location + rollDice()
print("now I roll the dice and player \(player) move \(advance) step ")
//check if the index is still on the board, and if it hits the magic number
if advance < board.count {
//create the magic number and plus with the advance
var magicNumber: Int = board[advance]
print("the magic number is \(magicNumber)")
//adding the magicNumber to advance
advance = advance + magicNumber
//check the number after magicNumber added
print("after adding the magic number, the current position is \(advance)")
}
//check if the player exceeds the board number
if advance >= board.count {
var won = true
print("player \(player) won")
break
}else{
advance = advance + rollDice()
print("not win yet, after I add the number from the roll dice it advance to \(advance)")
won = false
}
}
}
这是调试文本的片段:
//player b is now at 0//
//now I roll the dice and player b move 5 step //
//the magic number is 0//
//after adding the magic number, the current position is 5//
//not win yet, after I add the number from the roll dice it advance to 6//
然后在所有玩家完成后,它返回到玩家b:
//player b is now at 0//
//now I roll the dice and player b move 6 step//
//the magic number is 11//
//after adding the magic number, the current position is 17//
//not win yet, after I add the number from the roll dice it advance to 22//
似乎每次新一轮开始时,它都不会从之前的位置开始,总是从零开始。
答案 0 :(得分:0)
你忘了在掷骰子后更新玩家的新位置。
//check the number after magicNumber added
print("after adding the magic number, the current position is \(advance)")
// update the player location
players[player] = advance