示例#1
0
 @Override
 public void buyField(Player player) {
   int svar = GUI.getUserInteger(Texts.getText("køb"));
   while (svar != 1 && svar != 2) {
     // Man har svaret andet end 1 eller 2
     svar = GUI.getUserInteger(Texts.getText("køb"));
   }
   if (svar == 1) {
     if (player.getBalance() > price) {
       player.addToBalance(-price);
       super.setOwner(player);
       GUI.setOwner(player.getCurrentField() + 1, player.getName());
       GUI.showMessage(
           Texts.getText("købt") + Texts.getText("f" + player.getCurrentField() + "s" + 1));
       player.setLaborCamp();
     } else {
       GUI.showMessage(Texts.getText("fattig"));
     }
   }
 }
示例#2
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
      }
    }
  }