mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-24 20:29:44 +01:00
implemented PlayerStates
This commit is contained in:
parent
628b98af9b
commit
d2e0b8187b
@ -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<Void>{
|
||||
private List<PropertyField> 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<Void>{
|
||||
return fieldID = (fieldID+position)%40;
|
||||
}
|
||||
|
||||
public void setRollResult(int rollResult) {
|
||||
this.rollResult = rollResult;
|
||||
}
|
||||
|
||||
public List<PropertyField> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
@ -122,6 +121,14 @@ public class Player implements FieldVisitor<Void>{
|
||||
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<Void>{
|
||||
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<Void>{
|
||||
|
||||
@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<Void>{
|
||||
@Override
|
||||
public Void visit(WacheField field) {
|
||||
movePos(10);
|
||||
|
||||
state = new JailState();
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -203,4 +210,170 @@ public class Player implements FieldVisitor<Void>{
|
||||
|
||||
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'");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<Integer> 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<Integer> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user