implemented PlayerStates

This commit is contained in:
Johannes Schmelz 2024-11-16 18:42:05 +01:00
parent 628b98af9b
commit d2e0b8187b
3 changed files with 187 additions and 45 deletions

View File

@ -8,9 +8,11 @@
package pp.monopoly.game.server; package pp.monopoly.game.server;
import java.util.List; import java.util.List;
import java.util.Random;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.FieldVisitor;
import pp.monopoly.model.Figure; import pp.monopoly.model.Figure;
import pp.monopoly.model.card.DeckHelper; import pp.monopoly.model.card.DeckHelper;
@ -37,8 +39,9 @@ public class Player implements FieldVisitor<Void>{
private List<PropertyField> properties; private List<PropertyField> properties;
private int getOutOfJailCard; private int getOutOfJailCard;
private int fieldID; private int fieldID;
private int rollResult; private DiceResult rollResult;
private PlayerHandler handler; private PlayerHandler handler;
private PlayerState state = new LobbyState();
public Player(int id, String name, PlayerHandler handler) { public Player(int id, String name, PlayerHandler handler) {
this.name = name; this.name = name;
@ -74,10 +77,6 @@ public class Player implements FieldVisitor<Void>{
return fieldID = (fieldID+position)%40; return fieldID = (fieldID+position)%40;
} }
public void setRollResult(int rollResult) {
this.rollResult = rollResult;
}
public List<PropertyField> getProperties() { public List<PropertyField> getProperties() {
return properties; return properties;
} }
@ -122,6 +121,14 @@ public class Player implements FieldVisitor<Void>{
getOutOfJailCard--; getOutOfJailCard--;
} }
public void payBail() {
state.payBail();
}
public void useJailCard() {
state.useJailCard();
}
@Override @Override
public Void visit(BuildingProperty field) { public Void visit(BuildingProperty field) {
int rent = field.calcRent(); int rent = field.calcRent();
@ -137,8 +144,8 @@ public class Player implements FieldVisitor<Void>{
if (field.getOwner().getNumProp(field) == 2) { if (field.getOwner().getNumProp(field) == 2) {
factor = 10; factor = 10;
} }
field.getOwner().earnMoney(rollResult*factor); field.getOwner().earnMoney(rollResult.calcTotal()*factor);
pay(rollResult*factor); pay(rollResult.calcTotal()*factor);
return null; return null;
} }
@ -153,8 +160,8 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public Void visit(GulagField field) { public Void visit(GulagField field) {
// TODO Auto-generated method stub //do nothing
throw new UnsupportedOperationException("Unimplemented method 'visit'"); return null;
} }
@Override @Override
@ -172,7 +179,7 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public Void visit(WacheField field) { public Void visit(WacheField field) {
movePos(10); movePos(10);
state = new JailState();
return null; return null;
} }
@ -203,4 +210,170 @@ public class Player implements FieldVisitor<Void>{
return count; 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<Integer> 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<Integer> 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'");
}
}
} }

View File

@ -40,32 +40,6 @@ public class ServerGameLogic implements ClientInterpreter {
this.config = config; 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<Integer> rollDice() {
return List.of(Dice.rollDice(), Dice.rollDice());
}
/** /**
* Retrieves the current state of the game. * Retrieves the current state of the game.
* *
@ -219,15 +193,7 @@ public class ServerGameLogic implements ClientInterpreter {
public void received(RollDice msg, int from) { public void received(RollDice msg, int from) {
Player player = getPlayerById(from); Player player = getPlayerById(from);
if (player != null && state == ServerState.INGAME) { if (player != null && state == ServerState.INGAME) {
List<Integer> rollResults = rollDice(); send(player, player.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));
} }
} }

View File

@ -29,4 +29,7 @@ public class DiceResult extends ServerMessage{
return rollResult.get(0) == rollResult.get(1); return rollResult.get(0) == rollResult.get(1);
} }
public int calcTotal() {
return rollResult.get(0)+rollResult.get(1);
}
} }