Jente
Jente

Reputation: 63

Use array to decide who is active player (turn based guessing game)

I am using a method (as shown below) that allows me to input the amount of players along with a name for each player. Is there a way for me to use this array to decide who is the active player? (turn based guessing game). If you could just point me in the right direction.

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;
          }
      }
 }

Upvotes: 0

Views: 2360

Answers (5)

lord_sneed
lord_sneed

Reputation: 824

You should look over the question I had about changing the player number. I think this exactly what you are looking for (or something similar): Java: Changing Player Number

Essentially I used a boolean array to keep track of who is still playing where the array index corresponds to the player number a[0] = Player 0, a[1] = Player 1, etc. If a player gets eliminated mark the corresponding index with false: a[i] = false; You can then use the following method (taken from my question) to switch the player number to the next player still playing:

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;
}

Let me know if you have any questions about my code, etc.

Upvotes: 2

Nuclearman
Nuclearman

Reputation: 5304

My java is a bit rusty, but something like the following should work.

i = 0
while (playing == True)
{
    player = playerList[i]
    i = (i + 1) % playerList.length

    [Do something]
}

Upvotes: 0

Bohemian
Bohemian

Reputation: 425083

Keep track of the turn number using an instance variable:

private int turn;

Increment it every turn:

turn++;

The index of the player whose turn it is can be calculated by using the remainder of dividing the turn by the number of players:

int playerIndex = turn % playersAmount;

I leave it to you to work these parts into your code.

Upvotes: 0

Adam Schmidt
Adam Schmidt

Reputation: 452

Well, to represent the current player you can use a int

int curplayer = 0;

Every time their round is done you can add one to get the index of the next player.

curplayer++;

As for it returning to the first player after the last player, I suggest you look into the % (modulo) operator.

Upvotes: 0

Will Jamieson
Will Jamieson

Reputation: 926

You have two different options in my opinion.

  1. create a Player object with instance variables as his name and a boolean that states whether or not he is active.
  2. You can create a boolean array that is sync with the player array that states wether or not the player is active.

ex for 2

boolean[] activeStatus= new boolean[1];
String[] players = new String[1];
activeStatus[0]=true;
players[0]="Joe Smith";

Upvotes: 0

Related Questions