adding all the movements possibility and highlighting the squares.

This commit is contained in:
Elève 2025-04-18 16:57:13 +02:00
parent 18c29cd2df
commit dc8c3d1faa
1 changed files with 296 additions and 195 deletions

View File

@ -11,6 +11,7 @@ public class Board {
private int width; private int width;
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
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
@ -34,15 +35,14 @@ public class Board {
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
if (turnNumber % 2 == 0) { // even turns including O are white's ones if (turnNumber % 2 == 0) { // even turns including 0 are white's ones
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() { //theses classes change the turn and the increment one solve a problem of infinite loop public int getTurnNumber() { // these classes change the turn and the increment one solves a problem of infinite loop
return turnNumber; return turnNumber;
} }
@ -86,12 +86,11 @@ public class Board {
public void cleanBoard() { public void cleanBoard() {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
board[x][y] = null; //each position become empty board[x][y] = null; // each position becomes empty
} }
} }
} }
private void clearConsole() { private void clearConsole() {
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
System.out.println(); // Print 50 empty lines to "clear" the console System.out.println(); // Print 50 empty lines to "clear" the console
@ -100,7 +99,6 @@ public class Board {
public String toString() { public String toString() {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
str.append(" A B C D E F G H\n"); // columns letter at the top str.append(" A B C D E F G H\n"); // columns letter at the top
// representation of the rows // representation of the rows
@ -111,19 +109,18 @@ public class Board {
if (board[x][y] == null) { if (board[x][y] == null) {
str.append("- "); // empty positions str.append("- "); // empty positions
} else { } else {
// convert each piece of both color into a character // convert each piece of both color into a character
Piece piece = board[x][y]; Piece piece = board[x][y];
char pieceChar; //one character char pieceChar;
switch(piece.getType()) { //switch function solve the problem of too many if-else switch (piece.getType()) { // switch function avoids too many if-else
case King: pieceChar = 'K'; break; //case is reading in king and break stop the switch function case King: pieceChar = 'K'; break;
case Queen: pieceChar = 'Q'; break; case Queen: pieceChar = 'Q'; break;
case Bishop: pieceChar = 'B'; break; case Bishop: pieceChar = 'B'; break;
case Knight: pieceChar = 'N'; break; // N because we already have King case Knight: pieceChar = 'N'; break; // N because we already have King
case Rook: pieceChar = 'R'; break; case Rook: pieceChar = 'R'; break;
case Pawn: pieceChar = 'P'; break; case Pawn: pieceChar = 'P'; break;
default: pieceChar = '?'; break; //solve an issue of unresolved compilation and act as a safety net default: pieceChar = '?'; break; // safety net
} }
// Make black pieces in lowercase // Make black pieces in lowercase
@ -137,9 +134,9 @@ public class Board {
str.append("\n"); // change of row str.append("\n"); // change of row
} }
// Additional infos for a proper ouput // Additional infos for a proper output
str.append("Turn ").append(getTurnNumber()).append(": "); //turnnumber str.append("Turn ").append(getTurnNumber()).append(": ");
str.append(isTurnWhite() ? "White" : "Black"); //color's turn str.append(isTurnWhite() ? "White" : "Black");
return str.toString(); return str.toString();
} }
@ -147,7 +144,7 @@ public class Board {
// list the placement of the pieces on the board // list the placement of the pieces on the board
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
//collect infos for the empty positions // collect infos for the non-empty positions
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
if (board[x][y] != null) { if (board[x][y] != null) {
@ -158,51 +155,172 @@ public class Board {
return pieces; return pieces;
} }
//user click on the board // user clicks on the board
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
// 1: no position was previously selected
if (selectedX == -1 && selectedY == -1) { if (selectedX == -1 && selectedY == -1) {
// check if the position is empty and the color // check if the position is empty and the color
if (board[x][y] != null && board[x][y].isWhite() == isTurnWhite()) { if (board[x][y] != null && board[x][y].isWhite() == isTurnWhite()) {
// 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
} }
} } else {
// 2: position was already selected
else {
// 2.1: click on the same position
if (x == selectedX && y == selectedY) { if (x == selectedX && y == selectedY) {
// unselect it // unselect it
selectedX = -1; selectedX = -1;
selectedY = -1; selectedY = -1;
highlightedPositions.clear();
} else {
// move if valid destination
boolean valid = false;
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
valid = true;
break;
} }
// 2.2: click on a different position }
else {
// Move piece if (valid) {
Piece pieceToMove = board[selectedX][selectedY]; Piece pieceToMove = board[selectedX][selectedY];
board[x][y] = new Piece(x, y, pieceToMove.getType(), pieceToMove.isWhite()); board[x][y] = new Piece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
board[selectedX][selectedY] = null; board[selectedX][selectedY] = null;
incrementTurn();
}
// reset the choose of position // reset selection
selectedX = -1; selectedX = -1;
selectedY = -1; selectedY = -1;
highlightedPositions.clear();
//update the game
incrementTurn();
clearConsole(); clearConsole();
System.out.println(toString()); System.out.println(toString());
} }
} }
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return (x == selectedX && y == selectedY); // true if matching position return (x == selectedX && y == selectedY); // true if matching position
} }
/* saving-loading feature :*/ public boolean isHighlighted(int x, int y) {
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) {
return true;
}
}
return false;
}
/* utility methods */
private boolean isInBounds(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < height;
}
private void addLinearMoves(ArrayList<int[]> moves, int x, int y, Piece piece, int dx, int dy) {
int nx = x + dx;
int ny = y + dy;
while (isInBounds(nx, ny)) {
if (board[nx][ny] == null) {
moves.add(new int[]{nx, ny});
} else {
if (board[nx][ny].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny});
}
break;
}
nx += dx;
ny += dy;
}
}
private ArrayList<int[]> getValidMoves(Piece piece) {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
switch (piece.getType()) {
case Pawn:
int direction = piece.isWhite() ? -1 : 1;
int nextY = y + direction;
// forward move
if (isInBounds(x, nextY) && board[x][nextY] == null) {
moves.add(new int[]{x, nextY});
// double move from starting position
int startRow = piece.isWhite() ? 6 : 1;
int doubleStepY = y + 2 * direction;
if (y == startRow && isInBounds(x, doubleStepY) && board[x][doubleStepY] == null) {
moves.add(new int[]{x, doubleStepY});
}
}
// diagonal captures
for (int dx = -1; dx <= 1; dx += 2) {
int nx = x + dx;
if (isInBounds(nx, nextY) && board[nx][nextY] != null && board[nx][nextY].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, nextY});
}
}
break;
case Rook:
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);
break;
case Bishop:
addLinearMoves(moves, x, y, piece, 1, 1);
addLinearMoves(moves, x, y, piece, -1, 1);
addLinearMoves(moves, x, y, piece, 1, -1);
addLinearMoves(moves, x, y, piece, -1, -1);
break;
case Queen:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx != 0 || dy != 0) {
addLinearMoves(moves, x, y, piece, dx, dy);
}
}
}
break;
case King:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx != 0 || dy != 0) {
int nx = x + dx;
int ny = y + dy;
if (isInBounds(nx, ny) && (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite())) {
moves.add(new int[]{nx, ny});
}
}
}
}
break;
case Knight:
int[][] jumps = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{-1, -2}, {-2, -1}, {1, -2}, {2, -1}
};
for (int[] j : jumps) {
int nx = x + j[0];
int ny = y + j[1];
if (isInBounds(nx, ny) && (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite())) {
moves.add(new int[]{nx, ny});
}
}
break;
}
return moves;
}
/* saving-loading feature : */
public String[] toFileRep() { public String[] toFileRep() {
// TODO // TODO
return null; return null;
@ -210,35 +328,18 @@ public class Board {
public Board(String[] array) { public Board(String[] array) {
// TODO // TODO
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
} }
/* additional functionality to implement later */
public void undoLastMove() { public void undoLastMove() {
// TODO // TODO
}
public Board(Board board) {
//TODO
} }
public void playMove(Move move) { public void playMove(Move move) {
// TODO // TODO
} }
public boolean isEmpty(int x, int newY) { public Board(Board board) {
// TODO Auto-generated method stub // TODO
return false;
} }
} }