added the method tryMove and the methods used by it into serverstate

This commit is contained in:
Hanno Fleischer
2024-11-17 17:52:25 +01:00
parent 9662e1f684
commit 90fb6e4133
4 changed files with 174 additions and 1 deletions

View File

@@ -1,9 +1,16 @@
package pp.mdga.server;
import pp.mdga.game.Color;
import pp.mdga.game.Node;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.game.PlayerData;
import pp.mdga.message.client.*;
import pp.mdga.message.server.PossibleCard;
import java.lang.System.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class ServerState implements Observer {
protected static final Logger LOGGER = System.getLogger(ServerState.class.getName());
@@ -58,6 +65,126 @@ public ServerState getParent() {
public void update() { /* do nothing */ }
/**
* This method is used to calculate the steps a piece can move
*
* @return the steps a piece can move
*/
private int calculateSteps(){
return logic.getGame().getDiceEyes() * logic.getGame().getDiceModifier();
}
/**
* This method is used to test if u can move a piece
*
* @param piece the piece to be moved
* @return true if the piece can be moved, false otherwise
*/
protected boolean tryMove(Piece piece) {
if (piece.getState() == PieceState.HOME){
return tryHomeMove(piece, calculateSteps());
} else {
int homeMoves = getHomeMoves(piece, calculateSteps());
if (homeMoves > 0){
return tryHomeMove(piece, homeMoves);
} else {
return tryInfieldMove(piece, calculateSteps());
}
}
}
/**
* This method is used to determine if a piece would move into the home area.
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return the number of steps the piece would move into the home area
*/
protected int getHomeMoves(Piece piece, int steps) {
int figureIndex = logic.getGame().getBoard().getInfieldIndexOfPiece(piece);
Color col = piece.getColor();
int startIndex = logic.getGame().getBoard().getPlayerData().get(col).getStartNodeIndex();
int moveIndex = startIndex + steps;
if (moveIndex > logic.getGame().getBoard().getInfield().length){
moveIndex %= logic.getGame().getBoard().getInfield().length;
if(moveIndex >= startIndex){
return moveIndex - startIndex + 1;
}
} else if (figureIndex < startIndex && moveIndex >= startIndex){
return moveIndex - startIndex + 1;
}
return 0;
}
/**
* This method is used to determine if a piece can move in the infield
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return true if the piece can move in the infield, false otherwise
*/
protected boolean tryInfieldMove(Piece piece, int steps){
int figureIndex = logic.getGame().getBoard().getInfieldIndexOfPiece(piece);
int moveIndex = (figureIndex + steps) % logic.getGame().getBoard().getInfield().length;
Piece occupant = logic.getGame().getBoard().getInfield()[moveIndex].getOccupant();
if(occupant != null){
return occupant.getColor() != piece.getColor();
}
return true;
}
/**
* This method is used to determine if a piece can move inside the home area
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return true if the piece can move into the home area, false otherwise
*/
protected boolean tryHomeMove(Piece piece, int steps){
Color col = piece.getColor();
PlayerData playerData = logic.getGame().getBoard().getPlayerData().get(col);
Node[] homeNodes = playerData.getHomeNodes();
int index;
if (playerData.homeIncludes(piece)){
index = playerData.getIndexInHome(piece);
} else {
index = 0;
}
if(index + steps >= homeNodes.length){
return false;
} else {
for(int i = index; i <= index + steps; i++){
if(homeNodes[i].getOccupant() != null){
return false;
}
}
return true;
}
}
/**
* This method is used to get the pieces that can be moved
*
* @param color the color of the pieces
* @return the pieces that can be moved
*/
protected List<Piece> getMoveablePieces(Color color) {
ArrayList<Piece> moveablePieces = new ArrayList<>();
ArrayList<Piece> pieces = new ArrayList<>();
for (Piece piece : logic.getGame().getBoard().getPlayerData().get(color).getPieces()) {
if(piece.getState() == PieceState.ACTIVE || piece.getState() == PieceState.HOME){
pieces.add(piece);
}
}
for (Piece piece : pieces) {
if (tryMove(piece)) {
moveablePieces.add(piece);
}
}
return moveablePieces;
}
@Override
public String toString() {
return getClass().getSimpleName();