Adding comments to the code
This commit is contained in:
parent
92c03430d4
commit
f90db6b70c
|
|
@ -12,21 +12,8 @@ public class Main {
|
|||
System.out.println(testBoard.toString());
|
||||
System.out.println(testBoard.getWidth() + "*" + testBoard.getHeight());
|
||||
|
||||
// Check for checkmate on white and black after setting up the board
|
||||
boolean isWhiteInCheckmate = CheckKing.isCheckmate(testBoard, true); // Check for white
|
||||
boolean isBlackInCheckmate = CheckKing.isCheckmate(testBoard, false); // Check for black
|
||||
|
||||
if (isWhiteInCheckmate) {
|
||||
System.out.println("White is in checkmate!");
|
||||
} else if (isBlackInCheckmate) {
|
||||
System.out.println("Black is in checkmate!");
|
||||
} else {
|
||||
System.out.println("No checkmate detected.");
|
||||
}
|
||||
|
||||
// launches graphical interface :
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -142,12 +142,12 @@ public class Board {
|
|||
String targetPos = x + "," + y;
|
||||
if (highlightedPositions.contains(targetPos)) {
|
||||
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedPiece.getX()) == 2) {
|
||||
// Castling move
|
||||
//castling move
|
||||
Castling castlingHandler = new Castling(this);
|
||||
int rookX = (x > selectedPiece.getX()) ? 7 : 0;
|
||||
castlingHandler.performCastle(selectedPiece, rookX, y);
|
||||
} else {
|
||||
// Normal move
|
||||
//normal move
|
||||
movePiece(selectedPiece.getX(), selectedPiece.getY(), x, y);
|
||||
}
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
|
@ -170,14 +170,14 @@ public class Board {
|
|||
Piece moving = board[fromY][fromX];
|
||||
Piece captured = board[toY][toX];
|
||||
|
||||
// For en passant
|
||||
//for en passant
|
||||
if (enPassant.isEnPassantCapture(moving, fromX, toX, toY, board)) {
|
||||
int capturedY = moving.isWhite() ? toY + 1 : toY - 1;
|
||||
captured = board[capturedY][toX];
|
||||
board[capturedY][toX] = null;
|
||||
}
|
||||
|
||||
// Save the move
|
||||
//save the move
|
||||
moveHistory.push(new MoveRecord(
|
||||
new Piece(moving.getX(), moving.getY(), moving.getType(), moving.isWhite()),
|
||||
captured == null ? null : new Piece(captured.getX(), captured.getY(), captured.getType(), captured.isWhite()),
|
||||
|
|
@ -185,10 +185,10 @@ public class Board {
|
|||
turnNumber, isWhiteTurn,
|
||||
enPassant.getTarget()
|
||||
));
|
||||
// En passant capture
|
||||
//en passant capture
|
||||
if (enPassant.isEnPassantCapture(moving, fromX, toX, toY, board)) {
|
||||
int capturedY = moving.isWhite() ? toY + 1 : toY - 1;
|
||||
board[capturedY][toX] = null; // Remove captured pawn
|
||||
board[capturedY][toX] = null; //remove captured pawn
|
||||
}
|
||||
|
||||
board[toY][toX] = new Piece(toX, toY, moving.getType(), moving.isWhite());
|
||||
|
|
@ -208,17 +208,17 @@ public class Board {
|
|||
int direction = isWhite ? -1 : 1;
|
||||
|
||||
switch (type) {
|
||||
case Pawn: //standard move
|
||||
case Pawn: //standard move forward
|
||||
if (inBounds(x, y + direction) && board[y + direction][x] == null) {
|
||||
validMoves.add(x + "," + (y + direction));
|
||||
}
|
||||
|
||||
boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1); //double move
|
||||
boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1); //double move from start
|
||||
if (onStartRow && board[y + direction][x] == null && board[y + 2 * direction][x] == null) {
|
||||
validMoves.add(x + "," + (y + 2 * direction));
|
||||
}
|
||||
|
||||
int[] dx = {-1, 1}; //captures
|
||||
int[] dx = {-1, 1}; //captures in diagonals
|
||||
for (int i : dx) {
|
||||
int targetX = x + i;
|
||||
int targetY = y + direction;
|
||||
|
|
@ -230,7 +230,7 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
//en passant
|
||||
//en passant capture
|
||||
int[] epTarget = enPassant.getTarget();
|
||||
if (epTarget != null && Math.abs(epTarget[0] - x) == 1 && epTarget[1] == y + direction) {
|
||||
validMoves.add(epTarget[0] + "," + epTarget[1]);
|
||||
|
|
@ -238,21 +238,21 @@ public class Board {
|
|||
|
||||
break;
|
||||
|
||||
case Rook:
|
||||
case Rook: //horizontal and vertical moves
|
||||
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:
|
||||
case Bishop: //diagonal moves
|
||||
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:
|
||||
case Queen: //combination of rook and bishop moves
|
||||
int[][] queenDirections = {
|
||||
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
|
||||
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}
|
||||
|
|
@ -262,7 +262,7 @@ public class Board {
|
|||
}
|
||||
break;
|
||||
|
||||
case Knight:
|
||||
case Knight: //L-shaped moves
|
||||
int[][] knightMoves = {
|
||||
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
|
||||
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
|
||||
|
|
@ -279,7 +279,7 @@ public class Board {
|
|||
}
|
||||
break;
|
||||
|
||||
case King:
|
||||
case King: //one square in every direction
|
||||
for (int dxKing = -1; dxKing <= 1; dxKing++) {
|
||||
for (int dyKing = -1; dyKing <= 1; dyKing++) {
|
||||
if (dxKing == 0 && dyKing == 0) continue;
|
||||
|
|
@ -307,7 +307,8 @@ public class Board {
|
|||
|
||||
if (skipKingCheck) {
|
||||
return validMoves;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return CheckKing.filterLegalMoves(this, piece, validMoves);
|
||||
}
|
||||
}
|
||||
|
|
@ -320,12 +321,12 @@ public class Board {
|
|||
while (inBounds(nx, ny)) {
|
||||
Piece target = board[ny][nx];
|
||||
|
||||
if (target == null) {
|
||||
|
||||
if (target == null) { //empty square then add a move
|
||||
valid.add(nx + "," + ny);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
|
||||
if (target.isWhite() != isWhite) {
|
||||
if (target.isWhite() != isWhite) { //enemy piece then add capture
|
||||
valid.add(nx + "," + ny);
|
||||
}
|
||||
|
||||
|
|
@ -345,7 +346,7 @@ public class Board {
|
|||
}
|
||||
|
||||
// PART3
|
||||
public String[] toFileRep() {
|
||||
public String[] toFileRep() { //board state to file representation
|
||||
String[] result = new String[lineNum + 1];
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
|
@ -354,7 +355,7 @@ public class Board {
|
|||
Piece piece = board[y][x];
|
||||
if (piece != null) {
|
||||
sb.append(piece.isWhite() ? "W" : "B");
|
||||
sb.append(piece.getType().getSummary()); // Ensure this returns K/Q/R/N/B/P
|
||||
sb.append(piece.getType().getSummary());
|
||||
}
|
||||
}
|
||||
result[y] = sb.toString();
|
||||
|
|
@ -363,8 +364,7 @@ public class Board {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
public Board(String[] array) {
|
||||
public Board(String[] array) { //creating board from file representation
|
||||
this.colNum = 8;
|
||||
this.lineNum = 8;
|
||||
this.board = new Piece[lineNum][colNum];
|
||||
|
|
@ -376,7 +376,7 @@ public class Board {
|
|||
if (token.length() == 2) {
|
||||
boolean isWhite = token.charAt(0) == 'W';
|
||||
char typeChar = token.charAt(1);
|
||||
PieceType type = PieceType.fromSummary(typeChar); // You must implement this method
|
||||
PieceType type = PieceType.fromSummary(typeChar);
|
||||
board[y][x] = new Piece(x, y, type, isWhite);
|
||||
}
|
||||
}
|
||||
|
|
@ -431,7 +431,7 @@ public class Board {
|
|||
|
||||
this.isWhiteTurn = other.isWhiteTurn;
|
||||
this.turnNumber = other.turnNumber;
|
||||
this.enPassant = new EnPassant(); // you can improve this if EnPassant has a copy constructor
|
||||
this.enPassant = new EnPassant();
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
|
|
@ -439,7 +439,7 @@ public class Board {
|
|||
isWhiteTurn = !isWhiteTurn;
|
||||
turnNumber++;
|
||||
}
|
||||
private class MoveRecord {
|
||||
private class MoveRecord { //recording moves for undo functionality
|
||||
final Piece movedPiece;
|
||||
final Piece capturedPiece;
|
||||
final int fromX, fromY, toX, toY;
|
||||
|
|
|
|||
Loading…
Reference in New Issue