作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为一系列玩家创建一款蛇梯游戏。
//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//
似乎每次新一轮开始时,它都不会从之前的位置开始,并且总是从零开始。
最佳答案
您忘记在玩家掷骰子后更新他们的新位置。
//check the number after magicNumber added
print("after adding the magic number, the current position is \(advance)")
// update the player location
players[player] = advance
关于ios - 蛇与梯子玩家位置保持从零开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326306/
我是一名优秀的程序员,十分优秀!