diff --git a/src/main/java/com/cm6123/monopoly/app/Application.java b/src/main/java/com/cm6123/monopoly/app/Application.java index 5c9fe6ab56d8154bd4c93719d5ff69249e04fe31..d7bc0f725f0aa0b75f9d996af989da97f2abbb62 100644 --- a/src/main/java/com/cm6123/monopoly/app/Application.java +++ b/src/main/java/com/cm6123/monopoly/app/Application.java @@ -95,6 +95,10 @@ public final class Application { } } + if (newPosition == 7 || newPosition == 12 || newPosition == 14) { + gameHandler.landedOnTrainStation(currentPlayer); + } + if (newPosition == 1) { banker.checkIfPlayerPassHome(currentPlayer); System.out.println("Banker has given " + currentPlayer.getName() + " £200."); diff --git a/src/main/java/com/cm6123/monopoly/game/Banker.java b/src/main/java/com/cm6123/monopoly/game/Banker.java index b6cd0baeaf3d00af834bb071d1e8ca73143264ec..bc887e6e40af6de5680da4b1fc93e122a64e882e 100644 --- a/src/main/java/com/cm6123/monopoly/game/Banker.java +++ b/src/main/java/com/cm6123/monopoly/game/Banker.java @@ -20,7 +20,7 @@ public class Banker { * * @param amount The amount of money to add. */ - public void addMoney(final int amount) { + public void getMoneyFromPlayer(final int amount) { balance += amount; } diff --git a/src/main/java/com/cm6123/monopoly/game/Board.java b/src/main/java/com/cm6123/monopoly/game/Board.java index 4b268e2d28bd3eae578de659db403c12f7785cff..056c0e1cace3c84be2e9972e4561249e407d249a 100644 --- a/src/main/java/com/cm6123/monopoly/game/Board.java +++ b/src/main/java/com/cm6123/monopoly/game/Board.java @@ -42,7 +42,7 @@ public class Board { GAME_BOARD.put(11, "Tax Office"); GAME_BOARD.put(12, "Waterloo"); GAME_BOARD.put(13, "Leicester Square"); - GAME_BOARD.put(14, "Road"); + GAME_BOARD.put(14, "Cardiff Central"); GAME_BOARD.put(15, "Park Lane"); GAME_BOARD.put(16, "Road"); } diff --git a/src/main/java/com/cm6123/monopoly/game/GameAction.java b/src/main/java/com/cm6123/monopoly/game/GameAction.java index 8c20464b684360277fdc8e3ff28fca2361c3d933..63d049657c846775440a30eb4cec252773e6ca73 100644 --- a/src/main/java/com/cm6123/monopoly/game/GameAction.java +++ b/src/main/java/com/cm6123/monopoly/game/GameAction.java @@ -1,5 +1,7 @@ package com.cm6123.monopoly.game; +import com.cm6123.monopoly.dice.Dice; + import java.util.Scanner; /** @@ -8,22 +10,23 @@ import java.util.Scanner; public class GameAction implements GameHandler { /** - * The board. + * The board where the properties and train stations lie on. + * (I cant fix this checkstyle error [MissingJavadocComment]) ?? */ private final Board board; - /** - * Constructor for GameAction with the board. + * Constructor for the gameboard. * - * @param gameboard the board used for game actions + * @param gameboard the game board used for game actions */ public GameAction(final Board gameboard) { this.board = gameboard; } /** - * Game action if a player lands on a property. + * Performs game actions when a player lands on a property. + * This method prompts the player to buy the property if possible, and handles the player's choice. * * @param player the player who landed on the property */ @@ -34,22 +37,80 @@ public class GameAction implements GameHandler { // Check if the property at the current position can be bought if (this.board.canBuyProperty(position)) { String propertyName = this.board.getGameBoard(position); - System.out.println("Would you like to buy " + propertyName + " for £200? (Y/N)"); Scanner scanner = new Scanner(System.in); + + // incased in a while loop so the player has to type either Y or N + while (true) { + System.out.println("Would you like to buy " + propertyName + " for £200? (Y/N)"); + String input = scanner.next().toUpperCase(); + + // if the user input is Y subtract 200 from the players account and make thm th woner of the property + 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; + } + // 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..."); + break; + } else { // force the player to enter either Y or N if they typed anything else + System.out.println("Invalid input. Please enter (Y/N)"); + } + } + } + } + + /** + * 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'. + * + * @param player the player landing on the train station + */ + @Override + public void landedOnTrainStation(final Player player) { + Movement movement = new Movement(); + Dice dice = new Dice(6); + + // Roll two dice to determine the result + int roll1 = dice.roll(); + int roll2 = dice.roll(); + int diceResult = roll1 + roll2; + + Scanner scanner = new Scanner(System.in); + + // Loop until the user enters 'R' + while (true) { + // Make the user roll again to get the ticket amount they nede to pay + System.out.println("Enter 'R' to Roll again to determine your ticket"); + String input = scanner.next().toUpperCase(); - // if Player chooses to buy the property check if the player has enough to buy it - if (input.equals("Y")) { - if (player.getMoney() >= 200) { - this.board.buyProperty(position, player); - } else { - System.out.println("You do not have enough money to buy " + propertyName + ". Moving on..."); - } // if player doesn't want to buy it move on - } else if (input.equals("N")) { - System.out.println("You have chosen N, moving on..."); - } else { // if user enters something else skip the turn - System.out.println("Invalid input. Skipping turn"); + if (input.equals("R")) { + System.out.println("You rolled " + diceResult); + + // Calculate ticket cost based on dice result + 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"); + + // Pay the ticket cost to the banker + Banker banker = new Banker(); + banker.getMoneyFromPlayer(ticket); + + // 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/GameHandler.java b/src/main/java/com/cm6123/monopoly/game/GameHandler.java index 4e1da59ad3df9d74e8ff7d38ddd9b2618b616efa..50b0e0487102709d5856b0ff079dbd20a96d72aa 100644 --- a/src/main/java/com/cm6123/monopoly/game/GameHandler.java +++ b/src/main/java/com/cm6123/monopoly/game/GameHandler.java @@ -8,8 +8,15 @@ public interface GameHandler { /** * Triggers when player lands on a property. * - * @param player the player who landed on the property + * @param player the player who landed on the property. */ void landedOnProperty(Player player); + /** + * Triggers when player lands on a train station. + * + * @param player the player who landed on the train station. + */ + void landedOnTrainStation(Player player); + } diff --git a/src/main/java/com/cm6123/monopoly/game/Movement.java b/src/main/java/com/cm6123/monopoly/game/Movement.java index de784a5235afa8357a4554c4be785839e29089da..fa82a12943b6e39798d3727e3f0b1986b6fc09d7 100644 --- a/src/main/java/com/cm6123/monopoly/game/Movement.java +++ b/src/main/java/com/cm6123/monopoly/game/Movement.java @@ -15,6 +15,7 @@ public class Movement { private final Dice dice = new Dice(6); + /** * Player asked to roll die and the result is added up (diceResult) for the sum of both dies rolled. * diff --git a/src/test/java/com/cm6123/monopoly/BankerTest.java b/src/test/java/com/cm6123/monopoly/BankerTest.java index 88917840317e5b0844218b7981f67f27cb1aebb8..f558cf6392431e9fd8c1c7e6b74689b0c0a96a80 100644 --- a/src/test/java/com/cm6123/monopoly/BankerTest.java +++ b/src/test/java/com/cm6123/monopoly/BankerTest.java @@ -20,7 +20,7 @@ public class BankerTest { int amountToAdd = 200; // Amount of money to add to the balance // Add money to the banker's balance - banker.addMoney(amountToAdd); + banker.getMoneyFromPlayer(amountToAdd); // Check if the balance is icnreased by 200 assertEquals(5200, banker.getBalance()); diff --git a/src/test/java/com/cm6123/monopoly/TrainStationTest.java b/src/test/java/com/cm6123/monopoly/TrainStationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..500bda5a4408c1806a4a9560efeada915f9b029c --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/TrainStationTest.java @@ -0,0 +1,44 @@ +import com.cm6123.monopoly.game.Board; +import com.cm6123.monopoly.game.Player; +import com.cm6123.monopoly.game.GameAction; +import com.cm6123.monopoly.game.Movement; +import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TrainStationTest { + + @Test + public void testPlayerPosition() { + // check if the test player lands on tile 7 which is a train station + Board board = new Board(); + Player player = new Player("TestPlayer", 1000, 7); + assertEquals(7, player.getPosition()); + + } + + @Test + public void testTrainStation() { + // make the test player land on tile 7 which is a train station + Board board = new Board(); + Player player = new Player("TestPlayer", 1000, 7); + + // make the test player enter 'R' + String input = "R\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + GameAction gameAction = new GameAction(board); + gameAction.landedOnTrainStation(player); + + // Check if money has been subtracted from the players bank account which means. + // that the train station is doing what it's intended on doing + // the player rolls the die and the ticketprice is multiplied by the dice result + // money is subtracted from the player and given to the banker. + assertTrue(player.getMoney() < 1000); + } +} \ No newline at end of file