From 39b9fdc35f610b241badd11b319b7b31de33ca42 Mon Sep 17 00:00:00 2001
From: c23056305 <FernandesK3@cardiff.ac.uk>
Date: Wed, 1 May 2024 20:02:30 +0100
Subject: [PATCH] added navigation/taking turns/declaring winner (stage1) added
 test files too

---
 monopoly.30-Apr.log.gz                        | Bin 0 -> 350 bytes
 .../com/cm6123/monopoly/app/Application.java  |  80 ++++++++++++
 .../java/com/cm6123/monopoly/game/Banker.java |  66 ++++++++++
 .../java/com/cm6123/monopoly/game/Board.java  |  68 +++++++++++
 .../com/cm6123/monopoly/game/GameEndings.java |  49 ++++++++
 .../com/cm6123/monopoly/game/Movement.java    |  44 +++++++
 .../java/com/cm6123/monopoly/game/Player.java | 115 ++++++++++++++++++
 .../java/com/cm6123/monopoly/BankerTest.java  |  29 +++++
 .../java/com/cm6123/monopoly/BoardTest.java   |  35 ++++++
 .../com/cm6123/monopoly/GameEndingsTest.java  |  50 ++++++++
 .../com/cm6123/monopoly/MovementTest.java     |  29 +++++
 11 files changed, 565 insertions(+)
 create mode 100644 monopoly.30-Apr.log.gz
 create mode 100644 src/main/java/com/cm6123/monopoly/game/Banker.java
 create mode 100644 src/main/java/com/cm6123/monopoly/game/Board.java
 create mode 100644 src/main/java/com/cm6123/monopoly/game/GameEndings.java
 create mode 100644 src/main/java/com/cm6123/monopoly/game/Movement.java
 create mode 100644 src/main/java/com/cm6123/monopoly/game/Player.java
 create mode 100644 src/test/java/com/cm6123/monopoly/BankerTest.java
 create mode 100644 src/test/java/com/cm6123/monopoly/BoardTest.java
 create mode 100644 src/test/java/com/cm6123/monopoly/GameEndingsTest.java
 create mode 100644 src/test/java/com/cm6123/monopoly/MovementTest.java

diff --git a/monopoly.30-Apr.log.gz b/monopoly.30-Apr.log.gz
new file mode 100644
index 0000000000000000000000000000000000000000..79d9d61c4cac3f3de967d9dec6f3009c8b6530e4
GIT binary patch
literal 350
zcmV-k0ipgMiwFP!00000|DBb=Y6CG0MDK<EgMT1kN%pQ+w_FOnw1<8{NK3j9l8`L)
z_d9k^1w%G-+k+pYl}00tnl>?QECvzJ#qsg__VOOW_HsGS``t31PvK+PU6-%V@G~#p
zVRt>;{P{^&k^AHMHlGgvpQ}spI1oaOwCN$C)O)@{XzG{P6-pk9s}VwtOpgdno-!vi
zd0ySR>``2q88tG?dLbEHde83*$>?#UNG&c*7c*w%l30>z=-|=ZI8sQr3~tJG2;>Bq
zBGI8#2y(3dX^e+PPwJWJEy(J0j+|J{3=c7y$e7}p*%e@95HynVZ6gy?EkZl?<MckM
zk-L5%&4qD1meaWX1esu6j)aE@Ql(u}qFSv6Ld<jI=tg9MYMlyml7S&_h0z3=xZZ-y
wZiM5j^^kqUMXK$Iea+sX{(GK@1*s&aL6&G#$O1F?b?x|n0k52D{|5^I07Vq5mH+?%

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 4c906df..1db2187 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 0000000..338aacb
--- /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 0000000..4fde08d
--- /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 0000000..00ce357
--- /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 0000000..572d451
--- /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 0000000..bf9c8fb
--- /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 0000000..5ad92a9
--- /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 0000000..5a77b8a
--- /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 0000000..8a63157
--- /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 0000000..adc0a91
--- /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());
+    }
+}
-- 
GitLab