public void userAsBlack() { ChessEngine.flipBoard(); for (int i = 0; i < 8; i++) { System.arraycopy(ChessEngine.chessboard[i], 0, chessboard[i], 0, 8); } ChessEngine.flipBoard(); System.out.println(ChessEngine.sortMoves2(ChessEngine.possibleMoves())); ChessEngine.makeMove(ChessEngine.printMove()); ChessEngine.flipBoard(); if (ChessEngine.possibleMoves().length() == 0) { System.out.println("Defeat"); } }
public void movePieceEvent() { // check for castle remove castling if (Math.abs((mouseX - border) / squareSize - (newMouseX - border) / squareSize) == 2 && "A" .equals( ChessEngine.chessboard[(newMouseY - border) / squareSize][ (newMouseX - border) / squareSize])) { // (castling): if ((mouseX - border) / squareSize == 3) { // black ChessEngine.castleBlackLong = false; ChessEngine.castleBlackShort = false; } else { // white ChessEngine.castleWhiteLong = false; ChessEngine.castleWhiteShort = false; } } for (int i = 0; i < 8; i++) { System.arraycopy(ChessEngine.chessboard[i], 0, chessboard[i], 0, 8); } ChessEngine.flipBoard(); ChessEngine.rating(1, 0); System.out.println(ChessEngine.sortMoves2(ChessEngine.possibleMoves())); if (ChessEngine.possibleMoves().length() == 0) { System.out.println("You win!"); ChessEngine.flipBoard(); } else { ChessEngine.makeMove(ChessEngine.printMove()); ChessEngine.flipBoard(); if (ChessEngine.possibleMoves().length() == 0) { if (ChessEngine.kingSafe()) { System.out.println("Stalemate"); } else { System.out.println("Defeat!"); } } } repaint(); }
@Override public void mouseReleased(MouseEvent e) { newMouseX = e.getX(); newMouseY = e.getY(); mouseDrag[(mouseY - border) / squareSize][(mouseX - border) / squareSize][0] = 0; mouseDrag[(mouseY - border) / squareSize][(mouseX - border) / squareSize][1] = 0; repaint(); if (e.getButton() == MouseEvent.BUTTON1) { // if left mouse click String dragMove; if ((newMouseY - border) / squareSize == 0 && (mouseY - border) / squareSize == 1 && "P" .equals( ChessEngine.chessboard[(mouseY - border) / squareSize][ (mouseX - border) / squareSize])) { // pawn promotion dragMove = "" + (mouseX - border) / squareSize + (newMouseX - border) / squareSize + ChessEngine.chessboard[(newMouseY - border) / squareSize][ (newMouseX - border) / squareSize] + "QP"; // assumes queen promotion } else if (Math.abs((mouseX - border) / squareSize - (newMouseX - border) / squareSize) == 2 && "A" .equals( ChessEngine.chessboard[(mouseY - border) / squareSize][ (mouseX - border) / squareSize])) { // castling if ((mouseX - border) / squareSize > (newMouseX - border) / squareSize) { // left dragMove = "" + (mouseX - border) / squareSize + "0" + (newMouseX - border) / squareSize + ((newMouseX - border) / squareSize + 1) + "C"; } else { // right dragMove = "" + (mouseX - border) / squareSize + "7" + (newMouseX - border) / squareSize + ((newMouseX - border) / squareSize - 1) + "C"; } } else { dragMove = "" + (mouseY - border) / squareSize + (mouseX - border) / squareSize + (newMouseY - border) / squareSize + (newMouseX - border) / squareSize + ChessEngine.chessboard[(newMouseY - border) / squareSize][ (newMouseX - border) / squareSize]; } String userMovePosibilities = ChessEngine.possibleMoves(); if (userMovePosibilities.replace(dragMove, "").length() < userMovePosibilities.length()) { ChessEngine.makeMove(dragMove); ChessEngine.history += dragMove; movePieceEvent(); } } }