move rules for pawns and knights

This commit is contained in:
manon 2025-04-23 14:03:04 +02:00
parent 2d0f562795
commit 39e1c4a8fe
2 changed files with 94 additions and 4 deletions

View File

@ -10,6 +10,7 @@ public class Board {
ArrayList<Piece> pieces = new ArrayList<>();
private int selectedX = -1;
private int selectedY = -1;
ArrayList<int[]> highlightedSquares = new ArrayList<>();
public Board(int colNum, int lineNum) {
this.colNum = colNum;
@ -134,6 +135,8 @@ public class Board {
System.out.println("Selecting piece at: " + x + ", " + y);
selectedX = x;
selectedY = y;
highlightedSquares = getValidMoves(clickedPiece);
System.out.println("Valid moves highlighted for selected piece.");
} else {
System.out.println("No valid piece to select at: " + x + ", " + y);
}
@ -144,6 +147,7 @@ public class Board {
System.out.println("Unselecting piece at: " + x + ", " + y);
selectedX = -1;
selectedY = -1;
highlightedSquares.clear();
return;
}
@ -161,6 +165,7 @@ public class Board {
selectedX = -1;
selectedY = -1;
highlightedSquares.clear();
}
public boolean isSelected(int x, int y) {
@ -182,8 +187,12 @@ public class Board {
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
for (int[] pos : highlightedSquares) {
if (pos[0] == x && pos[1] == y) {
return true;
}
}
return false;
}
public void undoLastMove() {
@ -200,5 +209,18 @@ public class Board {
//TODO
}
public ArrayList<int[]> getValidMoves(Piece piece) {
Move moveHelper = new Move(piece, this);
switch (piece.getType()) {
case Pawn:
return moveHelper.getPawnMoves();
case Knight:
return moveHelper.getKnightMoves();
default:
return new ArrayList<>();
}
}
}

View File

@ -1,5 +1,73 @@
package backend;
import java.util.ArrayList;
public class Move {
}
private Piece piece;
private Board board;
public Move(Piece piece, Board board) {
this.piece = piece;
this.board = board;
}
public ArrayList<int[]> getPawnMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int dir = isWhite ? -1 : 1;
// Move one square forward (only if empty)
if (board.getPieceAt(x, y + dir) == null) {
moves.add(new int[]{x, y + dir});
// Move two squares from starting position (also only if empty)
boolean isAtStart = (isWhite && y == 6) || (!isWhite && y == 1);
if (isAtStart && board.getPieceAt(x, y + 2 * dir) == null) {
moves.add(new int[]{x, y + 2 * dir});
}
}
// Capture diagonally (because apparently pawns capture that way, I didn't know...)
for (int dx : new int[]{-1, 1}) {
int targetX = x + dx;
int targetY = y + dir;
if (targetX >= 0 && targetX < board.getWidth() && targetY >= 0 && targetY < board.getHeight()) {
Piece target = board.getPieceAt(targetX, targetY);
if (target != null && target.isWhite() != isWhite) {
moves.add(new int[]{targetX, targetY});
}
}
}
return moves;
}
public ArrayList<int[]> getKnightMoves() {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
boolean isWhite = piece.isWhite();
int[][] offsets = {
{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}
};
for (int[] offset : offsets) {
int newX = x + offset[0];
int newY = y + offset[1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
moves.add(new int[]{newX, newY});
}
}
}
return moves;
}
}