/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stupidchess.game.entity;

/**
 *
 * @author AnhNTTSE04296
 */
public abstract class ChessPiece {

    //Checks the cell to make sure it is empty
    private boolean checkCellEmpty(int newRow, int newColumn, int[][] playerMatrix) {
        if (playerMatrix[newRow][newColumn] == 0) //If not empty
        {
            return true;
        }
        return false;

    }

    /**
     * Method for checking the path to the destination and making sure nothing
     * is in the way
     *
     * @param startRow The row where the piece currently stands
     * @param startColumn The column where the piece currently stands
     * @param desRow The row where the piece attempt to move
     * @param desColumn The column where the piece attempt to move
     * @param playerMatrix The matrix represent all of player's alive pieces
     * @param straightAxis true if piece move straight Axis (queen,rock)
     * @return true if the move is possible, false if not
     */
    public boolean axisMove(int startRow, int startColumn, int desRow, int desColumn, int[][] playerMatrix, boolean straightAxis) {
        if ((desRow == startRow) && (desColumn == startColumn)) { // use in bishop, rock, knight, queen
            return false;
        }

        if (straightAxis) //Moving along a straight axis (rock, queen)
        {
            if ((startColumn == desColumn) && (startRow != desRow)) //Moving along the same column
            {
                int rMin = Math.min(startRow, desRow);
                int rMax = Math.max(startRow, desRow);
                for (int i = rMin + 1; i < rMax; i++) {
                    if (!checkCellEmpty(i, desColumn, playerMatrix)) {
                        return false;
                    }
                }
            } else if ((startColumn != desColumn) && (startRow == desRow)) //Moving along the same column
            { // Moving along the same row
                int cMin = Math.min(startColumn, desColumn);
                int cMax = Math.max(startColumn, desColumn);
                for (int i = cMin + 1; i < cMax; i++) {
                    if (!checkCellEmpty(desRow, i, playerMatrix)) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        } else //Moving diagonal (bishop/queen)
        {
            int currentColumn = Math.min(desColumn, startColumn) + 1;
            if ((desRow - startRow == desColumn - startColumn)) { // Moved NW, SE
                for (int currentRow = Math.min(desRow, startRow) + 1; currentRow < Math.max(desRow, startRow); currentRow++) {
                    if (!checkCellEmpty(currentRow, currentColumn++, playerMatrix)) {
                        return false;
                    }
                }
            } else if (Math.abs(desRow - startRow) == Math.abs(desColumn - startColumn)) { // Moved NE, SW
                currentColumn = Math.max(desColumn, startColumn) - 1;
                for (int currentRow = Math.min(desRow, startRow) + 1; currentRow < Math.max(desRow, startRow); currentRow++) {
                    if (!checkCellEmpty(currentRow, currentColumn--, playerMatrix)) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     *
     * @param startRow The row where the piece currently stands
     * @param startColumn The column where the piece currently stands
     * @param desRow The row where the piece attempt to move
     * @param desColumn The column where the piece attempt to move
     * @param playerMatrix The matrix represent all of player's alive pieces
     * @param currentPlayer 1 : player 1. 2 : player 2
     * @return true if the move is possible, false if not
     */
    public abstract boolean checkLegalMove(int startRow, int startColumn, int desRow, int desColumn, int[][] playerMatrix, int currentPlayer);
}
