gpt4 book ai didi

java - 使用数组来决定谁是活跃玩家(回合制猜谜游戏)

转载 作者:行者123 更新时间:2023-12-02 07:21:13 24 4
gpt4 key购买 nike

我正在使用一种方法(如下所示),该方法允许我输入玩家数量以及每个玩家的名称。有没有办法让我使用这个数组来决定谁是活跃玩家? (回合制猜谜游戏)。如果你能给我指出正确的方向就好了。

public class Program {
String[] playerList;
int playersAmount = 0;

public void inputPlayers() {
playersAmount = Input.readInt();
playerList= new String[playersAmount];
for (int g = 0; g < playersAmount; g++) {
String namePlayer = "player " + (g+1);
playerList [g] = namePlayer;
}
}
}

最佳答案

你应该仔细看看我关于更改玩家编号的问题。我认为这正是您正在寻找的(或类似的东西):Java: Changing Player Number

本质上,我使用了一个 boolean 数组来跟踪谁仍在玩,其中数组索引对应于玩家编号a[0] =玩家0,a[1] =玩家1等。如果玩家被淘汰标记相应的索引为 false: a[i] = false; 然后您可以使用以下方法(取 self 的问题)将玩家编号切换到下一个仍在玩的玩家:

public static int switchPlayer(int currentPlayer, boolean[] playerList) {
// if the current player + 1 = length (size) of array,
// start back at the beginning and find the first player still playing
if(currentPlayer + 1 == playerList.length) {
for(int i = 0; i < playerList.length; i++) {
if(playerList[i] == true) { // if player is still in the game
currentPlayer = i; // currentPlayer = current index of array
break;
}
}
}
// otherwise the current player number + 1 is not at the end of the array
// i.e. it is less than the length (size) of the array, so find the next player
// still playing
else {
for(int i = (currentPlayer+1); i < playerList.length; i++) {
if(playerList[i] == true) {
currentPlayer = i;
break;
}
}
}
return currentPlayer;
}

如果您对我的代码等有任何疑问,请告诉我。

关于java - 使用数组来决定谁是活跃玩家(回合制猜谜游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14189751/

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