Skip to content

Commit e1bc3cd

Browse files
committed
- Fix it with AI ... (somehow)
1 parent cf139d5 commit e1bc3cd

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

2026-02-05-Retro_Copilot/src/main/java/de/hilling/chess/ChessException.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

2026-02-05-Retro_Copilot/src/main/java/de/hilling/chess/ChessGame.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
public class ChessGame {
1111
private ChessPiece[][] board = new ChessPiece[8][8];
1212
private List<String> moves = new LinkedList<>();
13+
private MoveValidator validator = new MoveValidator(this.board);
1314

1415
public ChessGame() {
1516
for(int x=0; x<8; x++) {
@@ -68,6 +69,7 @@ public void move(String move) {
6869
Position to = Position.of(move.substring(2, 4));
6970
ChessPiece fromPiece = board[from.x][from.y];
7071
if(fromPiece != null) {
72+
validator.validateMove(from, to, fromPiece);
7173
board[to.x][to.y] = fromPiece;
7274
board[from.x][from.y] = null;
7375
moves.add(move);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.hilling.chess;
2+
3+
public class MoveValidator {
4+
private final ChessPiece[][] board;
5+
6+
public MoveValidator(ChessPiece[][] board) {
7+
this.board = board;
8+
}
9+
10+
public void validateMove(Position from, Position to, ChessPiece piece) {
11+
if (piece.type == PieceType.PAWN) {
12+
validatePawnMove(from, to, piece);
13+
}
14+
}
15+
16+
private void validatePawnMove(Position from, Position to, ChessPiece piece) {
17+
int direction = piece.color == Color.WHITE ? 1 : -1;
18+
int startRow = piece.color == Color.WHITE ? 1 : 6;
19+
20+
// Must move in same column (no capture implemented yet)
21+
if (from.x != to.x) {
22+
throw new IllegalArgumentException("Pawn must move straight forward");
23+
}
24+
25+
// Check if target square is empty
26+
if (board[to.x][to.y] != null) {
27+
throw new IllegalArgumentException("Target square is occupied");
28+
}
29+
30+
int yDiff = to.y - from.y;
31+
32+
// Move one square forward
33+
if (yDiff == direction) {
34+
return;
35+
}
36+
37+
// Move two squares forward from starting position
38+
if (from.y == startRow && yDiff == 2 * direction) {
39+
// Check if path is clear
40+
if (board[from.x][from.y + direction] == null) {
41+
return;
42+
}
43+
throw new IllegalArgumentException("Path is blocked");
44+
}
45+
46+
throw new IllegalArgumentException("Invalid pawn move");
47+
}
48+
}

2026-02-05-Retro_Copilot/src/test/java/de/hilling/chess/ChessGameTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void setUp() {
4848
chessGame = new ChessGame();
4949
}
5050

51-
// TODO: test more pieces, refactoring
5251
@Test
5352
void testSetup() {
5453
ChessPiece whitePawn = chessGame.get(Position.of(1, 1));
@@ -84,6 +83,16 @@ void illegalMoveCharacters() {
8483
}
8584
}
8685

86+
@Test
87+
void illegalMoveMoveNotPossibleForPawn() {
88+
try {
89+
chessGame.move("a2a5");
90+
fail("Illegal move exception");
91+
} catch (IllegalArgumentException iae) {
92+
assertEquals("Invalid pawn move", iae.getMessage());
93+
}
94+
}
95+
8796
@Test
8897
void movePawn() {
8998
chessGame.move("e2e4");

0 commit comments

Comments
 (0)