part 2 complete, the game works
This commit is contained in:
parent
22e014acee
commit
1916f1f86e
|
|
@ -12,6 +12,7 @@ public class Board {
|
||||||
private int selectedY = -1;
|
private int selectedY = -1;
|
||||||
private int turnNumber = 0;
|
private int turnNumber = 0;
|
||||||
private boolean turnWhite = true;
|
private boolean turnWhite = true;
|
||||||
|
private ArrayList<int[]> highlightedSquares = new ArrayList<>();
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum;
|
this.width = colNum;
|
||||||
|
|
@ -122,8 +123,117 @@ public class Board {
|
||||||
return hasSelectedPiece && selectedX == x && selectedY == y;
|
return hasSelectedPiece && selectedX == x && selectedY == y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean inBounds(int x, int y) {
|
||||||
|
return x >= 0 && x < width && y >= 0 && y < height;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private ArrayList<int[]> getValidMoves(Piece piece) {
|
||||||
|
ArrayList<int[]> moves = new ArrayList<>();
|
||||||
|
int x = piece.getX();
|
||||||
|
int y = piece.getY();
|
||||||
|
PieceType type = piece.getType();
|
||||||
|
|
||||||
|
if (type == PieceType.Pawn) {
|
||||||
|
int dir = piece.isWhite() ? 1 : -1;
|
||||||
|
int startRow = piece.isWhite() ? 1 : 6;
|
||||||
|
|
||||||
|
int oneStep = y + dir;
|
||||||
|
if (inBounds(x, oneStep) && board[x][oneStep] == null) {
|
||||||
|
moves.add(new int[]{x, oneStep});
|
||||||
|
|
||||||
|
// If on start row, try two steps forward
|
||||||
|
int twoStep = y + 2 * dir;
|
||||||
|
if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null) {
|
||||||
|
moves.add(new int[]{x, twoStep});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Capture diagonals
|
||||||
|
for (int dx : new int[]{-1, 1}) {
|
||||||
|
int nx = x + dx;
|
||||||
|
int ny = y + dir;
|
||||||
|
if (inBounds(nx, ny) && board[nx][ny] != null &&
|
||||||
|
board[nx][ny].isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (type == PieceType.Rook) {
|
||||||
|
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
|
||||||
|
for (int[] d : dirs) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
while (inBounds(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 += d[0]; ny += d[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.Bishop) {
|
||||||
|
int[][] dirs = {{1,1}, {-1,1}, {-1,-1}, {1,-1}};
|
||||||
|
for (int[] d : dirs) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
while (inBounds(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 += d[0]; ny += d[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.Queen) {
|
||||||
|
// Queen = Rook + Bishop
|
||||||
|
moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Rook)));
|
||||||
|
moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Bishop)));
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.Knight) {
|
||||||
|
int[][] jumps = {
|
||||||
|
{2,1},{1,2},{-1,2},{-2,1},
|
||||||
|
{-2,-1},{-1,-2},{1,-2},{2,-1}
|
||||||
|
};
|
||||||
|
for (int[] j : jumps) {
|
||||||
|
int nx = x + j[0], ny = y + j[1];
|
||||||
|
if (inBounds(nx, ny)) {
|
||||||
|
if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (type == PieceType.King) {
|
||||||
|
int[][] dirs = {
|
||||||
|
{1,0}, {-1,0}, {0,1}, {0,-1},
|
||||||
|
{1,1}, {-1,1}, {-1,-1}, {1,-1}
|
||||||
|
};
|
||||||
|
for (int[] d : dirs) {
|
||||||
|
int nx = x + d[0], ny = y + d[1];
|
||||||
|
if (inBounds(nx, ny)) {
|
||||||
|
if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) {
|
||||||
|
moves.add(new int[]{nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -138,43 +248,41 @@ public class Board {
|
||||||
selectedX = x;
|
selectedX = x;
|
||||||
selectedY = y;
|
selectedY = y;
|
||||||
hasSelectedPiece = true;
|
hasSelectedPiece = true;
|
||||||
|
highlightedSquares = getValidMoves(clicked); // ✅ Highlight legal moves
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Check if clicked again on the same square to unselect
|
||||||
if (selectedX == x && selectedY == y) {
|
if (selectedX == x && selectedY == y) {
|
||||||
// Unselect
|
|
||||||
hasSelectedPiece = false;
|
hasSelectedPiece = false;
|
||||||
} else {
|
highlightedSquares.clear();
|
||||||
|
}
|
||||||
|
// If clicked on a highlighted square, move there
|
||||||
|
else if (isHighlighted(x, y)) {
|
||||||
Piece selectedPiece = board[selectedX][selectedY];
|
Piece selectedPiece = board[selectedX][selectedY];
|
||||||
if (selectedPiece != null) {
|
|
||||||
// Capture if needed
|
|
||||||
if (board[x][y] != null) {
|
|
||||||
if (board[x][y].isWhite() == selectedPiece.isWhite()) {
|
|
||||||
// Can't capture own piece
|
|
||||||
hasSelectedPiece = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move piece
|
// Move piece
|
||||||
board[x][y] = selectedPiece;
|
board[x][y] = selectedPiece;
|
||||||
board[selectedX][selectedY] = null;
|
board[selectedX][selectedY] = null;
|
||||||
selectedPiece.setX(x);
|
selectedPiece.setX(x);
|
||||||
selectedPiece.setY(y);
|
selectedPiece.setY(y);
|
||||||
|
|
||||||
// Switch turn
|
// Update turn
|
||||||
turnWhite = !turnWhite;
|
turnWhite = !turnWhite;
|
||||||
turnNumber++;
|
turnNumber++;
|
||||||
|
|
||||||
// Unselect
|
// Clear selection & highlights
|
||||||
hasSelectedPiece = false;
|
hasSelectedPiece = false;
|
||||||
}
|
highlightedSquares.clear();
|
||||||
|
}
|
||||||
|
// Invalid move: just unselect
|
||||||
|
else {
|
||||||
|
hasSelectedPiece = false;
|
||||||
|
highlightedSquares.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//public boolean isSelected(int x, int y) {
|
|
||||||
//return false
|
|
||||||
// }
|
|
||||||
|
|
||||||
/* saving-loading feature */
|
/* saving-loading feature */
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
|
|
@ -187,6 +295,11 @@ public class Board {
|
||||||
|
|
||||||
/* The following methods require more work */
|
/* The following methods require more work */
|
||||||
public boolean isHighlighted(int x, int y) {
|
public boolean isHighlighted(int x, int y) {
|
||||||
|
for (int[] pos : highlightedSquares) {
|
||||||
|
if (pos[0] == x && pos[1] == y) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue