kish 1.1.7

A high-performance Turkish Draughts (Dama) engine with bitboard representation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Board representation and game logic for Turkish Draughts.
//!
//! This module defines the [`Board`] struct which combines the board state
//! with the current turn, providing the main interface for game logic.
//!
//! # Overview
//!
//! The `Board` struct is the primary type for interacting with the game:
//! - Query the current position and legal moves
//! - Apply moves to get new positions
//! - Check the game status (win/draw/in-progress)
//!
//! # Example
//!
//! ```rust
//! use kish::{Board, Team, GameStatus};
//!
//! // Create a new game
//! let mut board = Board::new_default();
//! assert_eq!(board.turn, Team::White);
//!
//! // Get legal moves
//! let actions = board.actions();
//! println!("Available moves: {}", actions.len());
//!
//! // Apply a move
//! if let Some(action) = actions.first() {
//!     board.apply_(action);
//!     board.swap_turn_();
//! }
//!
//! // Check game status
//! match board.status() {
//!     GameStatus::InProgress => println!("Game continues"),
//!     GameStatus::Draw => println!("Draw"),
//!     GameStatus::Won(team) => println!("{} wins!", team),
//! }
//! ```
//!
//! # Board Rotation
//!
//! The board can be rotated 180 degrees, which is useful for:
//! - Normalizing positions for transposition tables
//! - Viewing the board from the opponent's perspective
//! - Checking if the opponent is blocked
//!
//! ```rust
//! use kish::{Board, Team, Square};
//!
//! let board = Board::from_squares(
//!     Team::White,
//!     &[Square::A2],
//!     &[Square::H7],
//!     &[],
//! );
//!
//! let rotated = board.rotate();
//! // After rotation, the pieces swap positions (180° rotation)
//! ```

use std::fmt;

use super::{Action, GameStatus, Square, State, Team};
use crate::state::{
    MASK_COL_A, MASK_COL_B, MASK_COL_G, MASK_COL_H, MASK_ROW_1, MASK_ROW_2, MASK_ROW_7, MASK_ROW_8,
};

/// The main game board combining piece positions and current turn.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Board {
    /// The current team's turn.
    pub turn: Team,
    /// The state of the board.
    pub state: State,
}

impl Board {
    /// Creates a new board.
    #[must_use]
    pub const fn new(turn: Team, state: State) -> Self {
        Self { turn, state }
    }

    /// Creates a new board with default configuration.
    #[must_use]
    pub const fn new_default() -> Self {
        Self {
            turn: Team::White,
            state: State::default(),
        }
    }

    /// Creates a new board from the given squares.
    #[must_use]
    pub const fn from_squares(
        turn: Team,
        white_squares: &[Square],
        black_squares: &[Square],
        king_squares: &[Square],
    ) -> Self {
        let mut whites = 0u64;
        let mut i = 0;
        while i < white_squares.len() {
            let square = white_squares[i];
            whites |= square.to_mask();
            i += 1;
        }

        let mut blacks = 0u64;
        let mut i = 0;
        while i < black_squares.len() {
            let square = black_squares[i];
            blacks |= square.to_mask();
            i += 1;
        }

        let mut kings = 0u64;
        let mut i = 0;
        while i < king_squares.len() {
            let square = king_squares[i];
            kings |= square.to_mask();
            i += 1;
        }

        Self {
            turn,
            state: State {
                pieces: [whites, blacks],
                kings,
            },
        }
    }

    /// Returns the friendly pieces.
    #[inline(always)]
    #[must_use]
    pub const fn friendly_pieces(&self) -> u64 {
        self.state.pieces[self.turn as usize]
    }

    /// Returns the hostile pieces.
    #[inline(always)]
    #[must_use]
    pub const fn hostile_pieces(&self) -> u64 {
        self.state.pieces[self.turn.opponent() as usize]
    }

    /// Swaps the player's turn in-place.
    #[inline(always)]
    pub const fn swap_turn_(&mut self) {
        self.turn = self.turn.opponent();
    }

    /// Swaps the player's turn and returns the new board.
    #[inline]
    #[must_use]
    pub fn swap_turn(&self) -> Self {
        let mut new_board = *self;
        new_board.swap_turn_();
        new_board
    }

    /// Applies the given action to the board in-place.
    #[inline(always)]
    pub fn apply_(&mut self, action: &Action) {
        self.state.apply_(&action.delta);

        #[cfg(debug_assertions)]
        self.state.validate();
    }

