From 86ece3b70d7ca53f2fc38eb2145b201b06e7a311 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Fri, 29 Nov 2024 16:44:28 +0100 Subject: [PATCH] fixed move logic --- .../java/pp/monopoly/game/server/Player.java | 77 +++++++++---------- .../pp/monopoly/model/card/DeckHelper.java | 22 +++--- 2 files changed, 48 insertions(+), 51 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 ca911f8..219cea0 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 @@ -152,29 +152,18 @@ public class Player implements FieldVisitor{ } /** - * Moves by the specified amount of steps - * @param steps the number of steps to move + * Moves the player by a given number of steps, handling board wrapping. + * + * @param steps number of steps to move * @return the new position */ - public int move(int steps){ - return movePos(fieldID+steps); - } - - /** - * Moves the player to the specified Position on the board - * @param position the position to move to - * @return the new position - */ - public int movePos(int position){ - fieldID = fieldID+position; - System.out.println("Aktuelle Position" +fieldID); - System.err.println("Würfelergebniss"+ rollResult.calcTotal()); - if(fieldID >= 40) { - fieldID = fieldID%40; - earnMoney(2000); + public int move(int steps) { + fieldID = (fieldID + steps) % 40; + if (fieldID + steps >= 40) { + earnMoney(2000); // Passing GO gives money } figure.moveTo(fieldID); - getHandler().getLogic().send(this, new PlayerStatusUpdate(handler)); + handler.getLogic().send(this, new PlayerStatusUpdate(handler)); handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this); return fieldID; } @@ -452,29 +441,37 @@ public class Player implements FieldVisitor{ } } - /** - * Rolls two dice and returns a list with the results. - * - * @return a List of two integers representing the dice roll results - */ - DiceResult rollDice() { - if (mayRollDice) { - state.rollDice(); - } - if (rollResult.isDoublets()) { - doubletscounter++; - mayRollDice = true; - getHandler().getLogic().send(this, new NextPlayerTurn()); - setName(name); - if (doubletscounter >= 3) { - setPosition(10); - } - } else { - mayRollDice = false; - } - return rollResult; +/** + * Rolls two dice, handles movement logic, and checks for doublets. + * + * @return the DiceResult of the roll + */ +DiceResult rollDice() { + if (!mayRollDice) { + throw new IllegalStateException("Player is not allowed to roll dice at this moment."); } + rollResult = state.rollDice(); + mayRollDice = false; + + if (rollResult.isDoublets()) { + doubletscounter++; + mayRollDice = true; // Allow another roll for doublets + + if (doubletscounter >= 3) { + setPosition(10); // Send to jail on third doublet + doubletscounter = 0; // Reset doublets counter + } else { + getHandler().getLogic().send(this, new NextPlayerTurn()); + } + } else { + doubletscounter = 0; // Reset counter if no doublets + } + + return rollResult; +} + + /** * A interface representing the PlayerStates */ diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/card/DeckHelper.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/card/DeckHelper.java index 50aa77f..7bca666 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/card/DeckHelper.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/card/DeckHelper.java @@ -189,7 +189,7 @@ public class DeckHelper{ } private void spoparty(Player player) { - player.movePos(14); + player.setPosition(14); } private void gulakFrei(Player player) { @@ -197,11 +197,11 @@ public class DeckHelper{ } private void dienstfuehrerschein(Player player) { - player.movePos(20); + player.setPosition(20); } private void pubquiz(Player player) { - player.movePos(39); + player.setPosition(39); } private void namensschildTruppenkueche(Player player) { @@ -216,11 +216,11 @@ public class DeckHelper{ } private void pruefungsphaseKrank(Player player) { - player.movePos(player.getFieldID() - 3); + player.setPosition(player.getFieldID() - 3); } private void naechstesMonatsgehalt(Player player) { - player.movePos(0); + player.setPosition(0); } private void antretenVerschlafen(Player player) { @@ -237,20 +237,20 @@ public class DeckHelper{ private void dienstsportGym(Player player) { for (Player p : player.getHandler().getPlayers()) { - p.movePos(1); + p.setPosition(1); } } private void schimmelGulak(Player player) { - player.movePos(10); + player.setPosition(10); } private void partynachtGulak(Player player) { - player.movePos(10); + player.setPosition(10); } private void jahresabschlussantreten(Player player) { - player.movePos(17); + player.setPosition(17); } private void verkaufenVersicherungen(Player player) { @@ -263,7 +263,7 @@ public class DeckHelper{ private void hausfeierSturz(Player player) { player.pay(800); - player.movePos(32); + player.setPosition(32); } private void befoerderung(Player player) { @@ -292,7 +292,7 @@ public class DeckHelper{ } private void kranzExmatrikulation(Player player) { - player.movePos(5); + player.setPosition(5); } private void partyEskaliert(Player player) {