/* 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 } } } }
@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()); } }
/** Rolls 2 dice and returns the sum */ public int roll() { int a = (int) (Math.random() * sides + 1); int b = (int) (Math.random() * sides + 1); GUI.setDice(a, b); value = a + b; return value; }
public static void main(String[] args) { Strings string = new Strings(); init(string); Spiller spiller1 = new Spiller("Sherlock", 1111); Spiller spiller2 = new Spiller("Watson", 1112); Konto Sherlock1111 = new Konto(1000); Konto Watson1112 = new Konto(1000); Terning tern = new Terning(6); Tur tur = new Tur(); Felt felt = new Felt(); GUI.addPlayer(spiller1.getNavn(), 1000); GUI.addPlayer(spiller2.getNavn(), 1000); while (true) { if (tur.getCheck() == 1) spilloop(spiller1, tern, felt, Sherlock1111, tur, string); else if (tur.getCheck() == 2) spilloop(spiller2, tern, felt, Watson1112, tur, string); } }
// 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. } }
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 } } }
// Removes player ownership, both from logic fields and GUI fields, if the player is the owner private void removeOwnership(Player player) { for (int i = 1; i <= 22; i++) { if (gameboard.getLogicField()[i] instanceof AbstractOwnables) { if (gameboard.getLogicField()[i].getOwner() == player) { gameboard.getLogicField()[i].removeOwner(); GUI.removeOwner(i); } } } }
@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 landOnField(Player player) { if (super.isOwned() == false) { buyField(player); GUI.setBalance(player.getName(), player.getBalance()); } else if (owner.isAlive() == true) { if (owner != player) { payRent(player); } } }
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(); } }
public void move(int move) { GUI.removeCar(felt + 1, "" + number); felt += move; felt = felt % 21; // sætter feltet til 0-20 GUI.setCar(felt + 1, "" + number); }
// 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 + " <<<"); } }
public static void init(Strings string) { // initialiserer Gui'en Field[] fields = new Field[11]; fields[0] = new Street.Builder().setTitle(string.getFelt1()).build(); fields[1] = new Street.Builder().setTitle(string.getFelt2()).build(); fields[2] = new Street.Builder().setTitle(string.getFelt3()).build(); fields[3] = new Street.Builder().setTitle(string.getFelt4()).build(); fields[4] = new Street.Builder().setTitle(string.getFelt5()).build(); fields[5] = new Street.Builder().setTitle(string.getFelt6()).build(); fields[6] = new Street.Builder().setTitle(string.getFelt7()).build(); fields[7] = new Street.Builder().setTitle(string.getFelt8()).build(); fields[8] = new Street.Builder().setTitle(string.getFelt9()).build(); fields[9] = new Street.Builder().setTitle(string.getFelt10()).build(); fields[10] = new Street.Builder().setTitle(string.getFelt11()).build(); GUI.create(fields); GUI.setDescriptionText(9, string.getExtraTurnMessage()); GUI.setSubText(1, string.getIncomeText() + ": +250"); GUI.setSubText(2, string.getIncomeText() + ": -100"); GUI.setSubText(3, string.getIncomeText() + ": +100"); GUI.setSubText(4, string.getIncomeText() + ": -20"); GUI.setSubText(5, string.getIncomeText() + ": +180"); GUI.setSubText(6, string.getIncomeText() + ": 0"); GUI.setSubText(7, string.getIncomeText() + ": -70"); GUI.setSubText(8, string.getIncomeText() + ": +60"); GUI.setSubText(9, string.getIncomeText() + ": -80"); GUI.setSubText(10, string.getIncomeText() + ": -50"); GUI.setSubText(11, string.getIncomeText() + ": +650"); }