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

标签 java arrays

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

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/

相关文章:

java - 如何使 JTable 排序不在事件线程上?

php - MySql PHP 选择 JSON 值

c++: new 表达式中的数组大小必须是常量

java - 启动 jnlp 文件时出错 : Could not create the Java Virtual Machine and A fatal exception has occurred. 程序将退出

java - 从 ruby​​ SDK 使用参数调用 Java 中的 AWS Lambda

java - 在 Eclipse 中获取所有包的 Bundle []

java - Hibernate一对多关系级联删除

arrays - 我可以创建一个从 Array 派生的类吗?

arrays - 将一个整数从一个数组转移到另一个数组会导致无效的数组大小

arrays - fortran,将可分配数组传递给具有右边界的子例程