/** *Board and movement of pieces *@author arghavan soleymanpour *@version 2.0 */ package Main; import Pieces.*; import java.util.ArrayList; import java.util.Queue; public class Board { private King whiteKing; private King blackKing; private ArrayList whiteRemovedList = new ArrayList<>(); private ArrayList blackRemovedList = new ArrayList<>(); private Piece board[][] = new Piece[8][8]; private boolean whiteMove = true; public Board(){ board[0][0] = new Rook(0,0,false,this); board[7][0] = new Rook(7,0,false,this); board[1][0] = new Knight(1,0,false,this); board[6][0] = new Knight(6,0,false,this); board[2][0] = new Bishop(2,0,false,this); board[5][0] = new Bishop(5,0,false,this); blackKing = new King(3,0,false,this); board[3][0] = blackKing; board[4][0] = new Queen(4,0,false,this); for (int i = 0 ; i < 8 ; i++){ board[i][1] = new Pawn(i,1,false,this); board[i][6] = new Pawn(i,6,true,this); } board[0][7] = new Rook(0,7,true,this); board[7][7] = new Rook(7,7,true,this); board[1][7] = new Knight(1,7,true,this); board[6][7] = new Knight(6,7,true,this); board[2][7] = new Bishop(2,7,true,this); board[5][7] = new Bishop(5,7,true,this); board[3][7] = new Queen(3,7,true,this); whiteKing = new King(4,7,true,this); board[4][7] =whiteKing; } public ArrayList getBlackRemovedList(){ return blackRemovedList; } public ArrayList getWhiteRemovedList(){ return whiteRemovedList; } public boolean isWhiteMove(){ return whiteMove; } public boolean getMove(int x , int y , int destX , int destY){ if(board[x][y]==null) return false; // System.out.println(x+","+y+"->"+destX+","+destY); // System.out.println(board[x][y]); if((getColor(x, y)==1 && whiteMove)|| (getColor(x, y)==2 && !whiteMove)) { if (board[x][y].isValidMove(destX, destY)) { // System.out.println("Reach"); board[x][y].setX(destX); board[x][y].setY(destY); if(board[destX][destY]!=null){ if(board[destX][destY].isWhite()){ whiteRemovedList.add(getCopy(board[destX][destY])); System.out.println(whiteRemovedList.size()); }else { blackRemovedList.add(getCopy(board[destX][destY])); System.out.println(blackRemovedList.size()); } } board[destX][destY] = getCopy(board[x][y]); board[x][y] = null; whiteMove = !whiteMove; return true; } else return false; } else return false; } public ArrayList getMoves(int x,int y){ if(board[x][y]==null) return new ArrayList<>(); return board[x][y].moves(); } public Piece getCopy(Piece p){ if(p instanceof Bishop){ Bishop ret = new Bishop(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; }else if(p instanceof King){ King ret = new King(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; } else if(p instanceof Knight){ Knight ret = new Knight(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; }else if(p instanceof Pawn){ Pawn ret = new Pawn(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; }else if(p instanceof Queen){ Queen ret = new Queen(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; } else { Rook ret = new Rook(p.getX(),p.getY(),p.isWhite(),p.getBoard()); return ret; } } public boolean KishCheck(boolean isWhite){ for(int i = 0 ; i < 8 ; i++){ for (int j = 0 ; j < 8 ; j++){ Piece p = board[i][j]; if(p instanceof King && p.isWhite()) whiteKing = (King)p; else if(p instanceof King && p.isBlack()) blackKing = (King)p; } } if(isWhite){ for (int i = 0 ; i < 8 ;i++){ for(int j = 0 ; j < 8 ; j++){ if(board[i][j] != null && board[i][j].isBlack()){ int xx = whiteKing.getX(); int yy = whiteKing.getY(); if(board[i][j].isValidMove(xx,yy)) return true; } } } } else { for (int i = 0 ; i < 8 ;i++){ for(int j = 0 ; j < 8 ; j++){ if(board[i][j] != null && board[i][j].isWhite()){ int xx = blackKing.getX(); int yy = blackKing.getY(); if(board[i][j].isValidMove(xx,yy)) return true; } } } } return false; } public boolean checkGameOver(boolean isWhite){ if(!KishCheck(isWhite)) return false; if(isWhite){ if(whiteKing.moves().isEmpty()){ return true; } } else { if(blackKing.moves().isEmpty()) { return true; } } return false; } public Piece[][] getBoard() { return board; } public boolean isEmpty(int x , int y){ if (board[x][y] ==null) return true; return false; } public int getColor(int x , int y){ if(isEmpty(x, y)) return 0; if (board[x][y].isWhite()) return 1; if (board[x][y].isBlack()) return 2; return 0; } @Override public String toString() { String str = " a b c d e f g h\n"; str = str + "------------------\n"; for (int j = 0 ; j < 8 ; j++){ str = str+(j+1)+"|"; for (int i = 0 ; i < 7 ; i++){ if(board[i][j]!=null) { str = str + board[i][j] + " "; } else str = str + "0 "; } if(board[7][j]!=null) { str = str + board[7][j]; } else str = str + "0"; str = str+"|"+(j+1)+"\n"; } str = str + "------------------\n"; str = str+ " a b c d e f g h\n"; return str; } }