    /// Applies the given action to the board and returns the new board.
    #[must_use]
    pub fn apply(&self, action: &Action) -> Self {
        let mut new_board = *self;
        new_board.apply_(action);
        new_board
    }

    /// Rotates the board by 180 degrees in-place.
    pub fn rotate_(&mut self) {
        self.state.rotate_();

        #[cfg(debug_assertions)]
        self.state.validate();
    }

    /// Rotates the board by 180 degrees and returns the new board.
    #[must_use]
    pub fn rotate(&self) -> Self {
        let mut new_board = *self;
        new_board.rotate_();
        new_board
    }

    /// Computes the status of the board.
    #[must_use]
    pub fn status(&self) -> GameStatus {
        let friendly_pieces = self.friendly_pieces();
        let hostile_pieces = self.hostile_pieces();

        // If friendlies have no pieces, then hostiles have won
        if friendly_pieces == 0 {
            return GameStatus::Won(self.turn.opponent());
        }

        // If hostiles have no pieces, then friendlies have won
        if hostile_pieces == 0 {
            return GameStatus::Won(self.turn);
        }

        // If both teams have a single piece, it is a draw
        if friendly_pieces.is_power_of_two() && hostile_pieces.is_power_of_two() {
            return GameStatus::Draw;
        }

        // If friendlies have no actions, then hostiles have won
        if self.is_blocked() {
            return GameStatus::Won(self.turn.opponent());
        }

        // If hostiles have no actions, then friendlies have won
        if self.rotate().is_blocked() {
            return GameStatus::Won(self.turn);
        }

        // Otherwise, the game is in progress
        GameStatus::InProgress
    }

    /// Checks if the current player has no valid moves (is blocked).
    ///
    /// # Preconditions
    /// Caller must ensure both teams have pieces (checked by `status()`).
    const fn is_blocked(&self) -> bool {
        let friendly_pieces = self.friendly_pieces();
        let hostile_pieces = self.hostile_pieces();
        let friendly_kings = friendly_pieces & self.state.kings;
        let empty = self.state.empty();

        // Check if any piece can make a simple move (1 square)
        if self.can_any_piece_move(friendly_pieces, friendly_kings, empty) {
            return false;
        }

        // Check if any piece can make a simple capture (pawn: 2 squares, jumping 1 hostile)
        if self.can_any_piece_capture_simple(friendly_pieces, hostile_pieces, empty) {
            return false;
        }

        // Note: King flying captures are NOT checked here because they're redundant:
        // - Adjacent hostile with empty landing → found by can_any_piece_capture_simple
        // - Non-adjacent hostile (via ray) → requires empty adjacent squares → found by can_any_piece_move

        true
    }

    /// Checks if any piece can make a simple 1-square move.
    const fn can_any_piece_move(
        &self,
        friendly_pieces: u64,
        friendly_kings: u64,
        empty: u64,
    ) -> bool {
        // Left moves (all pieces)
        if ((friendly_pieces & !MASK_COL_A) >> 1) & empty != 0 {
            return true;
        }

        // Right moves (all pieces)
        if ((friendly_pieces & !MASK_COL_H) << 1) & empty != 0 {
            return true;
        }

        // Forward moves (team-dependent for pawns)
        let vertical_pawn_moves = match self.turn {
            Team::White => ((friendly_pieces & !MASK_ROW_8) << 8) & empty,
            Team::Black => ((friendly_pieces & !MASK_ROW_1) >> 8) & empty,
        };
        if vertical_pawn_moves != 0 {
            return true;
        }

        // Backward moves (kings only - opposite to team's forward direction)
        let backward_king_moves = match self.turn {
            Team::White => ((friendly_kings & !MASK_ROW_1) >> 8) & empty,
            Team::Black => ((friendly_kings & !MASK_ROW_8) << 8) & empty,
        };
        backward_king_moves != 0
    }

