diff --git a/monopoly.30-Apr.log.gz b/monopoly.30-Apr.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..79d9d61c4cac3f3de967d9dec6f3009c8b6530e4 Binary files /dev/null and b/monopoly.30-Apr.log.gz differ diff --git a/src/main/java/com/cm6123/monopoly/app/Application.java b/src/main/java/com/cm6123/monopoly/app/Application.java index 4c906df76b81fc0c58d72f56cc9f32e731e7885f..1db2187b8acd26a1e402c86a8cbb932831a042c3 100644 --- a/src/main/java/com/cm6123/monopoly/app/Application.java +++ b/src/main/java/com/cm6123/monopoly/app/Application.java @@ -1,8 +1,19 @@ package com.cm6123.monopoly.app; +import com.cm6123.monopoly.game.Player; +import com.cm6123.monopoly.game.Board; +import com.cm6123.monopoly.game.Movement; +import com.cm6123.monopoly.game.GameEndings; +import com.cm6123.monopoly.game.Banker; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + + public final class Application { /** @@ -26,6 +37,75 @@ public final class Application { LOGGER.info("Application Started with args:{}", String.join(",", args)); System.out.println("Hello. Welcome to Monopoly."); + Scanner scanner = new Scanner(System.in); + + System.out.println("Please enter how many rounds to play"); + int numberOfRounds = scanner.nextInt(); + + System.out.println("Please enter how many players to play this game:"); + int numberOfPlayers = scanner.nextInt(); + + Board board = new Board(); + Movement movement = new Movement(); + List<Player> players = new ArrayList<>(); + Banker banker = new Banker(); + + + for (int i = 0; i < numberOfPlayers; i++) { + System.out.println("Please enter player " + (i + 1) + "'s name"); + String playerName = scanner.next(); + Player player = new Player(playerName, 1000, 1); + players.add(player); + } + + int currentPlayerIndex = 0; + int round = 0; + + while (true) { + 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."); + + + + while(true) { + String input = scanner.next().toUpperCase(); + if (input.equals("R")) { + movement.diceResult(currentPlayer); + break; + } else { + System.out.println("Invalid input. Type 'R' to roll the dice."); + } + } + + + 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 == 1) { + banker.checkIfPlayerPassHome(currentPlayer); + System.out.println("Banker has given " + currentPlayer.getName() + " £200."); + currentPlayer.addMoney(200); + System.out.println("You now have £" + currentPlayer.getMoney() + " in your bank account "); + } + + } + + if (GameEndings.checkRoundsReached(round, numberOfRounds)) { + break; + } + } + + ArrayList<Player> playersArrayList = new ArrayList<>(players); + GameEndings.determineWinner(playersArrayList); diff --git a/src/main/java/com/cm6123/monopoly/game/Banker.java b/src/main/java/com/cm6123/monopoly/game/Banker.java new file mode 100644 index 0000000000000000000000000000000000000000..338aacb56e7e876b6543a7e7c9fb1a2f7f882362 --- /dev/null +++ b/src/main/java/com/cm6123/monopoly/game/Banker.java @@ -0,0 +1,66 @@ +package com.cm6123.monopoly.game; + + +/** + * Represents the banker in a Monopoly game, responsible for managing the game's financial transactions. + */ +public class Banker { + /** + * The initial balance of the banker. + */ + private static final int INITIAL_BALANCE = 5000; + + /** + * The current balance of the banker. + */ + private int balance; + + /** + * Adds money to the banker's balance. + * + * @param amount The amount of money to add. + */ + public void addMoney(final int amount) { + balance += amount; + } + + /** + * Constructs a new Banker instance with the initial balance. + */ + public Banker() { + this.balance = INITIAL_BALANCE; + } + + /** + * Pays a specified amount of money to a player and deducts this amount from the banker's balance. + * + * @param player The player to whom the money is being paid. + * @param amount The amount of money to pay to the player. + */ + public void payPlayerMoney(final Player player, final int amount) { + player.addMoney(amount); + this.balance -= amount; + } + + /** + * Checks if a player has passed the 'Go' (Home) position and pays them accordingly. + * If the player's position is greater than 1, the banker pays them $0 (as per the Monopoly rules). + * + * @param player The player whose position is being checked. + */ + public void checkIfPlayerPassHome(final Player player) { + if (player.getPosition() > 1) { + payPlayerMoney(player, 0); + } + } + + /** + * Retrieves the current balance of the banker. + * + * @return The current balance of the banker. + */ + public int getBalance() { + return balance; + } + +} diff --git a/src/main/java/com/cm6123/monopoly/game/Board.java b/src/main/java/com/cm6123/monopoly/game/Board.java new file mode 100644 index 0000000000000000000000000000000000000000..4fde08d313ea9773af06d8a0232673c9b47071a8 --- /dev/null +++ b/src/main/java/com/cm6123/monopoly/game/Board.java @@ -0,0 +1,68 @@ +package com.cm6123.monopoly.game; + +import java.util.HashMap; + +/** + * Represents the game board for the game. + */ +public class Board { + // Using a hashmap from w3schools.com/java/java_hashmap.asp + /** + * Calibrating hashmap with positions and tile names. + */ + public static final HashMap<Integer, String> GAME_BOARD = new HashMap<>(); + + /** + * add a constructor for the board so its creates it and then returns it. + */ + + public Board() { + createGameBoard(); + getGameBoard(); + } + // creating the hashmap with the position and name of the tiles + private void createGameBoard() { + GAME_BOARD.put(1, "Home"); + GAME_BOARD.put(2, "Road"); + GAME_BOARD.put(3, "Road"); + GAME_BOARD.put(4, "Old Kent Road"); + GAME_BOARD.put(5, "Pall Mall"); + GAME_BOARD.put(6, "Road"); + GAME_BOARD.put(7, "Paddington"); + GAME_BOARD.put(8, "Road"); + GAME_BOARD.put(9, "The Strand"); + GAME_BOARD.put(10, "Road"); + 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(15, "Park Lane"); + GAME_BOARD.put(16, "Road"); + } + + /** + * Returns the gameboard hashmap with all the names and positions. + * @return the GAME_BOARD hashmap + */ + public static HashMap<Integer, String> getGameBoard() { + return GAME_BOARD; + } + + /** + * Returns the name of tiles at the specified positions. + * @param position the position on the gameboard + * @return the name of the tile at the specified position + */ + public String getGameBoard(final int position) { + return GAME_BOARD.get(position); + } + + /** + * sets the tile name in the specified position. + * @param position the position on the game board + * @param name the name of the tile set + */ + public void printHashMap(final int position, final String name) { + GAME_BOARD.put(position, name); + } +} diff --git a/src/main/java/com/cm6123/monopoly/game/GameEndings.java b/src/main/java/com/cm6123/monopoly/game/GameEndings.java new file mode 100644 index 0000000000000000000000000000000000000000..00ce35713fd7b0c7091d159d2c26935f59061d24 --- /dev/null +++ b/src/main/java/com/cm6123/monopoly/game/GameEndings.java @@ -0,0 +1,49 @@ +package com.cm6123.monopoly.game; + + +import java.util.ArrayList; + +/** + * Represents the ways that the game can end and what would happen after. + */ +public final class GameEndings { + + private GameEndings() {} + + /** + * Checks if the specified round has reached or exceeded the maximum number of rounds. + * + * @param round The current round number. + * @param numberOfRounds The maximum number of rounds allowed. + * @return true if the round has reached or exceeded the maximum number of rounds, false otherwise. + */ + public static boolean checkRoundsReached (final int round, final int numberOfRounds){ + if (round >= numberOfRounds) { + System.out.println("Game over! Max rounds reached"); + return true; + } + return false; + } + + /** + * Determines the winner among the given list of players based on their money. + * Prints out the winner's name and money amount. + * + * @param players The list of players in the game. + */ + public static void determineWinner (final ArrayList<Player> players){ + Player winner = null; + int maxMoney = 0; + + for (Player player : players) { + if (player.getMoney() > maxMoney) { + maxMoney = player.getMoney(); + winner = player; + } + } + if (winner != null) { + System.out.println("The winner is... " + winner.getName() + " with £" + winner.getMoney() + "!!!!"); + } + } + } + diff --git a/src/main/java/com/cm6123/monopoly/game/Movement.java b/src/main/java/com/cm6123/monopoly/game/Movement.java new file mode 100644 index 0000000000000000000000000000000000000000..572d451b00d17f03d0d0d192158ba857ccf3faa6 --- /dev/null +++ b/src/main/java/com/cm6123/monopoly/game/Movement.java @@ -0,0 +1,44 @@ +package com.cm6123.monopoly.game; + +import com.cm6123.monopoly.dice.Dice; + +/** + * Represents the movement logic in a Monopoly game. + * This class manages the movement of players on the game board based on dice rolls. + */ +public class Movement { + + /** The size of the game board. */ + private static final int GAME_BOARD_SIZE = 16; + + + /** A die object used for rolling. */ + private final Dice dice = new Dice(6); + + + /** + * Simulates rolling two dice for a player and updates their position on the game board based on the dice roll result. + * + * @param player The player whose position will be updated. + */ + public void diceResult(final Player player) { + // Roll two dice + int roll1 = dice.roll(); + int roll2 = dice.roll(); + + // Calculate total dice result + int diceResult = roll1 + roll2; + System.out.println("You rolled " + diceResult); + + // Get current player position + int currentPosition = player.getPosition(); + + // Calculate new position on the game board based on dice result + int newPosition = (currentPosition + diceResult - 1) % GAME_BOARD_SIZE + 1; + + // Update player's position + player.setPosition(newPosition); + } + + +} diff --git a/src/main/java/com/cm6123/monopoly/game/Player.java b/src/main/java/com/cm6123/monopoly/game/Player.java new file mode 100644 index 0000000000000000000000000000000000000000..bf9c8fb4d5999b2fd28cdb253d291efe874cdfa5 --- /dev/null +++ b/src/main/java/com/cm6123/monopoly/game/Player.java @@ -0,0 +1,115 @@ +package com.cm6123.monopoly.game; + +import java.util.ArrayList; + +/** + * Represents a player in a Monopoly game. + * Players have a name, money, position, and can interact with the banker. + */ +public class Player { + + /** + * The name of the player. + */ + private String name; + + /** + * The amount of money the player has. + */ + private int money; + + /** + * The position of the player on the game board. + */ + private int position; + + + /** + * Constructs a new player with the specified name, initial money, and starting position. + * + * @param playerName The name of the player. + * @param playerMoney The initial amount of money the player has. + * @param playerPosition The starting position of the player on the game board. + */ + public Player(final String playerName, final int playerMoney, final int playerPosition) { + this.name = playerName; + this.money = playerMoney; + this.position = playerPosition; + } + + /** + * + * @param amount amount of money added to the players balance. + */ + public void addMoney(final int amount) { + this.money += amount; + } + + /** + * Retrieves the name of the player. + * + * @return The name of the player. + */ + public String getName() { + return name; + } + + /** + * Sets the name of the player. + * + * @param playerName + */ + public void setName(final String playerName) { + this.name = playerName; + } + + /** + * Retrieves the amount of money the player has. + * + * @return The amount of money the player has. + */ + public int getMoney() { + return money; + } + + /** + * Sets the amount of money the player has. + * + * @param playerMoney The new amount of money for the player. + */ + public void setMoney(final int playerMoney) { + this.money = playerMoney; + } + + + /** + * Retrieves the current position of the player on the game board. + * + * @return The current position of the player. + */ + public int getPosition() { + return position; + } + + /** + * Sets the current position of the player on the game board. + * + * @param playerPosition The new position for the player. + */ + public void setPosition(final int playerPosition) { + this.position = playerPosition; + } + + /** List of all players in the game. */ + public static final ArrayList<Player> PLAYERSINTHEGAME = new ArrayList<>(); + + /** + * Retrieves the list of all players in the game. + * + * @return The list of all players. + */ + public static ArrayList<Player> getPlayers() { + return PLAYERSINTHEGAME; + } + +} diff --git a/src/test/java/com/cm6123/monopoly/BankerTest.java b/src/test/java/com/cm6123/monopoly/BankerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5ad92a9092eac2be8376cece3e3c19356341dd33 --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/BankerTest.java @@ -0,0 +1,29 @@ +package com.cm6123.monopoly; + +import com.cm6123.monopoly.game.Banker; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class BankerTest { + + + @Test + public void testInitialBalance() { + Banker banker = new Banker(); // Create a new Banker + assertEquals(5000, banker.getBalance()); // Check the initial balance of the banker + } + + + @Test + public void testAddMoney() { + Banker banker = new Banker(); // Create a new Banker instance + int amountToAdd = 200; // Amount of money to add to the balance + + // Add money to the banker's balance + banker.addMoney(amountToAdd); + + // Check if the balance is updated correctly + assertEquals(5200, banker.getBalance()); + } +} + diff --git a/src/test/java/com/cm6123/monopoly/BoardTest.java b/src/test/java/com/cm6123/monopoly/BoardTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5a77b8aba8d5b3aa31c6350d50e4e8f89e848fa7 --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/BoardTest.java @@ -0,0 +1,35 @@ +package com.cm6123.monopoly; + + +import com.cm6123.monopoly.game.Board; +import com.cm6123.monopoly.game.Player; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BoardTest { + + @Test + public void testPlayerPositionChangeAndTileName() { + // Create a new player starting at position 1 + Player player = new Player("PlayerTest", 1000, 1); + + // Get player's initial position + int initialPosition = player.getPosition(); + Assertions.assertEquals(1, initialPosition); + + // Change player's position to 5 + player.setPosition(5); + + // Ensure the position has changed to 5 + int newPosition = player.getPosition(); + Assertions.assertEquals(5, newPosition); + + // Create an instance of Board + Board board = new Board(); + + // Retrieve the tile name at the new position using the Board instance + String tileName = board.getGameBoard(newPosition); + Assertions.assertEquals("Pall Mall", tileName); + } +} + diff --git a/src/test/java/com/cm6123/monopoly/GameEndingsTest.java b/src/test/java/com/cm6123/monopoly/GameEndingsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8a63157cd4204d3abb33fda42262886136603160 --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/GameEndingsTest.java @@ -0,0 +1,50 @@ +package com.cm6123.monopoly; + +import com.cm6123.monopoly.game.GameEndings; +import com.cm6123.monopoly.game.Player; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class GameEndingsTest { + + /** + * Tests the checkRoundsReached method of GameEndings. + * Verifies that the roundsReached flag is true when rounds reach the specified number of rounds. + */ + @Test + public void testRoundsReached() { + int rounds = 10; + int numberOfRounds = 10; + + // Check if rounds reached matches the specified number of rounds + boolean roundsReached = GameEndings.checkRoundsReached(rounds, numberOfRounds); + + // Assert that roundsReached is true + Assertions.assertTrue(roundsReached); + System.out.println("End game"); + } + + @Test + public void testDetermineWinner() { + // Create two players with different initial money + Player player1 = new Player("Player1", 1000, 0); + Player player2 = new Player("Player2", 2000, 0); + + // Add players to the game + ArrayList<Player> players = Player.getPlayers(); + players.add(player1); + players.add(player2); + + // Determine the winner + GameEndings.determineWinner(players); + + // Assert that the player with more money is identified as the winner + Player winner = players.stream().max((p1, p2) -> Integer.compare(p1.getMoney(), p2.getMoney())).orElse(null); + Assertions.assertNotNull(winner); + Assertions.assertEquals("Player2", winner.getName()); + System.out.println(winner.getName() + " is the winner"); + } +} + diff --git a/src/test/java/com/cm6123/monopoly/MovementTest.java b/src/test/java/com/cm6123/monopoly/MovementTest.java new file mode 100644 index 0000000000000000000000000000000000000000..adc0a919ac5ccca928791eeb0b4bd861dab2015d --- /dev/null +++ b/src/test/java/com/cm6123/monopoly/MovementTest.java @@ -0,0 +1,29 @@ +package com.cm6123.monopoly; + +import com.cm6123.monopoly.game.Player; +import com.cm6123.monopoly.game.Movement; +import org.junit.jupiter.api.Test; + + +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class MovementTest { + + @Test + public void testPlayerMovement() { + // Create a player with initial position + Player player = new Player("Alice", 1500, 1); + + // Initialize movement logic + Movement movement = new Movement(); + + // Get initial position + int initialPosition = player.getPosition(); + + // Test moving the player + movement.diceResult(player); + + // Ensure player's position has changed + assertNotEquals(initialPosition, player.getPosition()); + } +}