gpt4 book ai didi

ios - 蛇与梯子玩家位置保持从零开始

转载 作者:行者123 更新时间:2023-11-30 13:58:15 25 4
gpt4 key购买 nike

我正在尝试为一系列玩家创建一款蛇梯游戏。

//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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com