    /// Checks if any piece can make a simple capture (pawn-style: jump over 1 adjacent hostile).
    const fn can_any_piece_capture_simple(
        &self,
        friendly_pieces: u64,
        hostile_pieces: u64,
        empty: u64,
    ) -> bool {
        // Left capture: piece at col C+ can capture hostile 1 left, landing 2 left
        let left_captures =
            (((friendly_pieces & !(MASK_COL_A | MASK_COL_B)) >> 1) & hostile_pieces) >> 1 & empty;
        if left_captures != 0 {
            return true;
        }

        // Right capture: piece at col A-F can capture hostile 1 right, landing 2 right
        let right_captures =
            (((friendly_pieces & !(MASK_COL_G | MASK_COL_H)) << 1) & hostile_pieces) << 1 & empty;
        if right_captures != 0 {
            return true;
        }

        // Vertical capture (team-dependent direction)
        let vertical_captures = match self.turn {
            Team::White => {
                (((friendly_pieces & !(MASK_ROW_7 | MASK_ROW_8)) << 8) & hostile_pieces) << 8
                    & empty
            }
            Team::Black => {
                (((friendly_pieces & !(MASK_ROW_1 | MASK_ROW_2)) >> 8) & hostile_pieces) >> 8
                    & empty
            }
        };
        vertical_captures != 0
    }
}

impl fmt::Display for Board {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Status: {}\nTurn: {}\n{}",
            self.status(),
            self.turn,
            self.state
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::{MASK_ROW_2, MASK_ROW_3, MASK_ROW_6};

    #[test]
    fn new() {
        let state = State::new(
            [Square::A4.to_mask(), Square::H5.to_mask()],
            Square::H5.to_mask(),
        );
        let board = Board::new(Team::Black, state);
        assert_eq!(board.turn, Team::Black);
        assert_eq!(board.state, state);
    }

    #[test]
    fn new_default() {
        let board = Board::new_default();
        assert_eq!(board.turn, Team::White);
        assert_eq!(board.state, State::default());
    }

    #[test]
    fn from_squares() {
        let friendly_squares = [Square::A1, Square::B2];
        let hostile_squares = [Square::C3, Square::D4];
        let king_squares = [Square::A1, Square::D4];
        let board = Board::from_squares(
            Team::Black,
            &friendly_squares,
            &hostile_squares,
            &king_squares,
        );

        assert_eq!(board.turn, Team::Black);
        assert_eq!(
            board.friendly_pieces(),
            Square::C3.to_mask() | Square::D4.to_mask()
        );
        assert_eq!(
            board.hostile_pieces(),
            Square::A1.to_mask() | Square::B2.to_mask()
        );
        assert_eq!(
            board.state.kings,
            Square::A1.to_mask() | Square::D4.to_mask()
        );
    }

    #[test]
    fn apply() {
        let board = Board::from_squares(
            Team::White,
            &[Square::A2, Square::B2],
            &[Square::A3, Square::D4],
            &[],
        );
        let action = Action::new(
            Team::White,
            Square::A2,
            Square::A4,
            &[Square::A3],
            board.state.kings,
        );
        let new_board = board.apply(&action);
        let expected =
            Board::from_squares(Team::White, &[Square::A4, Square::B2], &[Square::D4], &[]);
        assert_eq!(new_board, expected);

        // Test in-place
        let mut new_board_ = board;
        new_board_.apply_(&action);
        assert_eq!(new_board_, new_board);
    }

    #[test]
    fn rotate() {
        let board = Board::new(
            Team::White,
            State::new(
                [
                    MASK_ROW_2 | Square::B3.to_mask(),
                    MASK_ROW_6 | Square::F5.to_mask(),
                ],
                Square::B3.to_mask() | Square::F5.to_mask(),
            ),
        );

        let expected = Board::new(
            Team::White,
            State::new(
                [
                    MASK_ROW_3 | Square::C4.to_mask(),
                    MASK_ROW_7 | Square::G6.to_mask(),
                ],
                Square::G6.to_mask() | Square::C4.to_mask(),
            ),
        );
        let new_board = board.rotate();
        assert_eq!(new_board, expected);

        // Test in-place
        let mut new_board_ = board;
        new_board_.rotate_();
        assert_eq!(new_board_, new_board);
    }

    #[test]
    fn status_no_friendly_pieces() {
        let board = Board::from_squares(
            Team::White,
            &[],
            &[Square::A1, Square::B2, Square::C3, Square::D4],
            &[Square::A1],
        );
        assert_eq!(board.status(), GameStatus::Won(Team::Black));
    }

    #[test]
    fn status_no_hostile_pieces() {
        let board = Board::from_squares(
            Team::White,
            &[Square::A1, Square::B2, Square::C3, Square::D4],
            &[],
            &[Square::A1],
        );
        assert_eq!(board.status(), GameStatus::Won(Team::White));
    }

    #[test]
    fn status_draw() {
        let board = Board::from_squares(Team::White, &[Square::A1], &[Square::B2], &[Square::A1]);
        assert_eq!(board.status(), GameStatus::Draw);
    }

