king check
This commit is contained in:
parent
f538efe699
commit
9e1e303d6c
|
|
@ -30,10 +30,15 @@ public class Board {
|
||||||
return lineNum;
|
return lineNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTurnNumber() {
|
public int getTurnNumber() { //for checking
|
||||||
return turnNumber;
|
return turnNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Piece[][] getBoardMatrix() {
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isTurnWhite() {
|
public boolean isTurnWhite() {
|
||||||
return isWhiteTurn;
|
return isWhiteTurn;
|
||||||
}
|
}
|
||||||
|
|
@ -131,7 +136,7 @@ public class Board {
|
||||||
return highlightedPositions.contains(x + "," + y);
|
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];
|
Piece moving = board[fromY][fromX];
|
||||||
|
|
||||||
// En passant capture
|
// En passant capture
|
||||||
|
|
@ -146,52 +151,31 @@ public class Board {
|
||||||
enPassant.updateTarget(moving, fromY, toY, toX);
|
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<>();
|
Set<String> validMoves = new HashSet<>();
|
||||||
|
|
||||||
int x = piece.getX(); // Current position coordinate x
|
int x = piece.getX();
|
||||||
int y = piece.getY(); // Current position coordinate y
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite(); // Returns true if a piece is white, if black then false
|
boolean isWhite = piece.isWhite();
|
||||||
PieceType type = piece.getType(); // What type of piece is considered (pawn, rook...)
|
PieceType type = piece.getType();
|
||||||
|
|
||||||
// Direction (y coordinate change): White moves "up" (-1), Black moves "down" (+1)
|
|
||||||
int direction = isWhite ? -1 : 1;
|
int direction = isWhite ? -1 : 1;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Pawn: // moves one square forward (1st move 2 squares possible)
|
case Pawn:
|
||||||
// Move one step forward (if the square in front is inside the board and empty)
|
|
||||||
if (inBounds(x, y + direction) && board[y + direction][x] == null) {
|
if (inBounds(x, y + direction) && board[y + direction][x] == null) {
|
||||||
validMoves.add(x + "," + (y + direction));
|
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);
|
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) {
|
if (onStartRow && board[y + direction][x] == null && board[y + 2 * direction][x] == null) {
|
||||||
validMoves.add(x + "," + (y + 2 * direction));
|
validMoves.add(x + "," + (y + 2 * direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capture diagonally
|
|
||||||
int[] dx = {-1, 1};
|
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) {
|
for (int i : dx) {
|
||||||
int targetX = x + i;
|
int targetX = x + i;
|
||||||
int targetY = y + direction;
|
int targetY = y + direction;
|
||||||
// Checking if the target square is inside the board and contains opponent's piece
|
|
||||||
if (inBounds(targetX, targetY)) {
|
if (inBounds(targetX, targetY)) {
|
||||||
Piece target = board[targetY][targetX];
|
Piece target = board[targetY][targetX];
|
||||||
if (target != null && target.isWhite() != isWhite) {
|
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;
|
break;
|
||||||
|
|
||||||
case Rook: // Moves in straight lines
|
case Rook:
|
||||||
addLinearMoves(validMoves, x, y, isWhite, 1, 0); // Right
|
addLinearMoves(validMoves, x, y, isWhite, 1, 0);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, -1, 0); // Left
|
addLinearMoves(validMoves, x, y, isWhite, -1, 0);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, 0, 1); // Down
|
addLinearMoves(validMoves, x, y, isWhite, 0, 1);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, 0, -1); // Up
|
addLinearMoves(validMoves, x, y, isWhite, 0, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Bishop: // Moves diagonally
|
case Bishop:
|
||||||
addLinearMoves(validMoves, x, y, isWhite, 1, 1); // Down-Right
|
addLinearMoves(validMoves, x, y, isWhite, 1, 1);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, 1, -1); // Up-Right
|
addLinearMoves(validMoves, x, y, isWhite, 1, -1);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, -1, 1); // Down-Left
|
addLinearMoves(validMoves, x, y, isWhite, -1, 1);
|
||||||
addLinearMoves(validMoves, x, y, isWhite, -1, -1); // Up-Left
|
addLinearMoves(validMoves, x, y, isWhite, -1, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Queen: // Combines Rook + Bishop movements
|
case Queen:
|
||||||
int[][] queenDirections = {
|
int[][] queenDirections = {
|
||||||
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
|
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
|
||||||
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}
|
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}
|
||||||
|
|
@ -225,7 +215,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Knight: // Jumps in L shapes (3 straight, 1 to the side)
|
case Knight:
|
||||||
int[][] knightMoves = {
|
int[][] knightMoves = {
|
||||||
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
|
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
|
||||||
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
|
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
|
||||||
|
|
@ -242,7 +232,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case King: // Moves 1 square in any direction
|
case King:
|
||||||
for (int dxKing = -1; dxKing <= 1; dxKing++) {
|
for (int dxKing = -1; dxKing <= 1; dxKing++) {
|
||||||
for (int dyKing = -1; dyKing <= 1; dyKing++) {
|
for (int dyKing = -1; dyKing <= 1; dyKing++) {
|
||||||
if (dxKing == 0 && dyKing == 0) continue;
|
if (dxKing == 0 && dyKing == 0) continue;
|
||||||
|
|
@ -259,9 +249,14 @@ public class Board {
|
||||||
break;
|
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) {
|
private void addLinearMoves(Set<String> valid, int x, int y, boolean isWhite, int dx, int dy) {
|
||||||
int nx = x + dx;
|
int nx = x + dx;
|
||||||
int ny = y + dy;
|
int ny = y + dy;
|
||||||
|
|
@ -270,22 +265,24 @@ public class Board {
|
||||||
Piece target = board[ny][nx];
|
Piece target = board[ny][nx];
|
||||||
|
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
// If empty square then add valid move
|
|
||||||
valid.add(nx + "," + ny);
|
valid.add(nx + "," + ny);
|
||||||
} else {
|
} else {
|
||||||
// If it's an opponent's piece → capture is allowed
|
|
||||||
if (target.isWhite() != isWhite) {
|
if (target.isWhite() != isWhite) {
|
||||||
valid.add(nx + "," + ny);
|
valid.add(nx + "," + ny);
|
||||||
}
|
}
|
||||||
// Stop moving further (can't jump over pieces)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move one step further in the same direction
|
|
||||||
nx += dx;
|
nx += dx;
|
||||||
ny += dy;
|
ny += dy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private Set<String> getValidMoves(Piece piece) {
|
||||||
|
return getValidMoves(piece, false);
|
||||||
|
}
|
||||||
|
|
||||||
private boolean inBounds(int x, int y) {
|
private boolean inBounds(int x, int y) {
|
||||||
return x >= 0 && y >= 0 && x < colNum && y < lineNum;
|
return x >= 0 && y >= 0 && x < colNum && y < lineNum;
|
||||||
|
|
@ -293,7 +290,7 @@ public class Board {
|
||||||
|
|
||||||
// PART3
|
// PART3
|
||||||
public String[] toFileRep() {
|
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++) {
|
for (int y = 0; y < lineNum; y++) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int x = 0; x < colNum; x++) {
|
for (int x = 0; x < colNum; x++) {
|
||||||
|
|
@ -335,9 +332,25 @@ public class Board {
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
public Board(Board board) {
|
public Board(Board other) {
|
||||||
//TODO
|
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) {
|
public void playMove(Move move) {
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue