|
| 1 | +--- |
| 2 | + |
| 3 | +# **Game Module Documentation** |
| 4 | + |
| 5 | +This module manages the core chess game logic, including move validation, game actions, and status tracking. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## **Overview** |
| 10 | +The `Game` module provides an abstraction for managing chess games: |
| 11 | +- Supports moves using SAN (Standard Algebraic Notation). |
| 12 | +- Offers/accepts draws. |
| 13 | +- Tracks game status (e.g., checkmate, stalemate, resignation). |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## **Key Enums** |
| 18 | + |
| 19 | +### **GameAction** |
| 20 | +Defines player actions: |
| 21 | +- `AcceptDraw`: Accept a draw. |
| 22 | +- `MakeMove(String)`: Make a move using SAN. |
| 23 | +- `OfferDraw(String)`: Make a move and offer a draw. |
| 24 | +- `Resign`: Resign the game. |
| 25 | + |
| 26 | +#### Example: |
| 27 | +```rust |
| 28 | +let action = GameAction::from("e4"); // Makes the move e4 |
| 29 | +``` |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +### **GameError** |
| 34 | +Common errors: |
| 35 | +- `AmbiguousMove`: SAN move is ambiguous. |
| 36 | +- `GameAlreadyOver`: Action attempted after game ended. |
| 37 | +- `InvalidMove`: Move invalid for current board state. |
| 38 | +- `InvalidPosition`: FEN string is invalid. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +### **GameOver** |
| 43 | +Possible game-ending states: |
| 44 | +- `WhiteCheckmates`, `BlackCheckmates` |
| 45 | +- `WhiteResigns`, `BlackResigns` |
| 46 | +- `Stalemate`, `DrawAccepted` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## **Game Struct** |
| 51 | +Manages the game state: |
| 52 | +- `board`: Current chessboard state. |
| 53 | +- `draw_offered`: Tracks if a draw is offered. |
| 54 | +- `status`: Game status (active or ended). |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## **Core Methods** |
| 59 | + |
| 60 | +1. **`make_move(&mut self, action: &GameAction)`** |
| 61 | + Executes a player action. |
| 62 | + **Returns:** `Result<&Option<GameOver>, GameError>` |
| 63 | + |
| 64 | + Example: |
| 65 | + ```rust |
| 66 | + game.make_move(&GameAction::from("e4"))?; |
| 67 | + ``` |
| 68 | + |
| 69 | +2. **`to_fen(&self, halfmove_clock: u8, fullmove_number: u8)`** |
| 70 | + Converts the current game state to a FEN string. |
| 71 | + |
| 72 | +3. **`get_turn_color(&self)`** |
| 73 | + Returns the color of the player whose turn it is. |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## **Tests** |
| 78 | +- **`test_game_moves`**: Verifies a sequence of valid moves. |
| 79 | +- **`test_fools_mate`**: Simulates Fool's Mate and checks the result. |
| 80 | +- **`test_promotion`**: Ensures pawn promotion works correctly. |
| 81 | + |
| 82 | +Example: |
| 83 | +```rust |
| 84 | +#[test] |
| 85 | +fn test_fools_mate() { |
| 86 | + let mut game = Game::default(); |
| 87 | + game.make_move(&GameAction::from("f3")).unwrap(); |
| 88 | + game.make_move(&GameAction::from("e5")).unwrap(); |
| 89 | + game.make_move(&GameAction::from("g4")).unwrap(); |
| 90 | + game.make_move(&GameAction::from("Qh4")).unwrap(); |
| 91 | + assert_eq!(game.status, Some(GameOver::BlackCheckmates)); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## **Purpose** |
| 98 | +- Simplifies onboarding for new developers. |
| 99 | +- Highlights key functionalities and error handling. |
| 100 | +- Reduces time spent reading code. |
| 101 | + |
| 102 | +--- |
0 commit comments