Skip to content
Snippets Groups Projects
Commit 8f69115e authored by Keegan Fernandes's avatar Keegan Fernandes Committed by Keegan Fernandes
Browse files

Added train stations where if a user lands on a train station the user rolls...

Added train stations where if a user lands on a train station the user rolls the die again and the result is multiplied by the ticket price and subtracted from the bank account and given to the banker.
parent 49a7bfc1
Branches
Tags stage-3 submission-3
No related merge requests found
Pipeline #57499 passed
...@@ -95,6 +95,10 @@ public final class Application { ...@@ -95,6 +95,10 @@ public final class Application {
} }
} }
if (newPosition == 7 || newPosition == 12 || newPosition == 14) {
gameHandler.landedOnTrainStation(currentPlayer);
}
if (newPosition == 1) { if (newPosition == 1) {
banker.checkIfPlayerPassHome(currentPlayer); banker.checkIfPlayerPassHome(currentPlayer);
System.out.println("Banker has given " + currentPlayer.getName() + " £200."); System.out.println("Banker has given " + currentPlayer.getName() + " £200.");
......
...@@ -20,7 +20,7 @@ public class Banker { ...@@ -20,7 +20,7 @@ public class Banker {
* *
* @param amount The amount of money to add. * @param amount The amount of money to add.
*/ */
public void addMoney(final int amount) { public void getMoneyFromPlayer(final int amount) {
balance += amount; balance += amount;
} }
......
...@@ -42,7 +42,7 @@ public class Board { ...@@ -42,7 +42,7 @@ public class Board {
GAME_BOARD.put(11, "Tax Office"); GAME_BOARD.put(11, "Tax Office");
GAME_BOARD.put(12, "Waterloo"); GAME_BOARD.put(12, "Waterloo");
GAME_BOARD.put(13, "Leicester Square"); 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(15, "Park Lane");
GAME_BOARD.put(16, "Road"); GAME_BOARD.put(16, "Road");
} }
......
package com.cm6123.monopoly.game; package com.cm6123.monopoly.game;
import com.cm6123.monopoly.dice.Dice;
import java.util.Scanner; import java.util.Scanner;
/** /**
...@@ -8,22 +10,23 @@ import java.util.Scanner; ...@@ -8,22 +10,23 @@ import java.util.Scanner;
public class GameAction implements GameHandler { 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; 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) { public GameAction(final Board gameboard) {
this.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 * @param player the player who landed on the property
*/ */
...@@ -34,22 +37,80 @@ public class GameAction implements GameHandler { ...@@ -34,22 +37,80 @@ public class GameAction implements GameHandler {
// Check if the property at the current position can be bought // Check if the property at the current position can be bought
if (this.board.canBuyProperty(position)) { if (this.board.canBuyProperty(position)) {
String propertyName = this.board.getGameBoard(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); 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(); String input = scanner.next().toUpperCase();
// if Player chooses to buy the property check if the player has enough to buy it // 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 (input.equals("Y")) {
if (player.getMoney() >= 200) { if (player.getMoney() >= 200) {
this.board.buyProperty(position, player); this.board.buyProperty(position, player);
break;
} else { } else {
System.out.println("You do not have enough money to buy " + propertyName + ". Moving on..."); System.out.println("You do not have enough money to buy " + propertyName + ". Moving on...");
} // if player doesn't want to buy it move 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")) { } else if (input.equals("N")) {
System.out.println("You have chosen N, moving on..."); System.out.println("You have chosen N, moving on...");
} else { // if user enters something else skip the turn break;
System.out.println("Invalid input. Skipping turn"); } 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 (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'");
} }
} }
} }
......
...@@ -8,8 +8,15 @@ public interface GameHandler { ...@@ -8,8 +8,15 @@ public interface GameHandler {
/** /**
* Triggers when player lands on a property. * 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); 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);
} }
...@@ -15,6 +15,7 @@ public class Movement { ...@@ -15,6 +15,7 @@ public class Movement {
private final Dice dice = new Dice(6); 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. * Player asked to roll die and the result is added up (diceResult) for the sum of both dies rolled.
* *
......
...@@ -20,7 +20,7 @@ public class BankerTest { ...@@ -20,7 +20,7 @@ public class BankerTest {
int amountToAdd = 200; // Amount of money to add to the balance int amountToAdd = 200; // Amount of money to add to the balance
// Add money to the banker's balance // Add money to the banker's balance
banker.addMoney(amountToAdd); banker.getMoneyFromPlayer(amountToAdd);
// Check if the balance is icnreased by 200 // Check if the balance is icnreased by 200
assertEquals(5200, banker.getBalance()); assertEquals(5200, banker.getBalance());
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment