fixed move logic

This commit is contained in:
Johannes Schmelz 2024-11-29 16:44:28 +01:00
parent 6cdad71693
commit 86ece3b70d
2 changed files with 48 additions and 51 deletions

View File

@ -152,29 +152,18 @@ public class Player implements FieldVisitor<Void>{
} }
/** /**
* Moves by the specified amount of steps * Moves the player by a given number of steps, handling board wrapping.
* @param steps the number of steps to move *
* @param steps number of steps to move
* @return the new position * @return the new position
*/ */
public int move(int steps){ public int move(int steps) {
return movePos(fieldID+steps); fieldID = (fieldID + steps) % 40;
} if (fieldID + steps >= 40) {
earnMoney(2000); // Passing GO gives money
/**
* 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);
} }
figure.moveTo(fieldID); figure.moveTo(fieldID);
getHandler().getLogic().send(this, new PlayerStatusUpdate(handler)); handler.getLogic().send(this, new PlayerStatusUpdate(handler));
handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this); handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this);
return fieldID; return fieldID;
} }
@ -452,29 +441,37 @@ public class Player implements FieldVisitor<Void>{
} }
} }
/** /**
* Rolls two dice and returns a list with the results. * Rolls two dice, handles movement logic, and checks for doublets.
* *
* @return a List of two integers representing the dice roll results * @return the DiceResult of the roll
*/ */
DiceResult rollDice() { DiceResult rollDice() {
if (mayRollDice) { if (!mayRollDice) {
state.rollDice(); throw new IllegalStateException("Player is not allowed to roll dice at this moment.");
}
if (rollResult.isDoublets()) {
doubletscounter++;
mayRollDice = true;
getHandler().getLogic().send(this, new NextPlayerTurn());
setName(name);
if (doubletscounter >= 3) {
setPosition(10);
}
} else {
mayRollDice = false;
}
return rollResult;
} }
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 * A interface representing the PlayerStates
*/ */

View File

@ -189,7 +189,7 @@ public class DeckHelper{
} }
private void spoparty(Player player) { private void spoparty(Player player) {
player.movePos(14); player.setPosition(14);
} }
private void gulakFrei(Player player) { private void gulakFrei(Player player) {
@ -197,11 +197,11 @@ public class DeckHelper{
} }
private void dienstfuehrerschein(Player player) { private void dienstfuehrerschein(Player player) {
player.movePos(20); player.setPosition(20);
} }
private void pubquiz(Player player) { private void pubquiz(Player player) {
player.movePos(39); player.setPosition(39);
} }
private void namensschildTruppenkueche(Player player) { private void namensschildTruppenkueche(Player player) {
@ -216,11 +216,11 @@ public class DeckHelper{
} }
private void pruefungsphaseKrank(Player player) { private void pruefungsphaseKrank(Player player) {
player.movePos(player.getFieldID() - 3); player.setPosition(player.getFieldID() - 3);
} }
private void naechstesMonatsgehalt(Player player) { private void naechstesMonatsgehalt(Player player) {
player.movePos(0); player.setPosition(0);
} }
private void antretenVerschlafen(Player player) { private void antretenVerschlafen(Player player) {
@ -237,20 +237,20 @@ public class DeckHelper{
private void dienstsportGym(Player player) { private void dienstsportGym(Player player) {
for (Player p : player.getHandler().getPlayers()) { for (Player p : player.getHandler().getPlayers()) {
p.movePos(1); p.setPosition(1);
} }
} }
private void schimmelGulak(Player player) { private void schimmelGulak(Player player) {
player.movePos(10); player.setPosition(10);
} }
private void partynachtGulak(Player player) { private void partynachtGulak(Player player) {
player.movePos(10); player.setPosition(10);
} }
private void jahresabschlussantreten(Player player) { private void jahresabschlussantreten(Player player) {
player.movePos(17); player.setPosition(17);
} }
private void verkaufenVersicherungen(Player player) { private void verkaufenVersicherungen(Player player) {
@ -263,7 +263,7 @@ public class DeckHelper{
private void hausfeierSturz(Player player) { private void hausfeierSturz(Player player) {
player.pay(800); player.pay(800);
player.movePos(32); player.setPosition(32);
} }
private void befoerderung(Player player) { private void befoerderung(Player player) {
@ -292,7 +292,7 @@ public class DeckHelper{
} }
private void kranzExmatrikulation(Player player) { private void kranzExmatrikulation(Player player) {
player.movePos(5); player.setPosition(5);
} }
private void partyEskaliert(Player player) { private void partyEskaliert(Player player) {