/* This method contains two different parts. First part is for a tax field that collects a certain fixed tax. The second part is for the tax field where the player gets a choice, to either pay 10% or 4000 The first part only runs if the player position is 3, and the second i its 13 */ @Override public void landedOn(Player player) { if (player.getPosition() == place) { if (player.getPosition() == 3) { // gold mine tax GUI.showMessage( player.getName() + Text.landedOn + Text.fieldName[place] + " " + Text.payTax + ": " + taxAmount3); player.getAccount().addBalance(-taxAmount3); // removes 2000 from balance } if (player.getPosition() == 13) { // caravan tax GUI.showMessage(player.getName() + Text.landedOn + Text.fieldName[place]); boolean Buttonpressed = GUI.getUserLeftButtonPressed(Text.taxChoice, "-10%", "-4000"); { if (Buttonpressed) { // We make a boolean called "Buttonpressed" if the boolean is true, // the player pressed 10% pay10procent(player); // 10% is remove from balance } else pay4000(player); // -4000 was pressed, 4000 is removed from balance } } } }
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 } } }
@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")); } } }
@Override public void payRent(Player player) { if (player.getBalance() > getRent()) { player.addToBalance(-getRent()); owner.addToBalance(getRent()); GUI.showMessage(Texts.getText("husleje") + owner.getName()); GUI.setBalance(player.getName(), player.getBalance()); GUI.setBalance(owner.getName(), owner.getBalance()); } else { owner.addToBalance(player.getBalance()); player.bankrupt(); GUI.removeAllCars(player.getName()); } }
public static void spilloop( Spiller player, Terning tern, Felt felt, Konto konto, Tur tur, Strings string) { if (GUI.getUserButtonPressed( player.getNavn() + string.getButtonPress(), string.getButtonPressAnswer()) .equals(string.getButtonPressAnswer())) { GUI.removeAllCars(player.getNavn()); GUI.setCar(tern.kast() - 1, player.getNavn()); GUI.showMessage("" + string.getFeltDesc((tern.getVærdi() - 2))); int feltværdi = felt.getFeltVærdi((tern.getVærdi() - 2), string); if (feltværdi > 0) { konto.deposit(feltværdi); GUI.setBalance(player.getNavn(), konto.getBeholdning()); if (konto.getBeholdning() >= 3000) { GUI.showMessage(player.getNavn() + string.getWinText()); if (GUI.getUserButtonPressed( string.getNewGameText(), string.getNewGameTextPositive(), string.getNewGameTextNegative()) .equals(string.getNewGameTextPositive())) { GUI.showMessage(string.getExitMessage()); System.exit(0); } else { System.exit(0); } } } else if (feltværdi < 0) { if (feltværdi < konto.getBeholdning()) { konto.withdraw(-feltværdi); GUI.setBalance(player.getNavn(), konto.getBeholdning()); } else { konto.withdraw(konto.getBeholdning()); } } if (tern.getVærdi() - 2 != 8) tur.skift(); } }
// Method for the player turn - The player can only use the turn if their balance is not 0 private void playerTurn(Player player) { if (player.getAccount().getBalance() != 0) { GUI.showMessage(player.getName() + Text.roll); // The player is asked to roll the dice GUI.removeCar( player.getPosition(), player.getName()); // car is removed from previous position player.addPosition(dicecup.roll()); // Dice is rolled, and the players position is saved GUI.setDice( dicecup.getDie1().getLastRoll(), dicecup.getDie2().getLastRoll()); // Dice is shown on screen GUI.setCar(player.getPosition(), player.getName()); // Car is moved to field landedOnField(player); // Runs the operation for the field the player landed on GUI.setBalance( player.getName(), player.getAccount().getBalance()); // Shows current player balance on GUI game board if (player.getAccount().getBalance() == 0) bankRupt( player); // Player is removed from the game if their balance is 0, after their turn. } }
// Removes all properties and cars from player, and subtracts 1 from the variable activePlayers private void bankRupt(Player player) { GUI.showMessage(player.getName() + Text.bankrupt); GUI.removeAllCars(player.getName()); removeOwnership(player); activePlayers -= 1; }
// Makes sure that that landedOn method does not run on Start field private void landedOnField(Player player) { if (player.getPosition() != 1) // If position is not 1, landedOn operation will run gameboard.getLogicField()[player.getPosition()].landedOn(player); else // If position is 1, player landed on start and no landedOn operation will run GUI.showMessage(player.getName() + Text.startField); }
// This method set a player to be the winner if he/she is the only active player in the game private void setWinner(Player player) { if (player.getAccount().getBalance() != 0 && activePlayers == 1) { while (true) GUI.showMessage(">>> " + player.getName() + Text.winner + " <<<"); } }