Skip to content

Commit 202230d

Browse files
authored
Merge pull request #29 from mgierschdev/copilot/extract-chess-engine-library
Add FEN import/export, undo/redo, AI opponent, and chess engine extraction guide
2 parents 8e4a803 + 920c825 commit 202230d

File tree

16 files changed

+2998
-4
lines changed

16 files changed

+2998
-4
lines changed

CHESS_ENGINE_API.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# Chess Engine Core API Reference
2+
3+
## Quick Start
4+
5+
```java
6+
import com.backend.domain.ChessGame;
7+
import com.backend.models.*;
8+
9+
// Create new game
10+
ChessGame game = new ChessGame();
11+
12+
// Make a move
13+
Position from = new Position(2, 5); // e2
14+
Position to = new Position(4, 5); // e4
15+
ChessPiece result = game.MoveController(from, to);
16+
17+
// Check if move was valid
18+
if (result.type() != ChessPieceType.Invalid) {
19+
System.out.println("Move successful!");
20+
}
21+
```
22+
23+
## ChessGame API
24+
25+
### Core Methods
26+
27+
#### `MoveController(Position from, Position to)`
28+
Make a move on the board.
29+
- **Parameters**: Source and target positions (1-indexed)
30+
- **Returns**: `ChessPiece` - captured piece, or Invalid if move is illegal
31+
- **Side effects**: Updates game state, switches turn
32+
33+
#### `MoveController(Position from, Position to, ChessPieceType promotionType)`
34+
Make a move with pawn promotion.
35+
- **Parameters**: Source, target, and promotion piece type
36+
- **Returns**: `ChessPiece` - captured piece, or Invalid if move is illegal
37+
38+
#### `getValidMovesController(Position position)`
39+
Get all valid moves for a piece.
40+
- **Parameters**: Position of piece (1-indexed)
41+
- **Returns**: Array of valid target positions
42+
43+
### Game State
44+
45+
#### `getTurn()`
46+
Get current player's turn.
47+
- **Returns**: `Color.White` or `Color.Black`
48+
49+
#### `getGameState()`
50+
Get current game state.
51+
- **Returns**: `GameState` enum (Free, Check, Checkmate, DrawByStalemate, etc.)
52+
53+
#### `getChessboard()`
54+
Get board representation for display.
55+
- **Returns**: Array of `ChessPieceResponse` for rendering
56+
57+
#### `getMoveHistory()`
58+
Get list of all moves made.
59+
- **Returns**: `List<Move>` - move history
60+
61+
#### `getCaptured(Color color)`
62+
Get captured pieces for a player.
63+
- **Returns**: `Set<ChessPiece>` - pieces captured by the color
64+
65+
### FEN Support
66+
67+
#### `importFromFEN(String fen)`
68+
Import position from FEN notation.
69+
- **Parameters**: FEN string
70+
- **Throws**: `IllegalArgumentException` for invalid FEN
71+
72+
#### `exportToFEN()`
73+
Export current position to FEN.
74+
- **Returns**: FEN string
75+
76+
#### `exportToPGN()`
77+
Export game in PGN format.
78+
- **Returns**: PGN formatted string
79+
80+
### Undo/Redo
81+
82+
#### `undo()`
83+
Undo last move.
84+
- **Returns**: `true` if successful, `false` if nothing to undo
85+
86+
#### `redo()`
87+
Redo previously undone move.
88+
- **Returns**: `true` if successful, `false` if nothing to redo
89+
90+
#### `canUndo()`
91+
Check if undo is available.
92+
- **Returns**: `boolean`
93+
94+
#### `canRedo()`
95+
Check if redo is available.
96+
- **Returns**: `boolean`
97+
98+
## AI API
99+
100+
### ChessAI.findBestMove(ChessGame game)
101+
Find best move using minimax algorithm.
102+
```java
103+
import com.backend.ai.ChessAI;
104+
105+
ChessAI.AIMove move = ChessAI.findBestMove(game);
106+
if (move != null) {
107+
game.MoveController(move.from, move.to);
108+
}
109+
```
110+
111+
### ChessAI.findBestMove(ChessGame game, int depth)
112+
Find best move with custom search depth.
113+
- **Parameters**: game state, search depth (default: 3)
114+
- **Returns**: `AIMove` with from/to positions and evaluation score
115+
116+
## FEN Parser API
117+
118+
### FENParser.parseFEN(String fen)
119+
Parse FEN string to board state.
120+
```java
121+
import com.backend.util.FENParser;
122+
123+
FENParser.FENParseResult result = FENParser.parseFEN(fen);
124+
// Access result.board, result.activeColor, result.castlingRights, etc.
125+
```
126+
127+
### FENParser.generateFEN(...)
128+
Generate FEN from board state.
129+
```java
130+
String fen = FENParser.generateFEN(
131+
board, activeColor,
132+
whiteKingMoved, blackKingMoved,
133+
whiteKingsideRookMoved, whiteQueensideRookMoved,
134+
blackKingsideRookMoved, blackQueensideRookMoved,
135+
enPassantTarget, halfMoveClock, fullMoveNumber
136+
);
137+
```
138+
139+
## Model Classes
140+
141+
### ChessPiece
142+
Immutable record representing a chess piece.
143+
- `type()` - ChessPieceType (Pawn, Knight, Bishop, Rock, Queen, King)
144+
- `color()` - Color (White, Black, None)
145+
146+
### Position
147+
Represents board position.
148+
- `row` - Row index (1-8 for user-facing APIs, 0-7 internally)
149+
- `col` - Column index (1-8 for user-facing APIs, 0-7 internally)
150+
151+
### Move
152+
Represents a chess move with metadata.
153+
- `getFrom()` - Source position
154+
- `getTo()` - Target position
155+
- `getPiece()` - Piece that moved
156+
- `getCapturedPiece()` - Piece captured (if any)
157+
- `isCapture()` - Whether this was a capture
158+
- `isPawnMove()` - Whether a pawn moved
159+
- `isEnPassant()` - Whether this was en passant
160+
- `isCastling()` - Whether this was castling
161+
162+
### GameState Enum
163+
- `Free` - Normal play
164+
- `Check` - King in check
165+
- `Checkmate` - Game over, checkmate
166+
- `DrawByStalemate` - Game over, stalemate
167+
- `DrawByFiftyMove` - Draw by 50-move rule
168+
- `DrawByRepetition` - Draw by threefold repetition
169+
170+
## Board Evaluator API
171+
172+
### BoardEvaluator.evaluate(ChessPiece[][] board, Color color)
173+
Evaluate board position for a color.
174+
```java
175+
import com.backend.ai.BoardEvaluator;
176+
177+
int score = BoardEvaluator.evaluate(board, Color.White);
178+
// Positive = White winning, Negative = Black winning
179+
```
180+
181+
## Examples
182+
183+
### Basic Game Flow
184+
```java
185+
ChessGame game = new ChessGame();
186+
187+
// White moves e2-e4
188+
game.MoveController(new Position(2, 5), new Position(4, 5));
189+
190+
// Black moves e7-e5
191+
game.MoveController(new Position(7, 5), new Position(5, 5));
192+
193+
// Check game state
194+
if (game.getGameState() == GameState.Check) {
195+
System.out.println("Check!");
196+
}
197+
```
198+
199+
### FEN Import/Export
200+
```java
201+
// Import from FEN
202+
String fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
203+
game.importFromFEN(fen);
204+
205+
// Play some moves...
206+
207+
// Export current position
208+
String currentFEN = game.exportToFEN();
209+
System.out.println(currentFEN);
210+
```
211+
212+
### AI Opponent
213+
```java
214+
ChessGame game = new ChessGame();
215+
216+
while (game.getGameState() == GameState.Free ||
217+
game.getGameState() == GameState.Check) {
218+
219+
if (game.getTurn() == Color.White) {
220+
// Human move (get from UI)
221+
game.MoveController(userFrom, userTo);
222+
} else {
223+
// AI move
224+
ChessAI.AIMove aiMove = ChessAI.findBestMove(game);
225+
if (aiMove != null) {
226+
game.MoveController(aiMove.from, aiMove.to);
227+
System.out.println("AI moved from " + aiMove.from +
228+
" to " + aiMove.to +
229+
" (score: " + aiMove.score + ")");
230+
}
231+
}
232+
}
233+
234+
System.out.println("Game over: " + game.getGameState());
235+
```
236+
237+
### Undo/Redo
238+
```java
239+
// Make moves
240+
game.MoveController(new Position(2, 5), new Position(4, 5));
241+
game.MoveController(new Position(7, 5), new Position(5, 5));
242+
243+
// Undo last move
244+
if (game.canUndo()) {
245+
game.undo();
246+
}
247+
248+
// Redo
249+
if (game.canRedo()) {
250+
game.redo();
251+
}
252+
```
253+
254+
## Thread Safety
255+
256+
The chess engine classes are **not thread-safe**. If using in a multi-threaded environment:
257+
- Use one `ChessGame` instance per game/thread
258+
- Or synchronize access externally
259+
- `BoardEvaluator` methods are stateless and thread-safe
260+
261+
## Performance
262+
263+
- Move validation: O(n) where n = number of pieces
264+
- AI move generation (depth 3): ~0.5-2 seconds typical
265+
- FEN parsing: O(1) for fixed board size
266+
- Undo/redo: O(1) with memory overhead for snapshots

0 commit comments

Comments
 (0)