kish/
lib.rs

1//! # Kish - Turkish Checkers (Dama) Engine
2//!
3//! A high-performance Rust implementation of Turkish Draughts (Dama), featuring
4//! bitboard-based state representation and optimized move generation.
5//!
6//! ## Overview
7//!
8//! Turkish Draughts, known as **Dama** (or **Türk Daması**) in Turkey, is a variant
9//! of checkers distinguished by its **orthogonal movement** (horizontal and vertical)
10//! rather than diagonal movement. This library implements all official rules including:
11//!
12//! - Orthogonal movement for men (forward, left, right) and kings (all four directions)
13//! - Flying captures for kings (capture from any distance, land anywhere beyond)
14//! - Mandatory capture rule (must capture if able)
15//! - Maximum capture rule (must choose the sequence capturing the most pieces)
16//! - 180-degree turn prohibition during multi-capture sequences
17//! - Immediate piece removal during captures
18//! - Promotion to king on the back row
19//! - Mid-capture promotion rules (continues as pawn, promotes at turn end)
20//!
21//! ## Quick Start
22//!
23//! ```rust
24//! use kish::{Board, Team, GameStatus};
25//!
26//! // Create a new game with the standard starting position
27//! let board = Board::new_default();
28//!
29//! // White moves first
30//! assert_eq!(board.turn, Team::White);
31//!
32//! // Get all legal actions
33//! let actions = board.actions();
34//! println!("White has {} legal moves", actions.len());
35//!
36//! // Apply an action to get the new board state
37//! if let Some(action) = actions.first() {
38//!     let mut new_board = board.apply(action);
39//!     new_board.swap_turn_(); // Change to opponent's turn
40//!
41//!     // Check game status
42//!     match new_board.status() {
43//!         GameStatus::InProgress => println!("Game continues"),
44//!         GameStatus::Draw => println!("Game is a draw"),
45//!         GameStatus::Won(team) => println!("{} wins!", team),
46//!     }
47//! }
48//! ```
49//!
50//! ## Board Representation
51//!
52//! The board uses a bitboard representation where each `u64` represents 64 squares:
53//!
54//! ```text
55//!     a   b   c   d   e   f   g   h
56//!   +---+---+---+---+---+---+---+---+
57//! 8 |56 |57 |58 |59 |60 |61 |62 |63 |  ← Black's back row (White promotes here)
58//!   +---+---+---+---+---+---+---+---+
59//! 7 |48 |49 |50 |51 |52 |53 |54 |55 |  ← Black pieces start (rows 6-7)
60//!   +---+---+---+---+---+---+---+---+
61//! 6 |40 |41 |42 |43 |44 |45 |46 |47 |
62//!   +---+---+---+---+---+---+---+---+
63//! 5 |32 |33 |34 |35 |36 |37 |38 |39 |
64//!   +---+---+---+---+---+---+---+---+
65//! 4 |24 |25 |26 |27 |28 |29 |30 |31 |
66//!   +---+---+---+---+---+---+---+---+
67//! 3 |16 |17 |18 |19 |20 |21 |22 |23 |  ← White pieces start (rows 2-3)
68//!   +---+---+---+---+---+---+---+---+
69//! 2 | 8 | 9 |10 |11 |12 |13 |14 |15 |
70//!   +---+---+---+---+---+---+---+---+
71//! 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |  ← White's back row (Black promotes here)
72//!   +---+---+---+---+---+---+---+---+
73//!     a   b   c   d   e   f   g   h
74//! ```
75//!
76//! ## Performance
77//!
78//! This engine is optimized for high-speed move generation and game tree search:
79//!
80//! - **Bitboard operations**: All piece positions stored as `u64` bitmasks
81//! - **Compile-time generics**: Team-specific code paths via const generics
82//! - **XOR-based deltas**: Actions store state changes, not full board copies
83//! - **Lazy evaluation**: Capture detection uses early-exit optimizations
84//!
85//! ## Key Types
86//!
87//! - [`Board`]: The main game state containing piece positions and current turn
88//! - [`Game`]: Full game with history tracking for draw detection (threefold repetition, 50-move rule)
89//! - [`Action`]: A move represented as a bitboard delta (fast for simulations)
90//! - [`ActionPath`]: A move with full path information (for UI/notation)
91//! - [`Square`]: A single square on the board (0-63)
92//! - [`Team`]: White or Black
93//! - [`State`]: Raw bitboard state without turn information
94//! - [`GameStatus`]: Current game status (InProgress, Draw, or Won)
95//!
96//! ## Move Notation
97//!
98//! The library supports standard algebraic notation for Turkish Draughts:
99//!
100//! | Move Type | Format | Example | Description |
101//! |-----------|--------|---------|-------------|
102//! | Non-capturing | `from-to` | `d3-d4` | Man moves forward |
103//! | Single capture | `fromxto` | `d4xd6` | Piece jumps over enemy |
104//! | Multi-capture | `fromxmidxto` | `d4xd6xf6` | Chain capture |
105//! | Promotion | `from-to=K` | `d7-d8=K` | Man becomes king |
106//!
107//! ```rust
108//! use kish::{Board, Team, Square};
109//!
110//! let board = Board::from_squares(
111//!     Team::White,
112//!     &[Square::D4],
113//!     &[Square::D5],
114//!     &[],
115//! );
116//!
117//! let actions = board.actions();
118//! let detailed = actions[0].to_detailed(board.turn, &board.state);
119//! assert_eq!(detailed.to_notation(), "d4xd6"); // Capture notation
120//! ```
121//!
122//! ## Rules Reference
123//!
124//! For complete rules documentation, see the `docs/rules.md` file in the repository.
125//!
126//! ### Key Rules Summary
127//!
128//! 1. **Movement**: Men move 1 square forward/left/right. Kings move any distance orthogonally.
129//! 2. **Captures are mandatory**: If you can capture, you must.
130//! 3. **Maximum capture**: Must choose the path capturing the most pieces.
131//! 4. **No 180° turns**: During multi-capture, can't reverse direction.
132//! 5. **One piece each = Draw**: When both players have exactly one piece.
133//! 6. **Blocking = Loss**: If you can't move, you lose.
134
135mod action;
136mod actiongen;
137mod board;
138mod game;
139mod game_status;
140mod perft;
141mod square;
142mod state;
143mod team;
144
145pub use action::{Action, ActionPath};
146pub use board::Board;
147pub use game::Game;
148pub use game_status::GameStatus;
149pub use square::Square;
150pub use state::State;
151pub use team::Team;