king check

This commit is contained in:
utente 2025-05-09 09:15:32 +02:00
parent f538efe699
commit 9e1e303d6c
1 changed files with 63 additions and 50 deletions

View File

@ -30,10 +30,15 @@ public class Board {
return lineNum;
}
public int getTurnNumber() {
public int getTurnNumber() { //for checking
return turnNumber;
}
public Piece[][] getBoardMatrix() {
return board;
}
public boolean isTurnWhite() {
return isWhiteTurn;
}
@ -131,7 +136,7 @@ public class Board {
return highlightedPositions.contains(x + "," + y);
}
private void movePiece(int fromX, int fromY, int toX, int toY) {
void movePiece(int fromX, int fromY, int toX, int toY) { //from private to not
Piece moving = board[fromY][fromX];
// En passant capture
@ -146,52 +151,31 @@ public class Board {
enPassant.updateTarget(moving, fromY, toY, toX);
}
private Set<String> getValidMoves(Piece piece) {
public Set<String> getValidMoves(Piece piece, boolean skipKingCheck) {
Set<String> validMoves = new HashSet<>();
int x = piece.getX(); // Current position coordinate x
int y = piece.getY(); // Current position coordinate y
boolean isWhite = piece.isWhite(); // Returns true if a piece is white, if black then false
PieceType type = piece.getType(); // What type of piece is considered (pawn, rook...)
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
PieceType type = piece.getType();
// Direction (y coordinate change): White moves "up" (-1), Black moves "down" (+1)
int direction = isWhite ? -1 : 1;
switch (type) {
case Pawn: // moves one square forward (1st move 2 squares possible)
// Move one step forward (if the square in front is inside the board and empty)
case Pawn:
if (inBounds(x, y + direction) && board[y + direction][x] == null) {
validMoves.add(x + "," + (y + direction));
}
// First move: two steps forward (initial position - white piece 6th row, black 1st row)
boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1);
// The square in front and one after that must be empty
if (onStartRow && board[y + direction][x] == null && board[y + 2 * direction][x] == null) {
validMoves.add(x + "," + (y + 2 * direction));
}
// Capture diagonally
int[] dx = {-1, 1};
for (int i : dx) {
int targetX = x + i;
int targetY = y + direction;
if (inBounds(targetX, targetY)) {
Piece target = board[targetY][targetX];
if (target != null && target.isWhite() != isWhite) {
validMoves.add(targetX + "," + targetY);
}
}
}
int[] epTarget = enPassant.getTarget();
if (epTarget != null && Math.abs(epTarget[0] - x) == 1 && epTarget[1] == y + direction) {
validMoves.add(epTarget[0] + "," + epTarget[1]);
}
// Checking diagonal squares in both directions
for (int i : dx) {
int targetX = x + i;
int targetY = y + direction;
// Checking if the target square is inside the board and contains opponent's piece
if (inBounds(targetX, targetY)) {
Piece target = board[targetY][targetX];
if (target != null && target.isWhite() != isWhite) {
@ -199,23 +183,29 @@ public class Board {
}
}
}
int[] epTarget = enPassant.getTarget();
if (epTarget != null && Math.abs(epTarget[0] - x) == 1 && epTarget[1] == y + direction) {
validMoves.add(epTarget[0] + "," + epTarget[1]);
}
break;
case Rook: // Moves in straight lines
addLinearMoves(validMoves, x, y, isWhite, 1, 0); // Right
addLinearMoves(validMoves, x, y, isWhite, -1, 0); // Left
addLinearMoves(validMoves, x, y, isWhite, 0, 1); // Down
addLinearMoves(validMoves, x, y, isWhite, 0, -1); // Up
case Rook:
addLinearMoves(validMoves, x, y, isWhite, 1, 0);
addLinearMoves(validMoves, x, y, isWhite, -1, 0);
addLinearMoves(validMoves, x, y, isWhite, 0, 1);
addLinearMoves(validMoves, x, y, isWhite, 0, -1);
break;
case Bishop: // Moves diagonally
addLinearMoves(validMoves, x, y, isWhite, 1, 1); // Down-Right
addLinearMoves(validMoves, x, y, isWhite, 1, -1); // Up-Right
addLinearMoves(validMoves, x, y, isWhite, -1, 1); // Down-Left
addLinearMoves(validMoves, x, y, isWhite, -1, -1); // Up-Left
case Bishop:
addLinearMoves(validMoves, x, y, isWhite, 1, 1);
addLinearMoves(validMoves, x, y, isWhite, 1, -1);
addLinearMoves(validMoves, x, y, isWhite, -1, 1);
addLinearMoves(validMoves, x, y, isWhite, -1, -1);
break;
case Queen: // Combines Rook + Bishop movements
case Queen:
int[][] queenDirections = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}
@ -225,7 +215,7 @@ public class Board {
}
break;
case Knight: // Jumps in L shapes (3 straight, 1 to the side)
case Knight:
int[][] knightMoves = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
@ -242,7 +232,7 @@ public class Board {
}
break;
case King: // Moves 1 square in any direction
case King:
for (int dxKing = -1; dxKing <= 1; dxKing++) {
for (int dyKing = -1; dyKing <= 1; dyKing++) {
if (dxKing == 0 && dyKing == 0) continue;
@ -259,9 +249,14 @@ public class Board {
break;
}
return validMoves; // Returning the list of valid moves for each piece
if (skipKingCheck) {
return validMoves;
} else {
return CheckKing.filterLegalMoves(this, piece, validMoves);
}
}
private void addLinearMoves(Set<String> valid, int x, int y, boolean isWhite, int dx, int dy) {
int nx = x + dx;
int ny = y + dy;
@ -270,22 +265,24 @@ public class Board {
Piece target = board[ny][nx];
if (target == null) {
// If empty square then add valid move
valid.add(nx + "," + ny);
} else {
// If it's an opponent's piece capture is allowed
if (target.isWhite() != isWhite) {
valid.add(nx + "," + ny);
}
// Stop moving further (can't jump over pieces)
break;
}
// Move one step further in the same direction
nx += dx;
ny += dy;
}
}
private Set<String> getValidMoves(Piece piece) {
return getValidMoves(piece, false);
}
private boolean inBounds(int x, int y) {
return x >= 0 && y >= 0 && x < colNum && y < lineNum;
@ -293,7 +290,7 @@ public class Board {
// PART3
public String[] toFileRep() {
String[] result = new String[lineNum + 1]; // One line per row + 1 for turn
String[] result = new String[lineNum + 1];
for (int y = 0; y < lineNum; y++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < colNum; x++) {
@ -335,9 +332,25 @@ public class Board {
public void undoLastMove() {
//TODO
}
public Board(Board board) {
//TODO
public Board(Board other) {
this.colNum = other.colNum;
this.lineNum = other.lineNum;
this.board = new Piece[lineNum][colNum];
for (int y = 0; y < lineNum; y++) {
for (int x = 0; x < colNum; x++) {
Piece p = other.board[y][x];
if (p != null) {
this.board[y][x] = new Piece(p.getX(), p.getY(), p.getType(), p.isWhite());
}
}
}
this.isWhiteTurn = other.isWhiteTurn;
this.turnNumber = other.turnNumber;
this.enPassant = new EnPassant(); // you can improve this if EnPassant has a copy constructor
}
public void playMove(Move move) {
//TODO
}