Exemplo n.º 1
0
  public void run() {

    // Set up the game board
    gameboard.setupGuiFields();
    // createCars();

    // Determines the number of players in the game
    numOfPlayers = GUI.getUserInteger(Text.howManyPlayers, 2, 6);
    activePlayers = numOfPlayers;

    // This loop adds the determined number of players to the game
    for (int i = 0; i < numOfPlayers; i++) {
      name[i] = GUI.getUserString(Text.enterName[i]);

      while ((name[i].equals("")) || name[i].equals(" ")) {
        GUI.showMessage(Text.nameInvalid);
        name[i] = GUI.getUserString(Text.enterName[i]);
      }
      for (int j = 1; j <= i; j++) {
        while (name[i].equals(name[i - j])) {
          GUI.showMessage(Text.nameTaken);
          name[i] = GUI.getUserString(Text.enterName[i]);
        }
      }
      player[i] =
          new Player(30000, name[i]); // Player gets a balance and the name chosen by the user
      GUI.addPlayer(
          player[i].getName(),
          player[i].getAccount().getBalance(),
          gameboard.getCar()[i]); // Player is added to board
      GUI.setCar(1, player[i].getName()); // Players car is added to board
    }

    // This loop gives all active players a turn, until only one player is left
    while (activePlayers > 1) {
      for (int i = 0; i < numOfPlayers; i++) {
        playerTurn(player[i]);
        for (int j = 0; j < numOfPlayers; j++)
          GUI.setBalance(
              player[j].getName(),
              player[j].getAccount().getBalance()); // Updates everybody's balance after every turn
        if (activePlayers == 1) break; // Breaks out of the loop, if only one active player is left
      }
      for (int i = 0; i < numOfPlayers; i++) {
        setWinner(player[i]); // Checks if only one player is left, then sets the player as winner
      }
    }
  }