This commit is contained in:
mathy 2025-05-06 15:00:16 +02:00
commit b2a57c85ec
2 changed files with 15 additions and 19 deletions

View File

@ -7,8 +7,8 @@ public class Board {
private int selectedX = -1; // negative value means impossible x and y so unselected private int selectedX = -1; // negative value means impossible x and y so unselected
private int selectedY = -1; private int selectedY = -1;
private int turnNumber = 0; // track current turn private int turnNumber = 0; // tracks current turn
private int width; private int width; // enables to define the dimensions of board
private int height; private int height;
private Piece[][] board; // 2D array chess board private Piece[][] board; // 2D array chess board
private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // list of valid positions to highlight private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // list of valid positions to highlight
@ -16,7 +16,7 @@ public class Board {
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
this.height = lineNum; this.height = lineNum;
this.board = new Piece[width][height]; // first empty board this.board = new Piece[width][height]; // first empty board *********REVIEW************
clearConsole(); clearConsole();
System.out.println(toString()); // print the chess at the beginning of the game System.out.println(toString()); // print the chess at the beginning of the game
} }
@ -29,28 +29,28 @@ public class Board {
return height; return height;
} }
// new piece on the board at x,y // new piece on the board at x,y (More specifically changes the empty cell of coordinates x,y with a new chess piece)
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[x][y] = new Piece(x, y, type, isWhite); board[x][y] = new Piece(x, y, type, isWhite);
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
if (turnNumber % 2 == 0) { // even turns including 0 are white's ones if (turnNumber % 2 == 0) { // even turns including 0 are white's ones (% calculates the reminder of the euclidean division)
return true; return true;
} else { // same reasoning, odd turns are black's ones } else { // same reasoning, odd turns are black's ones
return false; return false;
} }
} }
public int getTurnNumber() { // these classes change the turn and the increment one solves a problem of infinite loop public int getTurnNumber() { // this class enables to obtain the current turn number while increment adds 1 to this value for each turn
return turnNumber; return turnNumber; // Necessarly in two functions to get rid of an infinite loop ****WHY****
} }
public void incrementTurn() { public void incrementTurn() {
turnNumber++; turnNumber++;
} }
// set up the chess board taking it as a matrix // set up the classic chess board taking it as a matrix and putting each corresponding starting piece at its place 0,0 is the top left spot of the board
public void populateBoard() { public void populateBoard() {
// Black // Black
setPiece(false, PieceType.Rook, 0, 0); setPiece(false, PieceType.Rook, 0, 0);
@ -154,7 +154,7 @@ public class Board {
// select it as active location // select it as active location
selectedX = x; selectedX = x;
selectedY = y; selectedY = y;
highlightedPositions = getValidMoves(board[x][y]); // compute moves highlightedPositions = getValidMoves(board[x][y]); // compute valid moves
} }
} else { } else {
if (x == selectedX && y == selectedY) { if (x == selectedX && y == selectedY) {
@ -163,7 +163,7 @@ public class Board {
selectedY = -1; selectedY = -1;
highlightedPositions.clear(); highlightedPositions.clear();
} else { } else {
// move if valid destination // allow move if valid destination
boolean valid = false; boolean valid = false;
for (int[] pos : highlightedPositions) { for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) { if (pos[0] == x && pos[1] == y) {
@ -193,7 +193,7 @@ public class Board {
return (x == selectedX && y == selectedY); // true if matching position return (x == selectedX && y == selectedY); // true if matching position
} }
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) { // checking for a given position if the square is highlighted or not
for (int[] pos : highlightedPositions) { for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) { if (pos[0] == x && pos[1] == y) {
return true; return true;
@ -254,8 +254,8 @@ public class Board {
} }
} }
break; break;
//for each piece, we calculate the positions it can end up in from an initial position
case Rook: case Rook:
addLinearMoves(moves, x, y, piece, 1, 0); addLinearMoves(moves, x, y, piece, 1, 0);
addLinearMoves(moves, x, y, piece, -1, 0); addLinearMoves(moves, x, y, piece, -1, 0);
addLinearMoves(moves, x, y, piece, 0, 1); addLinearMoves(moves, x, y, piece, 0, 1);
@ -295,7 +295,7 @@ public class Board {
case Knight: case Knight:
int[][] jumps = { int[][] jumps = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, {1, 2}, {2, 1}, {-1, 2}, {-2, 1}, // possible moves for
{-1, -2}, {-2, -1}, {1, -2}, {2, -1} {-1, -2}, {-2, -1}, {1, -2}, {2, -1}
}; };
for (int[] j : jumps) { for (int[] j : jumps) {

View File

@ -1,11 +1,7 @@
package backend; package backend;
import java.util.Optional; import java.util.ArrayList;
/**
* Represents a chess move, including the starting and ending positions,
* the moving piece, and an optional captured piece.
*/
public class Move { public class Move {
} }