From dd7e00061e9d747e7c9b584e36405fc1c2f08375 Mon Sep 17 00:00:00 2001 From: c23056305 <FernandesK3@cardiff.ac.uk> Date: Mon, 6 May 2024 20:25:44 +0100 Subject: [PATCH] Added bankruptcy, if a players bank account goes to 0 or below the game checks if the player had previously bought a property, if the player did the game force sells the property and gives the money to the player game continues as normal, else if player has no assets player is removed from the game. If there is only 1 player left they are declared the winner. --- monopoly.03-May.log.gz | Bin 0 -> 245 bytes monopoly.04-May.log.gz | Bin 0 -> 344 bytes monopoly.05-May.log.gz | Bin 0 -> 364 bytes .../com/cm6123/monopoly/app/Application.java | 35 ++++++- .../java/com/cm6123/monopoly/game/Board.java | 28 +++--- .../com/cm6123/monopoly/game/GameAction.java | 24 +++-- .../java/com/cm6123/monopoly/game/Player.java | 59 +++++++++++ .../com/cm6123/monopoly/BankruptcyTest.java | 94 ++++++++++++++++++ .../cm6123/monopoly/PropertyPurchaseTest.java | 7 +- .../com/cm6123/monopoly/PropertyTest.java | 16 ++- 10 files changed, 231 insertions(+), 32 deletions(-) create mode 100644 monopoly.03-May.log.gz create mode 100644 monopoly.04-May.log.gz create mode 100644 monopoly.05-May.log.gz create mode 100644 src/test/java/com/cm6123/monopoly/BankruptcyTest.java diff --git a/monopoly.03-May.log.gz b/monopoly.03-May.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..73f4bdc58d3bbc8df50ed5d9a23788724e8be77f GIT binary patch literal 245 zcmb2|=3oGW|8uX}dL0cAV0{o*z+cYH|IFO{dx=?7uh=?;6&Vg+KJx6SRo}R2GIOSV zk=kcr;qJ*#Jol`0WKY_%`}ghkYkB9MfB%|!^V<3TZ##6SzkRy1cKz+>Xub9EJN9j| zH?lI(oXJ<}=U1S*<Z#*&K_i8OOKnrmO!?AMl)2ZrWm4qdIUJFJ>MjA7cn@8v^hi}x zoXC}aDQsd_N=kcZ@(e3)znEKn0uky@EcWRdb1n|!aXclZoD}Z!O}F?_)7v74&RES% zx4SK|ZnjfOma9DsH!9DVUOZRH?^=E9jq{UCx1TFjeKq&L?{DTwzx3nS*cliAQkZX* literal 0 HcmV?d00001 diff --git a/monopoly.04-May.log.gz b/monopoly.04-May.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..6bf6557b832381e5df923aa055e225c62f6427ad GIT binary patch literal 344 zcmb2|=3oGW|8uX|da*bPus(?Sz*o$;aM!A;s$Z#UO`UA>5&|!>UQCt!@N1uu@nv>t zpHG2TW9|3m%?rP_UT3=4hnC+()#b(eb*?YJeEZ|a4f6K)rnmnsxHo_PwDphw?UFgC zKYz!b5BG~)c^u1bxUD(ax$dNyz@j@BOkT3(ui}}UB2@0nr+Zs8?Z~W>84{NTXKO9Z zaX-stud;KIuP58)&~rNvN(8@DJaw>8@JUOn*kmKYQ+NI1baq{Gp4`p9><pJ`_{2@7 z>Pita{(I}O+{@)M^j4dt(SBw76x*em+B?0z>YTh5x+(X=gj6qCe@UN+7yBmhtWNb_ zxu;2WXZ}>VMR%5nuT)|8y}>yp^OAkfq%&bJqt{0U-g`5tXXVLbnwL|yt_u=9e%^ob zoJ|*0&ab?3IXigyCf_}2OS7M!ak+kOX7JwVchSLCidUv@f8y{z`%HdS!m;aRJPZr~ DDa5EU literal 0 HcmV?d00001 diff --git a/monopoly.05-May.log.gz b/monopoly.05-May.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..368c5b53be627b49d7c2ed2904712fc4ff5d38e8 GIT binary patch literal 364 zcmb2|=3oGW|8u7u^kR18X?x%F-audBjmqS^H}CA>i2L$Dc#2l(!Yh~be*8*XrqJ>A z0ekVC{s~Y1|6jus^p352`lem8qJ8`Q`Oj<KmGA$Zx;t`uzjIZj`SP!CO>6aDSKpdb zmcD=QMER7Hac*9-j4s6(DXWJEZT>OAcJi#bA`!(Uf-Y%=k`k7?Ce^Y<#-_WS%VV7= zBsEJyGjQ4@rju$8Zc@dOg`)aLZXWfXl4^Q5)nmJu*k`9S=_PFpjF!84=k-pU%)jKk z-y)Nlv%)9*N>cskF~9L@$DRL^6VK^Yxozsca^eP;iuR@43AdtBRlHxhi7jd2UYX;x zbkUw%mD4}OKD;`nmSU)QZTUk^*I7Jgw8I`hFfK}yn)FOmwQG9aW=T!v&*|!-OVX5Y zisqygD*xswauI*nunzW(0%xBnjRkty46*S|t#*_$U<5B%OO@UuQM<!IK-$Kq@g WrMUJk+x-67J_bo;_9M2u3=9Ah)~W#j literal 0 HcmV?d00001 diff --git a/src/main/java/com/cm6123/monopoly/app/Application.java b/src/main/java/com/cm6123/monopoly/app/Application.java index d7bc0f7..ff865af 100644 --- a/src/main/java/com/cm6123/monopoly/app/Application.java +++ b/src/main/java/com/cm6123/monopoly/app/Application.java @@ -51,6 +51,7 @@ public final class Application { List<Player> players = new ArrayList<>(); Banker banker = new Banker(); GameHandler gameHandler = new GameAction(board); + ArrayList<Player> playersArrayList = new ArrayList<>(players); for (int i = 0; i < numberOfPlayers; i++) { @@ -67,7 +68,11 @@ public final class Application { round++; System.out.println("Round " + round); + + for (int turn = 0; turn < numberOfPlayers; turn++) { + + Player currentPlayer = players.get(currentPlayerIndex); System.out.println("It's " + currentPlayer.getName() + "'s turn. Type 'R' to roll the die."); @@ -82,13 +87,26 @@ public final class Application { } } + if (currentPlayer.getMoney() <= 0 && !currentPlayer.getHasAssets()) { + currentPlayer.checkBankruptcy(currentPlayer, board); + players.remove(currentPlayer); + numberOfPlayers--; + currentPlayerIndex--; + } + + if (currentPlayer.getMoney() <= 0 && currentPlayer.getHasAssets()) { + currentPlayer.checkBankruptcy(currentPlayer, board); + } + int newPosition = currentPlayer.getPosition(); String tileName = board.getGameBoard(newPosition); System.out.println(currentPlayer.getName() + " landed on " + tileName); - currentPlayerIndex = (currentPlayerIndex + 1) % numberOfPlayers; + System.out.println("You have £" + currentPlayer.getMoney() + " in your bank account "); + + if (newPosition == 4 || newPosition == 5 || newPosition == 9 || newPosition == 13 || newPosition == 15) { if (board.canBuyProperty(newPosition)) { gameHandler.landedOnProperty(currentPlayer); @@ -105,16 +123,23 @@ public final class Application { currentPlayer.addMoney(200); System.out.println("You now have £" + currentPlayer.getMoney() + " in your bank account "); } + + currentPlayerIndex = (currentPlayerIndex + 1) % numberOfPlayers; + } if (GameEndings.checkRoundsReached(round, numberOfRounds)) { + GameEndings.determineWinner(new ArrayList<>(players)); break; } - } - ArrayList<Player> playersArrayList = new ArrayList<>(players); - GameEndings.determineWinner(playersArrayList); + if (players.size() < 2) { + GameEndings.determineWinner(new ArrayList<>(players)); + break; + } + + } - LOGGER.info("Application closing"); + LOGGER.info("Thank you for playing!"); } } diff --git a/src/main/java/com/cm6123/monopoly/game/Board.java b/src/main/java/com/cm6123/monopoly/game/Board.java index 056c0e1..f0515bf 100644 --- a/src/main/java/com/cm6123/monopoly/game/Board.java +++ b/src/main/java/com/cm6123/monopoly/game/Board.java @@ -15,7 +15,7 @@ public class Board { /** * HashMap with the properties on them so that when a player buys the property it'll be noticed. */ - private static final HashMap<Integer, Boolean> PROPERTY_OWNERSHIP = new HashMap<>(); + public static final HashMap<Integer, Boolean> PROPERTY_OWNERSHIP = new HashMap<>(); /** * Constructor for the board. @@ -90,22 +90,26 @@ public class Board { /** * If a player decides to purchase a property then 200 will be taken away from the players bank account. * The posisiton/property they just bought will become true so now other players cannot purchase the same property. - * + * It also sets the players 'hasAssets' to true so if the player goes bankrupt then player can sell property. * @param position the position of the property to be purchased * @param player the player buying the property */ public void buyProperty(final int position,final Player player) { - if (canBuyProperty(position)) { - String propertyName = getGameBoard(position); - Properties property = new Properties(propertyName); - player.subtractMoney(property.getPrice()); - PROPERTY_OWNERSHIP.put(position, true); - property.setOwner(player); - - Player propertyOwner = property.getOwner(); + try { + if (canBuyProperty(position)) { + String propertyName = getGameBoard(position); + Properties property = new Properties(propertyName); + player.subtractMoney(property.getPrice()); + PROPERTY_OWNERSHIP.put(position, true); + player.setHasAssets(true); + property.setOwner(player); - System.out.println(propertyOwner.getName() + " has bought this property."); - System.out.println("You now have £" + player.getMoney() + " in your bank account."); + Player propertyOwner = property.getOwner(); + System.out.println(propertyOwner.getName() + " has bought this property."); + System.out.println("You now have £" + player.getMoney() + " in your bank account."); + } + } catch (Exception e) { + System.out.println("Error has occurred during purchase" + e.getMessage()); } } } diff --git a/src/main/java/com/cm6123/monopoly/game/GameAction.java b/src/main/java/com/cm6123/monopoly/game/GameAction.java index 63d0496..9fff475 100644 --- a/src/main/java/com/cm6123/monopoly/game/GameAction.java +++ b/src/main/java/com/cm6123/monopoly/game/GameAction.java @@ -49,11 +49,10 @@ public class GameAction implements GameHandler { if (input.equals("Y")) { if (player.getMoney() >= 200) { this.board.buyProperty(position, player); - break; } else { System.out.println("You do not have enough money to buy " + propertyName + ". Moving on..."); - break; } + break; // if user types N then user doesn't want to buy the property and then continue the game } else if (input.equals("N")) { System.out.println("You have chosen N, moving on..."); @@ -65,6 +64,7 @@ public class GameAction implements GameHandler { } } + /** * Performs game actions when a player lands on a train station. * Allows the player to roll again to determine a ticket cost until they enter 'R'. @@ -97,17 +97,19 @@ public class GameAction implements GameHandler { int trainTicket = 10; int ticket = trainTicket * diceResult; - // Subtract ticket cost from player's money and display updated balance - System.out.println("You pay £" + ticket + " to the banker"); - player.subtractMoney(ticket); - System.out.println("You now have £" + player.getMoney() + " in your bank account"); + if (player.getMoney() > 0) { + // Subtract ticket cost from player's money and display updated balance + System.out.println("You pay £" + ticket + " to the banker"); + player.subtractMoney(ticket); + System.out.println("You now have £" + player.getMoney() + " in your bank account"); - // Pay the ticket cost to the banker - Banker banker = new Banker(); - banker.getMoneyFromPlayer(ticket); + // Pay the ticket cost to the banker + Banker banker = new Banker(); + banker.getMoneyFromPlayer(ticket); - // Exit the loop since the user has entered 'R' - break; + // Exit the loop since the user has entered 'R' + break; + } } else { // Make the user enter 'R' or game doesn't continue System.out.println("Invalid input. Please enter 'R'"); diff --git a/src/main/java/com/cm6123/monopoly/game/Player.java b/src/main/java/com/cm6123/monopoly/game/Player.java index 6c6ef9a..a64a78e 100644 --- a/src/main/java/com/cm6123/monopoly/game/Player.java +++ b/src/main/java/com/cm6123/monopoly/game/Player.java @@ -22,6 +22,11 @@ public class Player { */ private int position; + /** + * Checking if the player has bought properties, initially false. + */ + private static boolean hasAssets = false; + /** @@ -108,6 +113,27 @@ public class Player { this.position = playerPosition; } + /** + * Gets the players assets. + * + * @return the players assets. + */ + public boolean getHasAssets() { + return hasAssets; + } + + /** + * Set the players assets. + * + * @param assetsOwned + */ + public void setHasAssets(final boolean assetsOwned) { + this.hasAssets = assetsOwned; + } + + + + /** List of all players in the game. */ public static final ArrayList<Player> PLAYERSINTHEGAME = new ArrayList<>(); @@ -119,4 +145,37 @@ public class Player { public static ArrayList<Player> getPlayers() { return PLAYERSINTHEGAME; } + + /** + * Handles bankruptcy of the player by selling assets if available, or removing the player from the game. + * + * @param player The player who is declaring bankruptcy. + * @param board The game board containing property ownership information. + */ + public void checkBankruptcy(final Player player, final Board board) { + while (true) { + if (player.getMoney() <= 0) { + if (hasAssets) { + for (Integer propertyPosition : board.PROPERTY_OWNERSHIP.keySet()) { + if (board.PROPERTY_OWNERSHIP.get(propertyPosition)) { + board.PROPERTY_OWNERSHIP.put(propertyPosition, false); + player.addMoney(200); + System.out.println("You have sold " + board.getGameBoard(position) + " for £200 to survive bankruptcy"); + System.out.println("You now have £" + player.getMoney() + " in your bank account"); + player.setHasAssets(false); + return; + } + } + } + + System.out.println("You have no assets to sell, therefore you are bankrupt!"); + System.out.println(player.getName() + " has been removed from the game"); + PLAYERSINTHEGAME.remove(player); + break; + + } else { + break; + } + } + } } diff --git a/src/test/java/com/cm6123/monopoly/BankruptcyTest.java b/src/test/java/com/cm6123/monopoly/BankruptcyTest.java new file mode 100644 index 0000000..88d6be5 --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/BankruptcyTest.java @@ -0,0 +1,94 @@ +package com.cm6123.monopoly; + +import com.cm6123.monopoly.game.Board; +import com.cm6123.monopoly.game.GameAction; +import com.cm6123.monopoly.game.Player; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class BankruptcyTest { + + @Test + public void testBankruptcyIfNoAsset() { + Board board = new Board(); + Player player1 = new Player("Player1", 10, 0); // Player1 starts with very low money (10) + Player player2 = new Player("Player2", 1000, 0); // Player2 starts with sufficient money (1000) + + Player.getPlayers().add(player1); + Player.getPlayers().add(player2); + GameAction gameAction = new GameAction(board); + + // Land player 1 on tile 7 which is a train station to force player to go below 0 + player1.setPosition(7); + ByteArrayInputStream in = new ByteArrayInputStream("R".getBytes()); + System.setIn(in); + + gameAction.landedOnTrainStation(player1); + + // Make player 1 now land on a property and type "N" to not buy the property + player1.setPosition(9); + in = new ByteArrayInputStream("N".getBytes()); + System.setIn(in); + + gameAction.landedOnProperty(player1); + + // Check bankruptcy and remove player if bankrupt + if (player1.getMoney() <= 0) { + player1.checkBankruptcy(player1, board); + Player.getPlayers().remove(player1); + } + // checking if the player is bankrupt and removing the player + // checking if there is only 1 player remaining since player 1 got removed + assertTrue(player1.getMoney() <= 0); + assertFalse(Player.getPlayers().contains(player1)); + assertEquals(1, Player.getPlayers().size()); + Player.getPlayers().remove(player1); + Player.getPlayers().remove(player2); + } + + + @Test + public void testBankruptcyIfPlayerHasAssets() { + Board board = new Board(); + Player player1 = new Player("Player1", 210, 0); + Player player2 = new Player("Player2", 1000, 0); + + Player.getPlayers().add(player1); + Player.getPlayers().add(player2); + + + GameAction gameAction = new GameAction(board); + + ByteArrayInputStream in = new ByteArrayInputStream("Y".getBytes()); + // Make the player land on a property and purchases the property, the player will have £10 + player1.setPosition(9); + in = new ByteArrayInputStream("Y".getBytes()); + System.setIn(in); + + gameAction.landedOnProperty(player1); + + // Make player 1 land on a train station and pay the ticket so the players balance goes below 0 + player1.setPosition(7); + in = new ByteArrayInputStream("R".getBytes()); + System.setIn(in); + + gameAction.landedOnTrainStation(player1); + + // Now make player 1 land on a property and type "N" + + if (player1.getMoney() <= 0 && player2.getHasAssets() == true) { + // Checking if players money is below 0, if it is remove the player + player1.checkBankruptcy(player1, board); + } + // The game should have forced sold the property the player had bought and added the funds to the players bank account + // checking if the player got removed or not + assertEquals(2, Player.getPlayers().size() ); + Player.getPlayers().remove(player1); + Player.getPlayers().remove(player2); + player1.setHasAssets(false); + } +} + diff --git a/src/test/java/com/cm6123/monopoly/PropertyPurchaseTest.java b/src/test/java/com/cm6123/monopoly/PropertyPurchaseTest.java index d293f23..e7fc4b8 100644 --- a/src/test/java/com/cm6123/monopoly/PropertyPurchaseTest.java +++ b/src/test/java/com/cm6123/monopoly/PropertyPurchaseTest.java @@ -1,5 +1,6 @@ package com.cm6123.monopoly; +import com.cm6123.monopoly.game.Banker; import com.cm6123.monopoly.game.Player; import com.cm6123.monopoly.game.GameAction; import com.cm6123.monopoly.game.Board; @@ -17,7 +18,7 @@ public class PropertyPurchaseTest { Board board = new Board(); - Player player = new Player("TestPlayer", 1000, 0); // Example player with £1000 starting money and at position 0 + Player player = new Player("TestPlayer", 1000, 0); player.setPosition(4); ByteArrayInputStream in = new ByteArrayInputStream("Y".getBytes()); @@ -27,7 +28,7 @@ public class PropertyPurchaseTest { GameAction gameAction = new GameAction(board); gameAction.landedOnProperty(player); - assertEquals(4, player.getPosition(), "Player should land on tile 4 (Old Kent Road)"); + assertEquals(4, player.getPosition()); System.setIn(System.in); } @@ -40,7 +41,7 @@ public class PropertyPurchaseTest { Player owner = new Player("TestPlayer1", 1200, 0); board.buyProperty(4, owner); - Player testPlayer2 = new Player("TestPlayer2", 1000, 0); // Example player with £1000 starting money and at position 0 + Player testPlayer2 = new Player("TestPlayer2", 1000, 0); testPlayer2.setPosition(4); GameAction gameAction = new GameAction(board); diff --git a/src/test/java/com/cm6123/monopoly/PropertyTest.java b/src/test/java/com/cm6123/monopoly/PropertyTest.java index fde740f..4e7f11f 100644 --- a/src/test/java/com/cm6123/monopoly/PropertyTest.java +++ b/src/test/java/com/cm6123/monopoly/PropertyTest.java @@ -1,22 +1,36 @@ package com.cm6123.monopoly; +import com.cm6123.monopoly.game.Board; +import com.cm6123.monopoly.game.GameAction; +import com.cm6123.monopoly.game.Player; import com.cm6123.monopoly.game.Properties; import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; + import static org.junit.jupiter.api.Assertions.*; public class PropertyTest { @Test public void testSetAndGetPrice() { + // checking if the price of the property changes Properties property = new Properties("Old Kent Road"); property.setPrice(400); int actualPrice = property.getPrice(); assertEquals(400, actualPrice); } -} + @Test + public void testAssets() { + Player player4 = new Player("TestPlayer", 1000, 0); + // Setting and checking if players assets become true + // Used in board file if player buys a property + player4.setHasAssets(true); + assertTrue(player4.getHasAssets()); + } +} -- GitLab