fixed issue

This commit is contained in:
Lucie 2025-05-14 14:21:43 +02:00
parent 5a54286394
commit ba320bcdd6
3 changed files with 26 additions and 39 deletions

View File

@ -12,7 +12,7 @@ public class Main {
// testing :
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
//System.out.println(testBoard.toString());
System.out.println(testBoard.toString());
// launches graphical interface :
MyInterface mjf = new MyInterface();
mjf.setVisible(true);

View File

@ -315,10 +315,10 @@ public class Board {
String[] line;
boolean white;
for (int j = 0; j < lineNum; j++) {
for (int j = 0; j < colNum; j++) {
line = array[j].split(",");
for (int i = 0; i < colNum; i++) {
if (line[i] != " ") {
for (int i = 0; i < lineNum; i++) {
if (line[i].charAt(0) != ' ') {
white = false;
if (line[i].charAt(0) == 'W') {
white = true;
@ -374,32 +374,19 @@ public class Board {
if (this.turns > 0) {
states.remove(this.turns);
this.turns = turns - 1;
this.board = new Piece[lineNum][colNum];
//this.board = new Piece[lineNum][colNum];
this.x = -1;
this.y = -1;
String[] undo = states.get(turns).split("\n");
String[] line;
boolean white;
Board newBoard = new Board(undo);
this.board = newBoard.board;
for (int j = 0; j < colNum; j++) {
line = undo[j].split(",");
for (int i = 0; i < lineNum; i++) {
if (line[i].charAt(0) != ' ') {
white = false;
if (line[i].charAt(0) == 'W') {
white = true;
}
Piece piece = new Piece(white, PieceType.fromSummary(line[i].charAt(1)), i, j);
board[j][i] = new Piece(piece);
}
}
}
if (undo[lineNum].charAt(0) == 'B' || turns == 0) {
this.whiteTurn = true;
if (undo[lineNum] == "B" || turns == 0) {
whiteTurn = true;
}
else {
this.whiteTurn = false;
whiteTurn = false;
}
}

View File

@ -3,7 +3,7 @@ package backend;
import java.util.ArrayList;
public class MoveCalculator {
private Piece[][] board1;
private Piece[][] board;
private int colNum;
private int lineNum;
private int[][] knightMoves;
@ -18,7 +18,7 @@ public class MoveCalculator {
ArrayList<int[]> moves;
public MoveCalculator(Piece[][] boardF) {
board1 = 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}};
@ -141,7 +141,7 @@ public class MoveCalculator {
Piece[][] temporaryBoard = new Piece[lineNum][colNum];
for (int i = 0; i < colNum; i++) {
for (int j = 0; j < lineNum; j++) {
Piece piece = board1[j][i];
Piece piece = board[j][i];
if (piece != null) {
temporaryBoard[j][i] = new Piece(piece);
}
@ -165,7 +165,7 @@ public class MoveCalculator {
// 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 (board1[j][i] != null && board1[j][i].getType() == PieceType.King && board1[j][i].isWhite() == white) {
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;
@ -186,10 +186,10 @@ public class MoveCalculator {
// locate the king to know if it would be in check if the selected piece moves
int[] coordinates = findKing(white);
// if the location is not empty
if (board1[j][i] != null) {
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 (board1[j][i].isWhite() != white && checkIf(move,coordinates[0],coordinates[1],white) == false) {
if (board[j][i].isWhite() != white && checkIf(move,coordinates[0],coordinates[1],white) == false) {
moves.add(new int[]{i, j});
}
}
@ -245,24 +245,24 @@ public class MoveCalculator {
// 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 (board1[j][i-1] == null && board1[j][i] == null && check(board1,i,j,isWhite) == false && check(board1,i-1,j,isWhite) == false) {
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 (board1[j][i+1] == null && board1[j][i] == null && board1[j][i-1] == null && check(board1,i,j,isWhite) == false && check(board1,i+1,j,isWhite) == 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 && board1[j][i] != null && board1[j][i].isWhite() != isWhite && check(board1,i,j,isWhite) == false) {
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 && board1[j][i] == null && check(board1,i,j,isWhite) == false){
else if (n < 8 && board[j][i] == null && check(board,i,j,isWhite) == false){
moves.add(new int[] {i, j});
}
}
@ -278,11 +278,11 @@ public class MoveCalculator {
// verify if the coordinates are still in the board
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 (board1[j][i] != null && board1[j][i].isWhite() != isWhite && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
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 (board1[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false){
else if (board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false){
moves.add(new int[] {i, j});
}
}
@ -304,7 +304,7 @@ public class MoveCalculator {
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) && board1[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
if (inBoard(i,j) && board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
moves.add(new int[] {i, j});
// verify if the pawn is at its initial location
if (y == pieceMoves[0][0]) {
@ -312,7 +312,7 @@ public class MoveCalculator {
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) && board1[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
if (inBoard(i,j) && board[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
moves.add(new int[] {i, j});
}
}
@ -324,11 +324,11 @@ public class MoveCalculator {
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) && board1[j][i] != null && board1[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 && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
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) && board1[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 && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
moves.add(new int[] {i, j});
// store the coordinates of the en passant move for later
enPassantX = i;