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;
}
}