added the method tryMove and the methods used by it into serverstate
This commit is contained in:
		@@ -57,4 +57,13 @@ public Node[] getInfield() {
 | 
			
		||||
    public void setPieceOnBoard(int index, Piece piece) {
 | 
			
		||||
        infield[index].setOccupant(piece);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getInfieldIndexOfPiece(Piece piece) {
 | 
			
		||||
        for (int i = 0; i < infield.length; i++) {
 | 
			
		||||
            if (infield[i].getOccupant() == piece) {
 | 
			
		||||
                return i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@
 | 
			
		||||
public class Piece {
 | 
			
		||||
    private ShieldState shield;
 | 
			
		||||
    private PieceState state;
 | 
			
		||||
    private Color color;
 | 
			
		||||
    private final Color color;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This constructor is used to create a new Piece
 | 
			
		||||
@@ -73,4 +73,13 @@ public boolean isShielded() {
 | 
			
		||||
    public boolean isSuppressed() {
 | 
			
		||||
        return shield == ShieldState.SUPPRESSED;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method is used to get the color of the piece
 | 
			
		||||
     *
 | 
			
		||||
     * @return the color of the piece
 | 
			
		||||
     */
 | 
			
		||||
    public Color getColor() {
 | 
			
		||||
        return color;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -108,4 +108,32 @@ public Piece removePieceFromWaitingArea() {
 | 
			
		||||
    public void setPieceInHome(int index, Piece piece) {
 | 
			
		||||
        homeNodes[index].setOccupant(piece);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method returns the homeNodes
 | 
			
		||||
     *
 | 
			
		||||
     * @return the homeNodes
 | 
			
		||||
     */
 | 
			
		||||
    public boolean homeIncludes(Piece piece) {
 | 
			
		||||
        for (int i = 0; i < 4; i++) {
 | 
			
		||||
            if (homeNodes[i].getOccupant() == piece) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This method returns the homeNodes
 | 
			
		||||
     *
 | 
			
		||||
     * @return the homeNodes
 | 
			
		||||
     */
 | 
			
		||||
    public int getIndexInHome(Piece piece) {
 | 
			
		||||
        for (int i = 0; i < 4; i++) {
 | 
			
		||||
            if (homeNodes[i].getOccupant() == piece) {
 | 
			
		||||
                return i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user