fix errors and checkmate
This commit is contained in:
parent
5f8d4f269a
commit
e5cddf031b
|
|
@ -70,6 +70,16 @@ public class Board {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean isCheckmate() {
|
||||
Check check = new Check(board);
|
||||
return check.checkmate(whiteTurn);
|
||||
}
|
||||
|
||||
public void setParameters() {
|
||||
//
|
||||
}
|
||||
|
||||
// INITIALISE THE BOARD
|
||||
public void initialise() {
|
||||
this.x = -1;
|
||||
|
|
@ -95,6 +105,10 @@ public class Board {
|
|||
board[y][x] = new Piece(isWhite, type, x, y);
|
||||
}
|
||||
|
||||
public void setPiece(Piece piece) {
|
||||
board[y][x] = new Piece(piece);
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
for (int i = 0; i < lineNum; i++) {
|
||||
boolean white = (i > 5);
|
||||
|
|
@ -192,6 +206,11 @@ public class Board {
|
|||
this.x = -1;
|
||||
this.y = -1;
|
||||
}
|
||||
else if (board[y][x] != null && board[y][x].isWhite() == whiteTurn) {
|
||||
// update coordinates
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
// the new location is highlighted, meaning it is a legal displacement
|
||||
else if (isHighlighted(x,y)) {
|
||||
Piece piece = board[this.y][this.x];
|
||||
|
|
@ -384,7 +403,7 @@ public class Board {
|
|||
whiteTurn = true;
|
||||
}
|
||||
|
||||
Boolean[] booleans = new Boolean[7];
|
||||
boolean[] booleans = new boolean[7];
|
||||
for (int k = 1; k < 8; k++) {
|
||||
if (undo[lineNum].charAt(k) == 'f') {
|
||||
booleans[k-1] = false;
|
||||
|
|
@ -431,32 +450,33 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
public Board(Piece[][] board) {
|
||||
Piece[][] newBoard = new Piece[lineNum][colNum];
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
Piece piece = board[j][i];
|
||||
if (piece != null) {
|
||||
newBoard[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
board[move.getFromY()][move.getFromX()].setX(move.getToX());
|
||||
board[move.getFromY()][move.getFromX()].setY(move.getToY());
|
||||
board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()];
|
||||
board[move.getFromY()][move.getFromX()] = null;
|
||||
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
canPromote(x, y);
|
||||
}
|
||||
}
|
||||
//this.states.add(toString());
|
||||
promotion(x, y);
|
||||
}
|
||||
|
||||
ArrayList<Piece> promoPieces= new ArrayList<>();
|
||||
boolean promotion;
|
||||
|
||||
private boolean canPromote(int x, int y) {
|
||||
private void promotion(int x, int y) {
|
||||
if (board[y][x] != null && board[y][x].getType() == PieceType.Pawn) {
|
||||
boolean isWhite = board[y][x].isWhite(); // ✅ Declare 'isWhite' here
|
||||
boolean isWhite = board[y][x].isWhite(); // Declare 'isWhite' here
|
||||
if ((isWhite && y == 0) || (!isWhite && y == 7)) {
|
||||
board[y][x] = new Piece(isWhite, PieceType.Queen, x, y); // ✅ Now valid
|
||||
return true;
|
||||
board[y][x] = new Piece(isWhite, PieceType.Queen, x, y); // Now valid
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,249 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Check {
|
||||
|
||||
private Piece[][] board;
|
||||
private int colNum;
|
||||
private int lineNum;
|
||||
private int[][] knightMoves;
|
||||
private int[][] kingMoves;
|
||||
private int[][] whitePawn;
|
||||
private int[][] blackPawn;
|
||||
private int[][] directions;
|
||||
ArrayList<int[]> moves;
|
||||
MoveCalculator calculator;
|
||||
|
||||
public Check(Piece[][] boardF) {
|
||||
board = boardF;
|
||||
knightMoves = new int[][] {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
|
||||
kingMoves = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}, {2, 0}, {-2, 0}};
|
||||
whitePawn = new int[][] {{6}, {0, -1}, {1, -1}, {-1, -1}, {0, -2}};
|
||||
blackPawn = new int[][] {{1}, {0, 1}, {1, 1}, {-1, 1}, {0, 2}};
|
||||
directions = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
|
||||
lineNum = 8;
|
||||
colNum = 8;
|
||||
calculator = new MoveCalculator(board);
|
||||
}
|
||||
|
||||
// return the coordinates of the king of the color stated in parameter
|
||||
public int[] findKing(boolean white) {
|
||||
int[] coordinates = {-1, -1};
|
||||
// iterate through all columns
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
// iterate through all lines
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
// if the location is not empty, verify if it is the king from the right color
|
||||
if (board[j][i] != null && board[j][i].getType() == PieceType.King && board[j][i].isWhite() == white) {
|
||||
// update the coordinates to return
|
||||
coordinates[0] = i;
|
||||
coordinates[1] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
// return if the piece of coordinates x,y in parameter is in check or not
|
||||
public boolean check(Piece[][] board2, int x, int y, boolean white) {
|
||||
int i;
|
||||
int j;
|
||||
|
||||
// verify for knight or king displacements
|
||||
for (int n = 0; n < knightMoves.length; n++) {
|
||||
i = x + knightMoves[n][0];
|
||||
j = y + knightMoves[n][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (calculator.inBoard(i,j)) {
|
||||
// verify if the location is empty, if not, verify if the piece is a knight from the opponent
|
||||
if (board2[j][i] != null && board2[j][i].isWhite() != white && board2[j][i].getType() == PieceType.Knight) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int n = 0; n < kingMoves.length; n++) {
|
||||
i = x + kingMoves[n][0];
|
||||
j = y + kingMoves[n][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (calculator.inBoard(i,j)) {
|
||||
// verify if the location is empty, if not, verify if the piece is a knight from the opponent
|
||||
if (board2[j][i] != null && board2[j][i].isWhite() != white && board2[j][i].getType() == PieceType.King) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify for pawn displacements
|
||||
int[][] pawn;
|
||||
if (white) {
|
||||
pawn = whitePawn;
|
||||
}
|
||||
else {
|
||||
pawn = blackPawn;
|
||||
}
|
||||
// consider only the diagonal displacements
|
||||
for (int o = 2; o < 4; o++) {
|
||||
i = x + pawn[o][0];
|
||||
j = y + pawn[o][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (calculator.inBoard(i,j)) {
|
||||
// verify if the location is empty, if not, verify if the piece is a pawn from the opponent
|
||||
if (board2[j][i] != null && board2[j][i].isWhite() != white && board2[j][i].getType() == PieceType.Pawn) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify for linear displacements (from directions array)
|
||||
for (int p = 0; p < directions.length; p++) {
|
||||
i = x + directions[p][0];
|
||||
j = y + directions[p][1];
|
||||
boolean metPiece = false;
|
||||
// continue until the end of the board or until a piece is met
|
||||
while (calculator.inBoard(i,j) && metPiece == false) {
|
||||
// if the location is not empty
|
||||
if (board2[j][i] != null) {
|
||||
metPiece = true;
|
||||
// verify if the met piece is from the opponent
|
||||
if (board2[j][i].isWhite() != white) {
|
||||
// first 4 directions correspond to horizontal and vertical displacements
|
||||
if (p < 4 && (board2[j][i].getType() == PieceType.Rook || board2[j][i].getType() == PieceType.Queen)) {
|
||||
return true;
|
||||
}
|
||||
// last 4 directions correspond to diagonal displacements
|
||||
else if (p > 3 && (board2[j][i].getType() == PieceType.Bishop || board2[j][i].getType() == PieceType.Queen)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
i += directions[p][0];
|
||||
j += directions[p][1];
|
||||
}
|
||||
}
|
||||
// if no true statement has been returned, the piece is not in check
|
||||
return false;
|
||||
}
|
||||
|
||||
// return if the king would be left in check if the move in parameter is played
|
||||
public boolean checkIf(Move move, int kingX, int kingY, boolean white) {
|
||||
// create a copy of the actual board
|
||||
Piece[][] temporaryBoard = new Piece[lineNum][colNum];
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
Piece piece = board[j][i];
|
||||
if (piece != null) {
|
||||
temporaryBoard[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
// make the displacement in the temporary board
|
||||
temporaryBoard[move.getFromY()][move.getFromX()].setX(move.getToX());
|
||||
temporaryBoard[move.getFromY()][move.getFromX()].setY(move.getToY());
|
||||
temporaryBoard[move.getToY()][move.getToX()] = temporaryBoard[move.getFromY()][move.getFromX()];
|
||||
temporaryBoard[move.getFromY()][move.getFromX()] = null;
|
||||
|
||||
// verify if in this temporary board, the king is in check
|
||||
return check(temporaryBoard, kingX, kingY, white);
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
String str = "";
|
||||
// iterate through lines
|
||||
for (int i = 0; i < lineNum; i++) {
|
||||
// iterate through columns
|
||||
for (int j = 0; j < colNum; j++) {
|
||||
// write the corresponding letters depending on the piece on the board
|
||||
if (board[i][j] == null) {
|
||||
str += " ";
|
||||
}
|
||||
else if (board[i][j].isWhite()) {
|
||||
str += "W" + board[i][j].getType().getSummary();
|
||||
}
|
||||
else if (board[i][j].isWhite() == false) {
|
||||
str += "B" + board[i][j].getType().getSummary();
|
||||
}
|
||||
if (j != colNum-1) {
|
||||
str += ",";
|
||||
}
|
||||
}
|
||||
str += "\n";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// return if there is a checkmate
|
||||
public boolean checkmate(boolean turnWhite) {
|
||||
MoveCalculator legalMoves = new MoveCalculator(board);
|
||||
ArrayList<int[]> moves;
|
||||
|
||||
int[] kingCoordinates = findKing(turnWhite);
|
||||
|
||||
if (check(board, kingCoordinates[0], kingCoordinates[1], turnWhite)) {
|
||||
Board board2 = new Board(board);
|
||||
String state = board2.toString();
|
||||
|
||||
boolean[] booleans = new boolean[7];
|
||||
for (int k = 1; k < 8; k++) {
|
||||
if (state.charAt(k) == 'f') {
|
||||
booleans[k-1] = false;
|
||||
}
|
||||
else {
|
||||
booleans[k-1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
int[] integers = new int[4];
|
||||
for (int n = 8; n < 12; n++) {
|
||||
if (state.charAt(n) == '-') {
|
||||
integers[n-8] = -1;
|
||||
}
|
||||
else {
|
||||
integers[n-8] = Character.getNumericValue(state.charAt(n));
|
||||
}
|
||||
}
|
||||
|
||||
boolean enPassant = booleans[0];
|
||||
boolean whiteRookLeft = booleans[1];
|
||||
boolean whiteRookRight = booleans[2];
|
||||
boolean blackRookLeft = booleans[3];
|
||||
boolean blackRookRight = booleans[4];
|
||||
boolean whiteKing = booleans[5];
|
||||
boolean blackKing = booleans[6];
|
||||
int pawnX = integers[0];
|
||||
int pawnY = integers[1];
|
||||
|
||||
boolean king = whiteKing;
|
||||
boolean rookRight = whiteRookRight;
|
||||
boolean rookLeft = whiteRookLeft;
|
||||
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
Piece piece = board[j][i];
|
||||
if (piece != null && piece.isWhite() == turnWhite) {
|
||||
if (turnWhite) {
|
||||
king = whiteKing;
|
||||
rookRight = whiteRookRight;
|
||||
rookLeft = whiteRookLeft;
|
||||
}
|
||||
else {
|
||||
king = blackKing;
|
||||
rookRight = blackRookRight;
|
||||
rookLeft = blackRookLeft;
|
||||
}
|
||||
PieceType type = piece.getType();
|
||||
moves = legalMoves.getMove(type, turnWhite, i, j, king, rookRight, rookLeft, enPassant, pawnX, pawnY);
|
||||
|
||||
if (moves.size() != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -107,15 +107,8 @@ public class Game extends Thread {
|
|||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*public static boolean isCheckmate(boolean b) {
|
||||
// TODO Auto-generated method stub
|
||||
return true;
|
||||
}
|
||||
|
||||
/*public boolean isCheckmate() {
|
||||
public boolean isCheckmate() {
|
||||
return board.isCheckmate();
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,122 +59,6 @@ public class MoveCalculator {
|
|||
|
||||
// CALCULATE LEGAL MOVES
|
||||
|
||||
// return if the piece of coordinates x,y in parameter is in check or not
|
||||
public boolean check(Piece[][] board2, int x, int y, boolean white) {
|
||||
int i;
|
||||
int j;
|
||||
|
||||
// verify for knight or pawn displacements
|
||||
for (int n = 0; n < knightMoves.length; n++) {
|
||||
i = x + knightMoves[n][0];
|
||||
j = y + knightMoves[n][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
// verify if the location is empty, if not, verify if the piece is a knight from the opponent
|
||||
if (board2[j][i] != null && board2[j][i].isWhite() != white && board2[j][i].getType() == PieceType.Knight) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify for pawn displacements
|
||||
int[][] pawn;
|
||||
if (white) {
|
||||
pawn = whitePawn;
|
||||
}
|
||||
else {
|
||||
pawn = blackPawn;
|
||||
}
|
||||
// consider only the diagonal displacements
|
||||
for (int o = 2; o < 4; o++) {
|
||||
i = x + pawn[o][0];
|
||||
j = y + pawn[o][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
// verify if the location is empty, if not, verify if the piece is a pawn from the opponent
|
||||
if (board2[j][i] != null && board2[j][i].isWhite() != white && board2[j][i].getType() == PieceType.Pawn) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify for linear displacements (from directions array)
|
||||
for (int p = 0; p < directions.length; p++) {
|
||||
i = x + directions[p][0];
|
||||
j = y + directions[p][1];
|
||||
boolean metPiece = false;
|
||||
// continue until the end of the board or until a piece is met
|
||||
while (inBoard(i,j) && metPiece == false) {
|
||||
// if the location is not empty
|
||||
if (board2[j][i] != null) {
|
||||
// verify if the met piece is from the opponent
|
||||
if (board2[j][i].isWhite() != white) {
|
||||
// first 4 directions correspond to horizontal and vertical displacements
|
||||
if (p < 4 && (board2[j][i].getType() == PieceType.Rook || board2[j][i].getType() == PieceType.Queen)) {
|
||||
return true;
|
||||
}
|
||||
// last 4 directions correspond to diagonal displacements
|
||||
else if (p > 3 && (board2[j][i].getType() == PieceType.Bishop || board2[j][i].getType() == PieceType.Queen)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// if the met piece is not from the opponent, this means that in this direction, the piece is protected
|
||||
else {
|
||||
// stop the while loop to change direction
|
||||
metPiece = true;
|
||||
}
|
||||
}
|
||||
i += directions[p][0];
|
||||
j += directions[p][1];
|
||||
}
|
||||
}
|
||||
// if no true statement has been returned, the piece is not in check
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// create a temporary board
|
||||
|
||||
// return if the king would be left in check if the move in parameter is played
|
||||
public boolean checkIf(Move move, int kingX, int kingY, boolean white) {
|
||||
// create a copy of the actual board
|
||||
Piece[][] temporaryBoard = new Piece[lineNum][colNum];
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
Piece piece = board[j][i];
|
||||
if (piece != null) {
|
||||
temporaryBoard[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
// make the displacement in the temporary board
|
||||
temporaryBoard[move.getFromY()][move.getFromX()].setX(move.getToX());
|
||||
temporaryBoard[move.getFromY()][move.getFromX()].setY(move.getToY());
|
||||
temporaryBoard[move.getToY()][move.getToX()] = temporaryBoard[move.getFromY()][move.getFromX()];
|
||||
temporaryBoard[move.getFromY()][move.getFromX()] = null;
|
||||
|
||||
// verify if in this temporary board, the king is in check
|
||||
return check(temporaryBoard, kingX, kingY, white);
|
||||
}
|
||||
|
||||
// return the coordinates of the king of the color stated in parameter
|
||||
public int[] findKing(boolean white) {
|
||||
int[] coordinates = {-1, -1};
|
||||
// iterate through all columns
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
// iterate through all lines
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
// if the location is not empty, verify if it is the king from the right color
|
||||
if (board[j][i] != null && board[j][i].getType() == PieceType.King && board[j][i].isWhite() == white) {
|
||||
// update the coordinates to return
|
||||
coordinates[0] = i;
|
||||
coordinates[1] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
// calculate linear move of the piece at x,y for the direction x+x2,y+y2
|
||||
public void linearMoves(ArrayList<int[]> moves, int x, int y, int x2, int y2, boolean white) {
|
||||
int i = x + x2;
|
||||
|
|
@ -182,37 +66,21 @@ public class MoveCalculator {
|
|||
boolean metPiece = false;
|
||||
// continue until the end of the board or a piece is met
|
||||
while (metPiece == false && inBoard(i,j)) {
|
||||
Move move = new Move(x,y,i,j);
|
||||
// locate the king to know if it would be in check if the selected piece moves
|
||||
int[] coordinates = findKing(white);
|
||||
moves.add(new int[]{i, j});
|
||||
// if the location is not empty
|
||||
if (board[j][i] != null) {
|
||||
metPiece = true;
|
||||
// verify if the piece met is from the opponent and that it won't let the king in check
|
||||
if (board[j][i].isWhite() != white && checkIf(move,coordinates[0],coordinates[1],white) == false) {
|
||||
moves.add(new int[]{i, j});
|
||||
}
|
||||
}
|
||||
// if the location is empty
|
||||
else {
|
||||
// verify if the piece won't let the king in check
|
||||
if (checkIf(move,coordinates[0],coordinates[1],white) == false) {
|
||||
moves.add(new int[]{i, j});
|
||||
}
|
||||
}
|
||||
i += x2;
|
||||
j += y2;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getMove(PieceType type, boolean isWhite, int x, int y, boolean king, boolean rookRight, boolean rookLeft, boolean enPassant, int pawnX, int pawnY) {
|
||||
public ArrayList<int[]> legalMovesCalculator(ArrayList<int[]> moves, PieceType type, boolean isWhite, int x, int y) {
|
||||
// create an array list that will store the legal moves
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int i;
|
||||
int j;
|
||||
int[][] pieceMoves;
|
||||
// locate the king to later see if the displacement is possible
|
||||
int[] coordinates = findKing(isWhite);
|
||||
|
||||
// the piece selected is a rook or the queen (it moves vertically and horizontally)
|
||||
if (type == PieceType.Rook || type == PieceType.Queen) {
|
||||
|
|
@ -233,59 +101,21 @@ public class MoveCalculator {
|
|||
}
|
||||
|
||||
// the piece selected is the king
|
||||
if (type == PieceType.King) {
|
||||
|
||||
// iterates for the different possible moves
|
||||
for (int n = 0; n < kingMoves.length; n++) {
|
||||
i = x + kingMoves[n][0];
|
||||
j = y + kingMoves[n][1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
// the last 2 moves correspond to castling (right and left), so it verifies if the king and the (right or left) rook have never moved
|
||||
if (n == 8 && king == false && rookRight == false) {
|
||||
// verify if all the locations are empty and not threatened
|
||||
if (board[j][i-1] == null && board[j][i] == null && check(board,i,j,isWhite) == false && check(board,i-1,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
castlingRight = true;
|
||||
}
|
||||
}
|
||||
// same for the other one
|
||||
else if (n == 9 && king == false && rookLeft == false) {
|
||||
if (board[j][i+1] == null && board[j][i] == null && board[j][i-1] == null && check(board,i,j,isWhite) == false && check(board,i+1,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
castlingLeft = true;
|
||||
}
|
||||
}
|
||||
// if the location is not empty, verify if the piece met is from the opponent and that it won't let the king in check
|
||||
else if (n < 8 && board[j][i] != null && board[j][i].isWhite() != isWhite && check(board,i,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
// if the location is empty, verify that it won't let the king in check
|
||||
else if (n < 8 && board[j][i] == null && check(board,i,j,isWhite) == false){
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type == PieceType.King || type == PieceType.Knight) {
|
||||
if (type == PieceType.Knight) {
|
||||
pieceMoves = knightMoves;
|
||||
}
|
||||
else {
|
||||
pieceMoves = kingMoves;
|
||||
}
|
||||
|
||||
// iterates for the different possible moves
|
||||
for (int n = 0; n < knightMoves.length; n++) {
|
||||
i = x + knightMoves[n][0];
|
||||
j = y + knightMoves[n][1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
// verify if the coordinates are still in the board
|
||||
for (int n = 0; n < 8; n++) {
|
||||
i = x + pieceMoves[n][0];
|
||||
j = y + pieceMoves[n][1];
|
||||
if (inBoard(i,j)) {
|
||||
// if the location is not empty, verify if the piece met is from the opponent and that it won't let the king in check
|
||||
if (board[j][i] != null && board[j][i].isWhite() != isWhite && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
// if the location is empty, verify that it won't let the king in check
|
||||
else if (board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false){
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -301,34 +131,78 @@ public class MoveCalculator {
|
|||
|
||||
i = x + pieceMoves[1][0];
|
||||
j = y + pieceMoves[1][1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
|
||||
// verify if the location in front of the pawn is in the board and empty, and if it won't let the king in check
|
||||
if (inBoard(i,j) && board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
if (inBoard(i,j) && board[j][i] == null) {
|
||||
moves.add(new int[] {i, j});
|
||||
// verify if the pawn is at its initial location
|
||||
if (y == pieceMoves[0][0]) {
|
||||
i = x + pieceMoves[4][0];
|
||||
j = y + pieceMoves[4][1];
|
||||
move = new Move(x,y,i,j);
|
||||
// verify if the location is empty and won't let the king in check
|
||||
if (inBoard(i,j) && board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
if (inBoard(i,j)) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> specialMovesCalculator(ArrayList<int[]> moves, PieceType type, boolean isWhite, int x, int y, boolean king, boolean rookRight, boolean rookLeft, boolean enPassant, int pawnX, int pawnY) {
|
||||
// create an array list that will store the legal moves
|
||||
Check check = new Check(this.board);
|
||||
int i;
|
||||
int j;
|
||||
int[][] pieceMoves;
|
||||
|
||||
// the piece selected is the king
|
||||
if (type == PieceType.King) {
|
||||
// iterates for the different possible moves
|
||||
for (int n = 8; n < kingMoves.length; n++) {
|
||||
i = x + kingMoves[n][0];
|
||||
j = y + kingMoves[n][1];
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
if ((isWhite && y == 7) || (!isWhite && y == 0)) {
|
||||
// the last 2 moves correspond to castling (right and left), so it verifies if the king and the (right or left) rook have never moved
|
||||
if (n == 8 && king == false && rookRight == false) {
|
||||
// verify if all the locations are empty and not threatened
|
||||
if (board[j][i-1] == null && board[j][i] == null && check.check(board,i,j,isWhite) == false && check.check(board,i-1,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
castlingRight = true;
|
||||
}
|
||||
}
|
||||
// same for the other one
|
||||
else if (n == 9 && king == false && rookLeft == false) {
|
||||
if (board[j][i+1] == null && board[j][i] == null && board[j][i-1] == null && check.check(board,i,j,isWhite) == false && check.check(board,i+1,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
castlingLeft = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the piece selected is a pawn
|
||||
if (type == PieceType.Pawn) {
|
||||
// select the right color
|
||||
if (isWhite == false) {
|
||||
pieceMoves = blackPawn;
|
||||
}
|
||||
else {
|
||||
pieceMoves = whitePawn;
|
||||
}
|
||||
// verify if there is any opponent piece in diagonal that the pawn can take
|
||||
for (int n = 2; n < 4; n++) {
|
||||
i = x + pieceMoves[n][0];
|
||||
j = y + pieceMoves[n][1];
|
||||
move = new Move(x,y,i,j);
|
||||
// verify if the location is in the board and not empty, if the piece is from the opponent and if it won't let the king in check
|
||||
if (inBoard(i,j) && board[j][i] != null && board[j][i].isWhite() != isWhite && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
if (inBoard(i,j) && board[j][i] != null && board[j][i].isWhite() != isWhite) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
// if en passant is possible, and if the piece right next to the selected one (on the same line) is the pawn of the opponent that went two steps forward the previous turn
|
||||
else if(inBoard(i,j) && board[j][i] == null && enPassant == true && i == pawnX && y == pawnY && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
else if(inBoard(i,j) && board[j][i] == null && enPassant == true && i == pawnX && y == pawnY) {
|
||||
moves.add(new int[] {i, j});
|
||||
// store the coordinates of the en passant move for later
|
||||
enPassantX = i;
|
||||
|
|
@ -338,4 +212,38 @@ public class MoveCalculator {
|
|||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getMove(PieceType type, boolean isWhite, int x, int y, boolean king, boolean rookRight, boolean rookLeft, boolean enPassant, int pawnX, int pawnY) {
|
||||
// create an array list that will store the legal moves
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
legalMovesCalculator(moves, type, isWhite, x, y);
|
||||
specialMovesCalculator(moves, type, isWhite, x, y, king, rookRight, rookLeft, enPassant, pawnX, pawnY);
|
||||
Check check = new Check(this.board);
|
||||
int[] coordinates = check.findKing(isWhite);
|
||||
int k = 0;
|
||||
|
||||
while (k < moves.size()) {
|
||||
int i = moves.get(k)[0];
|
||||
int j = moves.get(k)[1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
if (board[j][i] != null && board[j][i].isWhite() == isWhite) {
|
||||
moves.remove(k);
|
||||
k -= 1;
|
||||
}
|
||||
else if (type != PieceType.King) {
|
||||
if (check.checkIf(move,coordinates[0],coordinates[1],isWhite)) {
|
||||
moves.remove(k);
|
||||
k -= 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (check.check(board, i, j, isWhite)) {
|
||||
moves.remove(k);
|
||||
k -= 1;
|
||||
}
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -262,7 +262,7 @@ public class MyInterface extends JFrame {
|
|||
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
|
||||
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
|
||||
"Playing"));
|
||||
/*if (game != null && game.isCheckmate(true)) {
|
||||
if (game != null && game.isCheckmate()) {
|
||||
String winner = turnIsWhite ? "Black" : "White"; // since it's mate, last move won
|
||||
JOptionPane.showMessageDialog(this, "Checkmate! " + winner + " wins.", "Game Over", JOptionPane.INFORMATION_MESSAGE);
|
||||
game.setDefaultSetup(); // resets the board setup
|
||||
|
|
@ -270,7 +270,7 @@ public class MyInterface extends JFrame {
|
|||
actionLabel.setText("Game reset after checkmate");
|
||||
repaint();
|
||||
return; // skip repaint below as we already called it
|
||||
}*/
|
||||
}
|
||||
|
||||
this.repaint();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue