From d2e0b8187bbabec4ce7d2f75dc099bed5d8a8541 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Sat, 16 Nov 2024 18:42:05 +0100 Subject: [PATCH] implemented PlayerStates --- .../java/pp/monopoly/game/server/Player.java | 193 +++++++++++++++++- .../monopoly/game/server/ServerGameLogic.java | 36 +--- .../monopoly/message/server/DiceResult.java | 3 + 3 files changed, 187 insertions(+), 45 deletions(-) diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java index 9426dd2..56d6731 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java @@ -8,9 +8,11 @@ package pp.monopoly.game.server; import java.util.List; +import java.util.Random; import com.jme3.math.ColorRGBA; +import pp.monopoly.message.server.DiceResult; import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.Figure; import pp.monopoly.model.card.DeckHelper; @@ -37,8 +39,9 @@ public class Player implements FieldVisitor{ private List properties; private int getOutOfJailCard; private int fieldID; - private int rollResult; + private DiceResult rollResult; private PlayerHandler handler; + private PlayerState state = new LobbyState(); public Player(int id, String name, PlayerHandler handler) { this.name = name; @@ -74,10 +77,6 @@ public class Player implements FieldVisitor{ return fieldID = (fieldID+position)%40; } - public void setRollResult(int rollResult) { - this.rollResult = rollResult; - } - public List getProperties() { return properties; } @@ -122,6 +121,14 @@ public class Player implements FieldVisitor{ getOutOfJailCard--; } + public void payBail() { + state.payBail(); + } + + public void useJailCard() { + state.useJailCard(); + } + @Override public Void visit(BuildingProperty field) { int rent = field.calcRent(); @@ -137,8 +144,8 @@ public class Player implements FieldVisitor{ if (field.getOwner().getNumProp(field) == 2) { factor = 10; } - field.getOwner().earnMoney(rollResult*factor); - pay(rollResult*factor); + field.getOwner().earnMoney(rollResult.calcTotal()*factor); + pay(rollResult.calcTotal()*factor); return null; } @@ -153,8 +160,8 @@ public class Player implements FieldVisitor{ @Override public Void visit(GulagField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + //do nothing + return null; } @Override @@ -172,7 +179,7 @@ public class Player implements FieldVisitor{ @Override public Void visit(WacheField field) { movePos(10); - + state = new JailState(); return null; } @@ -203,4 +210,170 @@ public class Player implements FieldVisitor{ return count; } + + /** + * Inner class for dice functionality in the game. + * Rolls random dice values. + */ + private class Dice { + private static Random random = new Random(); + + /** + * Rolls a single die and returns a random value from 1 to 6. + * + * @return the result of a dice roll (1 to 6) + */ + private static int rollDice() { + return random.nextInt(6) + 1; + } + } + + /** + * Rolls two dice and returns a list with the results. + * + * @return a List of two integers representing the dice roll results + */ + DiceResult rollDice() { + return state.rollDice(); + } + + /** + * A interface representing the PlayerStates + */ + private interface PlayerState { + DiceResult rollDice(); + void payBail(); + void useJailCard(); + } + + + /** + * Class to represent the Active PlayerState + * This class is set when it is the Players turn to do actions + */ + private class ActiveState implements PlayerState { + + @Override + public DiceResult rollDice() { + List roll = List.of(Dice.rollDice(), Dice.rollDice()); + rollResult = new DiceResult(roll); + return rollResult; + } + + @Override + public void payBail() { + // do nothing + } + + @Override + public void useJailCard() { + // do nothings + } + } + + + /** + * A class to represent the Lobby PlayerState + * Set when in Lobby + */ + private class LobbyState implements PlayerState{ + + @Override + public DiceResult rollDice() { + //do nothing + return null; + } + + @Override + public void payBail() { + //do nothing + } + + @Override + public void useJailCard() { + // do nothing + } + + } + + + /** + * A class to represent the Jailed PlayerState + * Set when in Gulag + */ + private class JailState implements PlayerState { + + private int DoubletsCounter = 3; + + @Override + public DiceResult rollDice() { + List roll = List.of(Dice.rollDice(), Dice.rollDice()); + rollResult = new DiceResult(roll); + if (rollResult.isDoublets()) { + state = new ActiveState(); + } else if (DoubletsCounter == 0) { + + } else { + DoubletsCounter--; + } + return rollResult; + } + + @Override + public void payBail() { + pay(500); + state = new ActiveState(); + } + + @Override + public void useJailCard() { + getOutOfJailCard--; + state = new ActiveState(); + } + + } + + private class BankruptState implements PlayerState { + + @Override + public DiceResult rollDice() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'rollDice'"); + } + + @Override + public void payBail() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'payBail'"); + } + + @Override + public void useJailCard() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'useJailCard'"); + } + + } + + private class WaitForTurnState implements PlayerState { + + @Override + public DiceResult rollDice() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'rollDice'"); + } + + @Override + public void payBail() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'payBail'"); + } + + @Override + public void useJailCard() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'useJailCard'"); + } + + } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java index 0c89d40..53a741d 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java @@ -40,32 +40,6 @@ public class ServerGameLogic implements ClientInterpreter { this.config = config; } - /** - * Inner class for dice functionality in the game. - * Rolls random dice values. - */ - private class Dice { - private static Random random = new Random(); - - /** - * Rolls a single die and returns a random value from 1 to 6. - * - * @return the result of a dice roll (1 to 6) - */ - private static int rollDice() { - return random.nextInt(6) + 1; - } - } - - /** - * Rolls two dice and returns a list with the results. - * - * @return a List of two integers representing the dice roll results - */ - List rollDice() { - return List.of(Dice.rollDice(), Dice.rollDice()); - } - /** * Retrieves the current state of the game. * @@ -219,15 +193,7 @@ public class ServerGameLogic implements ClientInterpreter { public void received(RollDice msg, int from) { Player player = getPlayerById(from); if (player != null && state == ServerState.INGAME) { - List rollResults = rollDice(); - int rollSum = rollResults.get(0) + rollResults.get(1); - player.setRollResult(rollSum); - - player.move(rollSum); - - LOGGER.log(Level.INFO, "Player {0} rolled {1} and moved to position {2}", player.getName(), rollSum, player.getFieldID()); - - send(player, new DiceResult(rollResults)); + send(player, player.rollDice()); } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java index 53a137e..65a8223 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java @@ -29,4 +29,7 @@ public class DiceResult extends ServerMessage{ return rollResult.get(0) == rollResult.get(1); } + public int calcTotal() { + return rollResult.get(0)+rollResult.get(1); + } }