Escape-sprowe / src / escape / Board.java
Board.java
Raw
package escape;

import escape.enumerations.Player;
import escape.initializers.LocationInitializer;
import escape.initializers.PieceTypeDescriptor;
import escape.interfaces.Coordinate;
import escape.interfaces.EscapePiece;
import escape.enumerations.LocationType;
import escape.util.*;

import java.util.ArrayList;
import java.util.List;

import static escape.interfaces.EscapePiece.PieceAttributeID.*;
import static escape.enumerations.LocationType.CLEAR;

public class Board {
    private int maxX = 0;
    private int maxY = 0;
    private Coordinate.CoordinateType boardType;
    private List<Location> importantLocs;

    public Board(){
        importantLocs = new ArrayList<>();
    }

    public void setMaxX(int maxX){
        if(maxX <= 0){
            this.maxX = Integer.MAX_VALUE;
        }
        else {
            this.maxX = maxX;
        }
    }

    public void setMaxY(int maxY) {
        if(maxY <= 0){
            this.maxY = Integer.MAX_VALUE;
        }
        else {
            this.maxY = maxY;
        }
    }

    public void setBoardType(Coordinate.CoordinateType boardType) {
        this.boardType = boardType;
    }


    /**
     * Sets all locations with either pieces or non-CLEAR type
     * @param locs
     * @param pieceTypes
     */
    public void setImportantLocs(LocationInitializer[] locs, PieceTypeDescriptor[] pieceTypes) {

        for(LocationInitializer loc : locs){
            Location temp = new Location(0,0);
            temp.setX(loc.x);
            temp.setY(loc.y);
            temp.setLocationType(loc.locationType);


            if(loc.pieceName != null && pieceTypes != null){
                GamePiece tempPiece = new GamePiece();
                tempPiece.setPlayer(loc.player);
                tempPiece.setPieceName(loc.pieceName);
                tempPiece.setAttributes(getAttributes(loc.pieceName,pieceTypes));
                tempPiece.setMovementPattern(getMovement(loc.pieceName,pieceTypes));
                temp.setPiece(tempPiece);
            }


            importantLocs.add(temp);
        }
    }

    /**
     * Check pieceTypes array to obtain attributes
     * @param pieceName
     * @param pieceTypes
     * @return Array of PieceAttributes
     */
    private PieceAttribute[] getAttributes(EscapePiece.PieceName pieceName, PieceTypeDescriptor[] pieceTypes){
        for (PieceTypeDescriptor pieceType : pieceTypes) {
            if (pieceType.getPieceName().equals(pieceName)) {
                return pieceType.getAttributes();
            }
        }
        return null;
    }

    /**
     * Get Movement pattern of a given piece, defaults to OMNI
     * @param pieceName
     * @param pieceTypes
     * @return MovementPattern of given EscapePiece
     */
    private EscapePiece.MovementPattern getMovement(EscapePiece.PieceName pieceName, PieceTypeDescriptor[] pieceTypes){
        for (PieceTypeDescriptor pieceType : pieceTypes) {
            if (pieceType.getPieceName().equals(pieceName)) {
                return pieceType.getMovementPattern();
            }
        }
        return EscapePiece.MovementPattern.OMNI;
    }

    public boolean playerHasPieces(Player player){
        for (Location loc : importantLocs) {
            if (loc.getPiece() != null && loc.getPiece().getPlayer() == player) {
                return true;
            }
        }
        return false;
    }


    public int getMaxX() {
        return maxX;
    }

    public int getMaxY() {
        return maxY;
    }

    public Coordinate.CoordinateType getBoardType() {
        return boardType;
    }


    public GamePiece pieceAt(Location loc){
        for (Location location : importantLocs) {
            if (location.getX() == loc.getX() && location.getY() == loc.getY()) {
                return location.getPiece();
            }
        }
        return null;
    }

    public LocationType getLocationType(Location loc){
        for (Location location : importantLocs) {
            if (location.getX() == loc.getX() && location.getY() == loc.getY()) {
                return location.getLocationType();
            }
        }
        return CLEAR;
    }

    private boolean isExit(Location loc){
        int index = locationOf(loc);
        if(index != -1 && importantLocs.get(index).getLocationType() == LocationType.EXIT){
            return true;
        }
        return false;
    }

    /**
     * Get index of a given location within the list of important locations
     * @param loc
     * @return index of loc in importantLocs
     */
    private int locationOf(Location loc){
        for (int i = 0; i < importantLocs.size(); i++) {
            if (importantLocs.get(i).getX() == loc.getX() && importantLocs.get(i).getY() == loc.getY()) {
                return i;
            }
        }
        return -1;
    }

    public Location[][] getGraph(Location locCoords, int range){

        Location[][] graph = new Location[(2*range) + 1][(2*range) + 1];;
        int curX = locCoords.getX();
        int curY = locCoords.getY();

        //Initialize to all clear
        for(int i = 0; i < graph.length; i ++){
            for(int j = 0; j < graph[0].length; j ++){
                Location temp = new Location(curX - range + i, curY - range + j);
                temp.setLocationType(CLEAR);
                graph[i][j] = temp;
            }
        }

        // Insert all notable locations
        for(int i = 0; i < graph.length; i ++){
            for(int j = 0; j < graph[0].length; j ++){
                int arrayLocation = locationOf(graph[i][j]);
                if(arrayLocation != -1){
                    graph[i][j] = importantLocs.get(arrayLocation);
                }
            }
        }

        return graph;
    }

    /**
     * Moves piece on the board at 'from' to 'to' and returns number of points earned, if any
     * Assumes that 'from' contains a GamePiece
     * @param from
     * @param to
     * @return number of points earned from a piece if it moved to an exit
     */
    public int movePiece(Location from, Location to){
        int indexFrom = locationOf(from);
        int indexTo = locationOf(to);
        int points = 0;

        if(indexTo == -1){
            to.setPiece(importantLocs.get(indexFrom).getPiece());
            importantLocs.add(to);
        }
        else{
            if(isExit(to)){
                points = getPoints(from);
            }
            importantLocs.get(indexTo).setPiece(importantLocs.get(indexFrom).getPiece());
        }

        importantLocs.get(indexFrom).setPiece(null);

        return points;
    }

    /**
     * Gets a piece and returns its value. Defaults to 1
     * @param loc
     * @return value of removed piece, default 1
     */
    private int getPoints(Location loc){
        int index = locationOf(loc);
        int points = 1;
        if(index != -1){
            GamePiece piece = importantLocs.get(index).getPiece();
            if(piece == null){
                return points;
            }
            for(int i = 0; i < piece.getAttributes().length; i++){
                if(VALUE == piece.getAttributes()[i].getId()){
                    points = piece.getAttributes()[i].getValue();
                }
            }
        }
        return points;
    }

}