Java_Chess

.idea
    .name 5 B
    artifacts
       Chess_jar.xml 371 B
    compiler.xml 734 B
    copyright
    description.html 97 B
    encodings.xml 171 B
    libraries
       jansi_1_11.xml 214 B
    misc.xml 5 KB
    modules.xml 261 B
    project-template.xml 91 B
    scopes
       scope_settings.xml 143 B
    uiDesigner.xml 8 KB
    vcs.xml 176 B
    workspace.xml 61 KB
Chess.iml 1 KB
Doxyfile 101 KB
GUI_Manual_Testplan.pdf 437 KB
Graphics
    blackBerolinaPawn.png 1 KB
    blackBishop.png 5 KB
    blackKing.png 8 KB
    blackKnight.png 6 KB
    blackPawn.png 2 KB
    blackQueen.png 8 KB
    blackRook.png 3 KB
    blackWazir.png 1 KB
    whiteBerolinaPawn.png 2 KB
    whiteBishop.png 7 KB
    whiteKing.png 8 KB
    whiteKnight.png 7 KB
    whitePawn.png 4 KB
    whiteQueen.png 10 KB
    whiteRook.png 4 KB
    whiteWazir.png 2 KB
README 342 B
jansi-1.11.jar 111 KB
src
    CHANGES 1001 B
    GUI
       Controller.java 15 KB
       Main.java 360 B
       View.java 14 KB
       textPanel.java 776 B
    Game
       Board.java 5 KB
       CLI.java 5 KB
       Command.java 1022 B
       GameManager.java 11 KB
       GameType.java 74 B
       MoveHandler.java 1 KB
       Player.java 133 B
    META-INF
       MANIFEST.MF 53 B
    Main.java 154 B
    Pieces
       BerolinaPawn.java 1 KB
       Bishop.java 480 B
       BoardSquare.java 4 KB
       King.java 470 B
       Knight.java 846 B
       MoveType.java 530 B
       Pawn.java 1 KB
       Queen.java 474 B
       Rook.java 471 B
       Wazir.java 627 B
    TODO 176 B
    Tests
       BoardTest.java 2 KB
       GameManagerTest.java 21 KB
Board.java
package Game;

import Pieces.*;

/**
 * This class represents the game board.
 */
public class Board {

    int height = 8;
    int width = 8;

    /**
     * Stores GameType enum for Standard game or Custom game.
     */
    private GameType type;

    /**
     * Array of BoardSquares storing the pieces on the board.
     */
    private BoardSquare[][] boardValues;

    /**
     * Board constructor.
     *
     * @param width width of the board
     * @param height height of the board
     */
    public Board(int width, int height, GameType type) {
        this.height = height;
        this.width = width;
        this.type = type;

        boardValues = new BoardSquare[height][width];

        if(type == GameType.BLANK){

            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    boardValues[i][j] = new BoardSquare(i, j, Player.NONE);
                }
            }

        } else {

            for (int i = 0; i < width; i++) {
                for (int j = 2; j < height - 2; j++) {
                    boardValues[i][j] = new BoardSquare(i, j, Player.NONE);
                }
            }

            SetPieces();

            if(type == GameType.CUSTOM)
                setCustomPieces();

        }

    }

    /**
     * Helper function to set initial pieces.
     */
    private void SetPieces(){

        //Set initial pieces.
        setupRowhelper(0, Player.WHITE);
        setupRowhelper(7, Player.BLACK);

        if(type == GameType.STANDARD) {
            for (int i = 0; i < width; i++) {
                boardValues[i][1] = new Pawn(i, 1, Player.WHITE);
                boardValues[i][0].owner = Player.WHITE;
                boardValues[i][6] = new Pawn(i, 6, Player.BLACK);
                boardValues[i][7].owner = Player.BLACK;
            }
        } else {
            for (int i = 0; i < width; i++) {
                boardValues[i][1] = new BerolinaPawn(i, 1, Player.WHITE);
                boardValues[i][0].owner = Player.WHITE;
                boardValues[i][6] = new BerolinaPawn(i, 6, Player.BLACK);
                boardValues[i][7].owner = Player.BLACK;
            }
        }
    }

    /**
     * Helper function to Set non-pawn pieces.
     *
     * @param row row number
     * @param owner owner id
     */
    private void setupRowhelper(int row, Player owner)
    {
        boardValues[0][row] = new Rook(0, row, owner);
        boardValues[1][row] = new Knight(1, row, owner);
        boardValues[2][row] = new Bishop(2, row, owner);
        boardValues[3][row] = new Queen(3, row, owner);
        boardValues[4][row] = new King(4, row, owner);
        boardValues[5][row] = new Bishop(5, row, owner);
        boardValues[6][row] = new Knight(6, row, owner);
        boardValues[7][row] = new Rook(7, row, owner);

    }
    /**
     * Place custom pieces on the board.
     */
    private void setCustomPieces(){
        boardValues[0][0] = new Wazir(0, 0, Player.WHITE);
        boardValues[7][0] = new Wazir(7, 0, Player.WHITE);

        boardValues[0][7] = new Wazir(0, 7, Player.BLACK);
        boardValues[7][7] = new Wazir(7, 7, Player.BLACK);

    }

    /**
     * Returns the Pieces.BoardSquare at (x,y)
     *
     * @param x x location
     * @param y y location
     * @return Pieces.BoardSquare at (x,y)
     */
    public BoardSquare getPiece(int x, int y){

        if(x<0 || y<0 || x>=width || y>=height)
            return null;

        return boardValues[x][y];
    }

    /**
     *
     * Set a Piece at (x,y).
     *
     * @param x x location
     * @param y y location
     * @param Square Pieces.BoardSquare of the piece child class we want to set.
     * @param owner owner id of the piece's owner.
     */
    public void setPiece(int x, int y, BoardSquare Square, Player owner){
        if(x<0 || y<0 || x>=width || y>=height)
            return;

        boardValues[x][y] = Square.copy(Square);//new (instanceof piece);

        boardValues[x][y].x = x;//@todo refactor into function.
        boardValues[x][y].y = y;
        boardValues[x][y].owner = owner;

        boardValues[x][y].hasMoved = true;//@todo possible bug if move is reverted.
    }

    /**
     * Set x,y, to a blank Pieces.BoardSquare
     *
     * @param x x location
     * @param y y location
     */
    public void setBlank(int x, int y){
        if(x<0 || y<0 || x>=width || y>=height)
            return;

        setPiece(x, y, new BoardSquare(), Player.NONE);

    }

    /**
     * Returns true if pieces are found between startSquare and destSquare
     *
     * @param startSquare starting board location
     * @param destSquare ending board location
     * @param dist distance to check
     * @return true if pieces are found, false otherwise.
     */
    public boolean findPieces(BoardSquare startSquare, BoardSquare destSquare, int dist) {

        int x = startSquare.x;
        int xc = (startSquare.x < destSquare.x) ? 1 : -1;
        int y = startSquare.y;
        int yc = (startSquare.y < destSquare.y) ? 1 : -1;

        if(startSquare.y == destSquare.y)
            yc = 0;

        if(startSquare.x == destSquare.x)
            xc = 0;

        for(int i = 0; i < Math.abs(dist) - 1; i++){
            x += xc;
            y += yc;
            if(getPiece(x, y).owner != Player.NONE){
                return true;//Found Piece
            }
        }
        return false;
    }
}