import java.util.Scanner; public class Main{ /** *prints board and control the moves of the pieces *@author kimiadorani *@version 1.0 *@since 2019-5-7 */ private static char boardChar[][] = new char [8][8] ; private static void printBoard() { for (int i=1 ; i<9 ; ++i) { System.out.print(" " + i) ; } System.out.print("\n ") ; for (int i=0 ; i<8 ; ++i) System.out.print("+---") ; System.out.print("+\n") ; for (int i=0 ; i<8 ; ++i) { System.out.print( (char)(i+'a') ) ; for (int j=0 ; j<8 ; ++j) { System.out.print("| " + boardChar[i][j] + " ") ; } System.out.print("|\n ") ; for (int i2=0 ; i2<8 ; ++i2) System.out.print("+---") ; System.out.print("+\n") ; } } private static void updateBoard(Piece pw[], Piece pb[]) { for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { boardChar[i][j] = ' ' ; } } for (Piece p:pw) { if (p.isDeleted()) continue ; if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; } for (Piece p:pb) { if (p.isDeleted()) continue ; if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; } } private static void checkCondition(Cell board[][], Piece pw[], Piece pb[]) { for (Piece p:pw) { if (p instanceof King) { int sw1=0 ; for (Piece p1:pb) { if (p1.isValidMove(p.getCell(), board, pw, pb)) { sw1=1 ; } } if(sw1==1) System.out.println("Check condition for White player!!") ; } } for (Piece p:pb) { if (p instanceof King) { int sw1=0 ; for (Piece p1:pw) { if (p1.isValidMove(p.getCell(), board, pw, pb)) { sw1=1 ; } } if(sw1==1) System.out.println("Check condition for Balck player!!") ; } } } public static void main(String[] args) { Scanner in = new Scanner(System.in) ; Player white = new Player(PieceColor.WHITE) ; Player black = new Player(PieceColor.BLACK) ; Piece pw[] = white.getPieces() ; Piece pb[] = black.getPieces() ; Cell board[][] = new Cell[8][8] ; for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { board[i][j] = new Cell (i,j) ; } } for (Piece p:pw) { board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; } for (Piece p:pb) { board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; } int wORb = 0 ; updateBoard(pw, pb) ; printBoard() ; while (true) { if (wORb%2==0) System.out.println("White player turn") ; else System.out.println("Black player turn") ; String s = in.nextLine() ; if (s.length()==2) { for (Piece p:pw) { if (p.getCell().getRow()==s.charAt(0)-'a' && p.getCell().getCol()+1==s.charAt(1)-'0') { for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { if (p.isValidMove(board[i][j], board, pw, pb)) { System.out.print( (char)((int)'a'+i) ) ; System.out.println(j+1) ; } } } } } for (Piece p:pb) { if (p.getCell().getRow()==s.charAt(0)-'a' && p.getCell().getCol()+1==s.charAt(1)-'0') { for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { if (p.isValidMove(board[i][j], board, pw, pb)) { System.out.print( (char)((int)'a'+i) ) ; System.out.println(j+1) ; } } } } } } else if (s.length()==5) { if (wORb%2==0) { for (Piece p:pw) { if (p.getCell().getRow()==s.charAt(0)-'a' && p.getCell().getCol()+1==s.charAt(1)-'0') { if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb)) { wORb++ ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; p.setCell(tmp) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; for (Piece p1:pb) { if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) { p1.setDeleted(true) ; } } if (p instanceof Pawn) { Pawn myPawn = (Pawn) p ; myPawn.setOnce(false) ; } for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==tmp.getRow() && blackPiece.getCell().getCol()==tmp.getCol()) blackPiece.setDeleted(true) ; } } } } if (wORb%2==0) System.out.println("please enter a valid move!") ; else { checkCondition(board, pw, pb) ; updateBoard(pw, pb) ; printBoard() ; } } else { for (Piece p:pb) { if (p.getCell().getRow()==s.charAt(0)-'a' && p.getCell().getCol()+1==s.charAt(1)-'0') if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) ) { wORb++ ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; p.setCell(tmp) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; for (Piece p1:pw) { if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) { p1.setDeleted(true) ; } } if (p instanceof Pawn) { Pawn myPawn = (Pawn) p; myPawn.setOnce(false) ; } for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==tmp.getRow() && whitePiece.getCell().getCol()==tmp.getCol()) whitePiece.setDeleted(true) ; } } } if (wORb%2==1) System.out.println("please enter a valid move!") ; else { checkCondition(board, pw, pb) ; updateBoard(pw, pb) ; printBoard() ; } } } else { System.out.println("Please enter a valid input!") ; } } } }