gpt4 book ai didi

ios - 为多个玩家修改swift snake and ladder game

转载 作者:行者123 更新时间:2023-11-28 08:54:51 25 4
gpt4 key购买 nike

我正在尝试修改我为一个玩家在 Swift 中为一个简单的蛇梯游戏创建的代码。下面是一个成功运行的代码:

let finalSquare = 25
var playersLocation: Int = 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

//roll the dice
func rollDice ()->Int {
let value = Int(arc4random_uniform(6)+1)
return value
}

while won == false {
//roll the dice
var advanceplayer = rollDice()
//check the position
var advance: Int = playersLocation + advanceplayer
//check if it hits the magic number
if playersLocation < board.count{
var magicNumber: Int = board[playersLocation]
advance + magicNumber
}

//check if player won
if playersLocation >= board.count {
won == true
print("you won")
break
}else{
playersLocation = playersLocation + advanceplayer
}
}

现在代码可以运行了,我想修改为我有 3 个玩家(a、b、c)并且我想将他们插入游戏中。我几乎被困在这里:

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 in players.values{


var advanceplayer = rollDice()
var advance: Int = player + advanceplayer


if advance < board.count{
var magicNumber: Int = board[advance]
advance + magicNumber
}

if advance >= board.count {
won == true
print("you won")
break
}else{

advance + advanceplayer

}
}
}

不知何故玩家位置没有增加,我不知道如何跟踪 a、b 或 c 是否赢得了比赛。哪里出错了?

最佳答案

当您更新每个玩家的位置时,您并不是将其重新分配给该玩家在 players map 中的位置。这将很难用您当前的代码来完成,因为您正在遍历 players map 的值并忽略它的键。相反,枚举键和值,更新每个玩家的位置,然后将新值粘贴回 players

关于ios - 为多个玩家修改swift snake and ladder game,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33320556/

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