import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.PrintWriter; import java.util.ArrayList; /** * ChessBoardGUI is a class that show's the graphical form of the game. *@author kimiadorani *@version 1.0 *@since 2019-5-7 */ public class ChessBoardGUI extends JPanel { public static boolean isServer = false ; private PrintWriter out; public JButton[][] button = new JButton[8][8]; private JPanel whiteRemovedPiecePanel; private JPanel blackRemovedPiecePanel; private JPanel turnPanel; private JButton removedPieces[][] = new JButton[8][4]; private JLabel turnLabel = new JLabel(); private int wORb = 0; private Piece pw[]; private Piece pb[]; private Player white; private Player black; private Cell board[][]; private Cell source; private Cell dest; private boolean[][] moveBoard = null; public ArrayList deletedList = new ArrayList<>(); private JFrame parentPanel; private boolean isFreezGame = false; public ChessBoardGUI(JPanel whiteRemovedPiecePanel,JPanel blackRemovedPiecePanel,JPanel turnPanel,JFrame parentPanel,PrintWriter out){ super(); this.whiteRemovedPiecePanel= whiteRemovedPiecePanel; this.blackRemovedPiecePanel = blackRemovedPiecePanel; this.turnPanel = turnPanel; this.parentPanel = parentPanel; this.out = out; // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800,600); initlizeGame(); inilizeRemovedPiecePanel(); setLayout(new GridLayout(8,8)); Piece pieceBoard[][] = getUpdatedBoard(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { button[i][j] = new JButton(); int finalI = i; int finalJ = j; if(pieceBoard[i][j]!=null) button[i][j].setIcon(new ImageIcon(pieceBoard[i][j].getPieceImage())); button[i][j].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println(finalJ +","+ finalI); if(isFreezGame) return; if(isServer && wORb%2==1) return; if(!isServer && wORb%2==0) return; source = new Cell(dest); dest = new Cell(finalI,finalJ); if(moveBoard!=null){ if(moveBoard[finalI][finalJ] && source!=null && dest!=null){ movePiece(source,dest); sendMovmentToOtherApp(source,dest); updatesAfterMove(); //---------------- } } moveBoard = moves(dest); if(moveBoard !=null){ updateGUIBoard(moveBoard); } if(wORb%2==0){ turnLabel.setText("White Turn..."); }else { turnLabel.setText("Black Turn..."); } } }); add(button[i][j]); if((i+j)%2==0) button[i][j].setBackground(Color.CYAN); else { button[i][j].setBackground(Color.WHITE); } } } // setVisible(true); } public void updatesAfterMove(){ buttonUpdate(); updateDeletedPanel(); //---------------- int condition = checkmateCondition(board,pw,pb); if(condition==2){ JOptionPane.showMessageDialog(turnPanel,"White Win"); System.out.println("White Wiinnn"); isFreezGame=true; } else if(condition==1){ JOptionPane.showMessageDialog(turnPanel,"Black Win"); System.out.println("Black Win"); isFreezGame=true; } } private void inilizeRemovedPiecePanel(){ for (int i = 0 ; i < 8 ;i++){ for (int j = 0 ; j < 2 ;j++) { removedPieces[i][j] = new JButton(); removedPieces[i][j].setBackground(Color.YELLOW); // added.setMinimumSize(new Dimension(50,50)); whiteRemovedPiecePanel.add( removedPieces[i][j]); removedPieces[i][3-j] = new JButton(); removedPieces[i][3-j].setBackground(Color.YELLOW); blackRemovedPiecePanel.add( removedPieces[i][3-j]); } } turnPanel.setLayout( new FlowLayout(FlowLayout.CENTER)); turnPanel.add(turnLabel); } private void updateDeletedPanel(){ int wCount = 0; int bCount = 0; for (Piece p : deletedList){ if(p.getPieceColor()==PieceColor.WHITE) { int i = wCount % 8; int j = wCount / 8; wCount++; removedPieces[i][j].setIcon(new ImageIcon(p.getPieceImage())); }else { int i = bCount % 8; int j = bCount / 8; bCount++; removedPieces[i][3-j].setIcon(new ImageIcon(p.getPieceImage())); } } } /** * it sends the Coordinates of the moves of the pieces in form of 4 numbers * to be changed in the other * @param source the coordinates that we start from * @param dest the coordinates that is destination */ private void sendMovmentToOtherApp(Cell source, Cell dest){ out.println(source.getRow()+"\t"+source.getCol()+"\t"+dest.getRow()+"\t"+dest.getCol()); out.flush(); } /** * it updates the graphical page , it changes the pieces with the new actions */ private void buttonUpdate(){ Piece[][] pieceBoard = getUpdatedBoard(); for (int i = 0 ;i<8 ;i++){ for (int j = 0 ; j < 8 ;j++){ if(pieceBoard[i][j]!=null) { button[i][j].setIcon(new ImageIcon(pieceBoard[i][j].getPieceImage())); }else { button[i][j].setIcon(new ImageIcon()); } if((i+j)%2==0) button[i][j].setBackground(Color.CYAN); else { button[i][j].setBackground(Color.WHITE); } } } } /*** * change the background color of possible moves to red * @param moveBoard possible moves of the piece */ private void updateGUIBoard(boolean [][] moveBoard){ if(moveBoard ==null) return;; for (int i = 0 ; i < 8 ;i++){ for (int j =0 ; j < 8 ;j++){ if(moveBoard[i][j]){ button[i][j].setBackground(Color.RED); // System.out.println("Reach Thereeee..."); } else { if((i+j)%2==0) button[i][j].setBackground(Color.CYAN); else { button[i][j].setBackground(Color.WHITE); } } } } } /** * * @param board * @param pw * @param pb * @return */ private int checkmateCondition(Cell board[][], Piece pw[], Piece pb[]) { if (checkCondition(board, pw, pb, 1) == 1) { for (Piece p:pw) { for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ; Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ; if (p.isValidMove(board[i][j], board, pw, pb)) { board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[i][j].setEmpty(false) ; p.setCell(board[i][j]) ; for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j) blackPiece.setDeleted(true) ; } if (checkCondition(board, pw, pb, 1) != 1) { board[tmp.getRow()][tmp.getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j) blackPiece.setDeleted(false) ; } return 0 ; } board[tmp.getRow()][tmp.getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j) blackPiece.setDeleted(false) ; } } } } } System.out.println("Black player win!") ; return 1; } else if (checkCondition(board, pw, pb, 1) == 2) { for (Piece p:pb) { for (int i=0 ; i<8 ; ++i) { for (int j=0 ; j<8 ; ++j) { if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ; Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ; if (p.isValidMove(board[i][j], board, pw, pb)) { board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[i][j].setEmpty(false) ; p.setCell(board[i][j]) ; for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j) whitePiece.setDeleted(true) ; } if (checkCondition(board, pw, pb, 1) != 2) { board[tmp.getRow()][tmp.getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j) whitePiece.setDeleted(false) ; } return 0 ; } board[tmp.getRow()][tmp.getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j) whitePiece.setDeleted(false) ; } } } } } System.out.println("White player win!") ; return 2; } else return 0 ; } public void initlizeGame(){ white = new Player(PieceColor.WHITE) ; black = new Player(PieceColor.BLACK) ; pw = white.getPieces() ; pb = black.getPieces() ; 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() ; } } /** *give coordinate of a piece and shows the possible moves * @param source the coordinates that move start from * @return list of possible moves in a binary board */ public boolean[][] moves(Cell source){ boolean[][] binaryBoard = new boolean[8][8]; if (wORb%2==0) { for (Piece p : pw){ if(!p.isDeleted() && p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){ for (int i = 0 ; i < 8;i++){ for (int j = 0 ; j < 8 ;j++){ // Cell dest = new Cell(i,j); if(p.isValidMove(board[i][j],board,pw,pb)){ binaryBoard[i][j] = true; } } } return binaryBoard; } } }else { for (Piece p : pb){ if(!p.isDeleted() && p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){ for (int i = 0 ; i < 8;i++){ for (int j = 0 ; j < 8 ;j++){ if(p.isValidMove(board[i][j],board,pw,pb)){ binaryBoard[i][j] = true; } } } return binaryBoard; } } } return null; } /** * it gets a source and destination and moves piece from source to destination * @param source a coordinates that move has started from * @param dest a coordinates that is our destination * @return true if the change was possible and piece moved */ public boolean movePiece(Cell source, Cell dest){ //-------------- if (wORb%2==0) { for (Piece p : pw) { if (!p.isDeleted() && p.getCell().getRow() == source.getRow() && p.getCell().getCol() == source.getCol()) { int sw ; if (checkCondition(board, pw, pb, 1)==1) { sw=1 ; int i=dest.getRow(); int j=dest.getCol(); Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[i][j].setEmpty(false) ; p.setCell(board[i][j]) ; for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j) blackPiece.setDeleted(true) ; } if (checkCondition(board, pw, pb, 1) != 1) sw=0 ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece blackPiece:pb) { if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j) blackPiece.setDeleted(false) ; } } else sw=0 ; if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb)&& sw==0) { dest = board[dest.getRow()][dest.getCol()]; wORb++; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true); p.setCell(dest); board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false); if (p instanceof Pawn) { Pawn myPawn = (Pawn) p; myPawn.setOnce(false); } for (Piece blackPiece : pb) { if (blackPiece.getCell().getRow() == dest.getRow() && blackPiece.getCell().getCol() == dest.getCol()) { blackPiece.setDeleted(true); deletedList.add(blackPiece); } } return true; } } } }else { for (Piece p:pb) { if (!p.isDeleted() && p.getCell().getRow()==source.getRow() && p.getCell().getCol() == source.getCol()) { int sw ; if (checkCondition(board, pw, pb, 1)==2) { sw=1 ; int i=dest.getRow(); int j=dest.getCol(); Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[i][j].setEmpty(false) ; p.setCell(board[i][j]) ; for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j) whitePiece.setDeleted(true) ; } if (checkCondition(board, pw, pb, 1) != 2) sw=0 ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; board[i][j].setEmpty(true) ; p.setCell(tmp) ; for (Piece whitePiece:pw) { if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j) whitePiece.setDeleted(false) ; } } else sw=0 ; if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb) && sw==0) { dest = board[dest.getRow()][dest.getCol()]; wORb++; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true); p.setCell(dest); board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false); if (p instanceof Pawn) { Pawn myPawn = (Pawn) p; myPawn.setOnce(false); } for (Piece whitePiece : pw) { if (whitePiece.getCell().getRow() == dest.getRow() && whitePiece.getCell().getCol() == dest.getCol()) { whitePiece.setDeleted(true); deletedList.add(whitePiece); } } return true; } } } } //-------------- return false; } /** *when we call this method the last changes of the board are shown * @return the final board with changes */ public Piece[][] getUpdatedBoard(){ Piece[][] pieceboard = new Piece[8][8]; Piece pw[] = white.getPieces() ; Piece pb[] = black.getPieces() ; for (Piece p : pw){ if(!p.isDeleted()) pieceboard[p.getCell().getRow()][p.getCell().getCol()] = p; } for (Piece p : pb){ if(!p.isDeleted()) pieceboard[p.getCell().getRow()][p.getCell().getCol()] = p; } return pieceboard; } /** * it check Condition for pieces that are instance of white or black kings. * @param board the board of the game * @param pw the piece that belongs to white player * @param pb the piece that belong to black player * @param flag * @return */ private int checkCondition(Cell board[][], Piece pw[], Piece pb[], int flag) { for (Piece p:pw) { if (p instanceof King) { int sw1=0 ; for (Piece p1:pb) { if (p1.isDeleted()) continue ; if (p1.isValidMove(p.getCell(), board, pw, pb)) { sw1=1 ; } } if(sw1==1) { if (flag==0) System.out.println("Check condition for White player!!") ; return 1 ; } } } for (Piece p:pb) { if (p instanceof King) { int sw1=0 ; for (Piece p1:pw) { if (p1.isDeleted()) continue ; if (p1.isValidMove(p.getCell(), board, pw, pb)) { sw1=1 ; } } if(sw1==1) { if (flag==0) System.out.println("Check condition for Black player!!") ; return 2 ; } } } return 0 ; } }