    #[test]
    fn status_in_progress() {
        let board = Board::from_squares(
            Team::White,
            &[Square::A2, Square::B2, Square::C3, Square::D4],
            &[Square::E5, Square::F6, Square::G7, Square::H8],
            &[Square::C3],
        );
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn status_friendly_blocked() {
        let board = Board::from_squares(
            Team::White,
            &[Square::A2, Square::A3],
            &[
                Square::A4,
                Square::A5,
                Square::B2,
                Square::B3,
                Square::C2,
                Square::C3,
            ],
            &[],
        );
        assert_eq!(board.status(), GameStatus::Won(Team::Black));
    }

    #[test]
    fn status_hostile_blocked() {
        let board = Board::from_squares(
            Team::White,
            &[
                Square::A2,
                Square::A3,
                Square::B4,
                Square::B5,
                Square::C4,
                Square::C5,
            ],
            &[Square::A4, Square::A5],
            &[],
        );
        assert_eq!(board.status(), GameStatus::Won(Team::White));
    }

    #[test]
    fn status_king_can_escape_via_flying_capture() {
        // King at A1 is surrounded but can make a flying capture
        // King can fly over empty squares and capture hostile at D1
        let board = Board::from_squares(
            Team::White,
            &[Square::A1],
            &[Square::A2, Square::B1, Square::D1],
            &[Square::A1], // A1 is a king
        );
        // King can capture D1 by flying right, so game is in progress
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn status_king_blocked_no_flying_capture_possible() {
        // King at A1 is completely surrounded with no escape:
        // - B1, C1 block right movement and capture
        // - A2, A3 block up movement and capture
        let board = Board::from_squares(
            Team::White,
            &[Square::A1],
            &[Square::A2, Square::A3, Square::B1, Square::C1],
            &[Square::A1], // A1 is a king
        );
        // King cannot move or capture - blocked
        assert_eq!(board.status(), GameStatus::Won(Team::Black));
    }

    #[test]
    fn swap_turn_returns_new_board() {
        let board = Board::new_default();
        let swapped = board.swap_turn();
        assert_eq!(swapped.turn, Team::Black);
        assert_eq!(board.turn, Team::White); // Original unchanged
    }

    #[test]
    fn display_format() {
        let board = Board::from_squares(Team::White, &[Square::A1], &[Square::H8], &[]);
        let display = format!("{}", board);
        assert!(display.contains("Turn: White"));
        assert!(display.contains("Status:"));
    }

    #[test]
    fn default_board() {
        let board = Board::default();
        assert_eq!(board, Board::new_default());
    }

    #[test]
    fn status_black_pawn_blocked_can_capture_backward() {
        // Black pawn at A7 surrounded, but can capture down (backward for black)
        // This tests the vertical capture path for Black team
        let board = Board::from_squares(Team::Black, &[Square::A7], &[Square::A6, Square::B7], &[]);
        // Black pawn can capture A6 (moving down/backward)
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn status_king_can_move_backward() {
        // White king at D4, surrounded except can move backward (down)
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::C4, Square::E4, Square::D5],
            &[Square::D4],
        );
        // King can move down to D3
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn is_blocked_black_king_only_backward_move() {
        // Black king at A2, can move up (backward for Black) to A3
        // Left=edge, right=hostile, forward(down)=hostile
        let board = Board::from_squares(
            Team::Black,
            &[Square::A2],
            &[Square::A1, Square::B2],
            &[Square::A2],
        );
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn is_blocked_black_vertical_capture_only() {
        // Black pawn at A3, can only capture forward (down to A1 over A2)
        // Left: edge. Right move: B3 hostile. Right capture: C3 hostile blocks landing.
        // Covers lines 320-321: Black vertical capture branch
        let board = Board::from_squares(
            Team::Black,
            &[Square::A3],
            &[Square::A2, Square::B3, Square::C3], // A2=capturable, B3=block right move, C3=block right capture
            &[],
        );
        assert_eq!(board.status(), GameStatus::InProgress);
    }

    #[test]
    fn is_blocked_left_capture_only() {
        // White pawn at C3, can only capture left (to A3 over B3)
        // Right: D3 hostile blocks move. E3 hostile blocks right capture landing.
        // Covers line 303: left capture return
        let board = Board::from_squares(
            Team::White,
            &[Square::C3],
            &[Square::B3, Square::C4, Square::D3, Square::E3], // B3=left capture, C4+D3=block moves, E3=block right capture
            &[],
        );
        assert_eq!(board.status(), GameStatus::InProgress);
    }
}