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
637
638
639
640
641
642
643
//! Section 6: Capturing Rules tests
//!
//! Tests for mandatory capture, men/king captures, chain captures,
//! maximum capture rule, and 180-degree turn prohibition.

use kish::{Board, Square, Team};

// =============================================================================
// 6.1 Mandatory Capture
// =============================================================================

/// Rule 6.1: Captures are mandatory - cannot make non-capturing move if capture available
#[test]
fn captures_are_mandatory() {
    let board = Board::from_squares(Team::White, &[Square::D4], &[Square::D5, Square::H8], &[]);
    let actions = board.actions();

    // All actions must be captures
    for action in &actions {
        assert_ne!(
            action.delta.pieces[Team::Black.to_usize()],
            0,
            "Must capture when capture is available"
        );
    }
}

/// Rule 6.1: Moves allowed when no capture available
#[test]
fn moves_allowed_when_no_capture() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::H8], // Far away, no capture possible
        &[],
    );
    let actions = board.actions();

    // All actions should be moves (non-captures)
    for action in &actions {
        assert_eq!(
            action.delta.pieces[Team::Black.to_usize()],
            0,
            "Should be moves, not captures"
        );
    }
    assert!(!actions.is_empty(), "Should have moves available");
}

/// Rule 6.1: Multiple pieces, only one can capture - must use that one
#[test]
fn only_capturing_piece_can_move() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4, Square::A1],
        &[Square::D5], // Only D4 can capture D5
        &[],
    );
    let actions = board.actions();

    // Only capture actions should be returned
    assert_eq!(actions.len(), 1, "Only one capture available");
    assert_ne!(
        actions[0].delta.pieces[Team::Black.to_usize()],
        0,
        "Must be a capture"
    );
}

// =============================================================================
// 6.2 Men's Captures
// =============================================================================

/// Rule 6.2: Men capture forward
#[test]
fn white_pawn_captures_forward() {
    let board = Board::from_squares(Team::White, &[Square::D4], &[Square::D5], &[]);
    let actions = board.actions();

    // Should capture D5, land on D6
    assert_eq!(actions.len(), 1);
    assert_eq!(
        actions[0].delta.pieces[Team::Black.to_usize()],
        Square::D5.to_mask()
    );
}

/// Rule 6.2: Men capture left (sideways)
#[test]
fn white_pawn_captures_left() {
    let board = Board::from_squares(Team::White, &[Square::D4], &[Square::C4], &[]);
    let actions = board.actions();

    // Should capture C4, land on B4
    assert_eq!(actions.len(), 1);
    assert_eq!(
        actions[0].delta.pieces[Team::Black.to_usize()],
        Square::C4.to_mask()
    );
}

/// Rule 6.2: Men capture right (sideways)
#[test]
fn white_pawn_captures_right() {
    let board = Board::from_squares(Team::White, &[Square::D4], &[Square::E4], &[]);
    let actions = board.actions();

    // Should capture E4, land on F4
    assert_eq!(actions.len(), 1);
    assert_eq!(
        actions[0].delta.pieces[Team::Black.to_usize()],
        Square::E4.to_mask()
    );
}

/// Rule 6.2: Men cannot capture backward
#[test]
fn white_pawn_cannot_capture_backward() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::D3, Square::D5], // D3 is behind, D5 is in front
        &[],
    );
    let actions = board.actions();

    // Should only capture D5 (forward), not D3 (backward)
    assert_eq!(actions.len(), 1);
    assert_eq!(
        actions[0].delta.pieces[Team::Black.to_usize()],
        Square::D5.to_mask(),
        "Should only capture forward (D5), not backward (D3)"
    );
}

/// Rule 6.2: Men cannot capture backward - black pawn
#[test]
fn black_pawn_cannot_capture_backward() {
    let board = Board::from_squares(
        Team::Black,
        &[Square::D5, Square::D3], // D5 is behind (up) for black, D3 is forward (down)
        &[Square::D4],
        &[],
    );
    let actions = board.actions();

    // Should only capture D3 (forward for black = down), not D5 (backward)
    assert_eq!(actions.len(), 1);
    assert_eq!(
        actions[0].delta.pieces[Team::White.to_usize()],
        Square::D3.to_mask(),
        "Should only capture forward (D3), not backward (D5)"
    );
}

/// Rule 6.2: Men cannot capture diagonally
#[test]
fn pawn_cannot_capture_diagonally() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::C3, Square::C5, Square::E3, Square::E5],
        &[],
    );
    let actions = board.actions();

    // Should have NO captures (all enemies are diagonal)
    // Without captures, should have moves instead
    for action in &actions {
        assert_eq!(
            action.delta.pieces[Team::Black.to_usize()],
            0,
            "Should not capture diagonal enemies"
        );
    }
}

// =============================================================================
// 6.3 King's Captures (Flying Capture)
// =============================================================================

/// Rule 6.3: King can capture from any distance
#[test]
fn king_captures_from_distance() {
    let board = Board::from_squares(
        Team::White,
        &[Square::A4],
        &[Square::E4], // Distance 4 from A4
        &[Square::A4],
    );
    let actions = board.actions();

    // King can capture E4 from A4 (flying capture)
    assert!(!actions.is_empty(), "King should have captures");
    for action in &actions {
        assert_eq!(
            action.delta.pieces[Team::Black.to_usize()],
            Square::E4.to_mask()
        );
    }
}

/// Rule 6.3: King can land on any empty square beyond the captured piece
#[test]
fn king_can_land_anywhere_beyond_captured() {
    let board = Board::from_squares(
        Team::White,
        &[Square::A4],
        &[Square::D4], // After capturing, can land on E4, F4, G4, or H4
        &[Square::A4],
    );
    let actions = board.actions();

    // Should have 4 landing options: E4, F4, G4, H4
    assert_eq!(actions.len(), 4, "King should have 4 landing options");

    let landing_squares: Vec<u64> = actions
        .iter()
        .map(|a| a.delta.pieces[Team::White.to_usize()] & !Square::A4.to_mask())
        .collect();

    assert!(landing_squares.contains(&Square::E4.to_mask()));
    assert!(landing_squares.contains(&Square::F4.to_mask()));
    assert!(landing_squares.contains(&Square::G4.to_mask()));
    assert!(landing_squares.contains(&Square::H4.to_mask()));
}

/// Rule 6.3: King captures in all 4 directions
#[test]
fn king_captures_in_all_directions() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::B4, Square::F4, Square::D2, Square::D6],
        &[Square::D4],
    );
    let actions = board.actions();

    // King can capture in any direction - check all are available
    // Due to max capture rule, single captures may not all be returned if chains exist
    // But all 4 pieces should be capturable
    let captured_pieces: u64 = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()])
        .fold(0, |acc, x| acc | x);

    // At least one capture should be possible
    assert!(captured_pieces != 0, "King should be able to capture");
}

/// Rule 6.3: King cannot jump over two adjacent pieces at once
#[test]
fn king_cannot_jump_two_pieces_at_once() {
    // When two enemy pieces are adjacent (C4, D4), and king at A4,
    // the king cannot capture C4 because there's no landing square
    // between C4 and D4, and D4 blocks the path.
    // This tests that you can't skip over two pieces in a single jump.
    let board = Board::from_squares(
        Team::White,
        &[Square::A4],
        &[Square::C4, Square::D4], // Two adjacent enemies - no landing between them
        &[Square::A4],
    );
    let actions = board.actions();

    // King at A4 cannot capture C4 (would land on D4 which is occupied)
    // King at A4 cannot capture D4 (C4 is in the way)
    // So no captures are possible - only moves available
    for action in &actions {
        // All actions should be moves (no captures possible)
        assert_eq!(
            action.delta.pieces[Team::Black.to_usize()],
            0,
            "No captures should be possible with two adjacent blocking pieces"
        );
    }
    assert!(!actions.is_empty(), "Should have moves available");
}

// =============================================================================
// 6.4 Immediate Removal Rule
// =============================================================================

/// Rule 6.4: Captured pieces removed immediately (can cross vacated square)
#[test]
fn captured_piece_removed_immediately_allows_crossing() {
    // King at A4, enemies at C4 and E4
    // After capturing C4 -> land at D4
    // From D4, can capture E4 (would cross where C4 was, but it's removed)
    let board = Board::from_squares(
        Team::White,
        &[Square::A4],
        &[Square::C4, Square::E4],
        &[Square::A4],
    );
    let actions = board.actions();

    // Should be able to capture both pieces
    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(
        max_captures, 2,
        "Should capture 2 pieces using immediate removal"
    );
}

/// Rule 6.4: Pawn chain capture with immediate removal
#[test]
fn pawn_chain_capture_with_immediate_removal() {
    // White pawn at A2, enemies at A3 and B4
    // Capture A3 -> land A4
    // From A4, capture B4 -> land C4
    let board = Board::from_squares(Team::White, &[Square::A2], &[Square::A3, Square::B4], &[]);
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 2, "Pawn should capture 2 pieces in chain");
}

// =============================================================================
// 6.5 Multi-Capture (Chain Captures)
// =============================================================================

/// Rule 6.5: Multi-capture must continue if more captures available
#[test]
fn multi_capture_must_continue() {
    let board = Board::from_squares(Team::White, &[Square::A2], &[Square::A3, Square::B4], &[]);
    let actions = board.actions();

    // Should only return the full chain (2 captures), not partial (1 capture)
    for action in &actions {
        let captures = action.delta.pieces[Team::Black.to_usize()].count_ones();
        assert_eq!(captures, 2, "Must complete the full capture chain");
    }
}

/// Rule 6.5: Three-piece capture chain
#[test]
fn three_piece_capture_chain() {
    let board = Board::from_squares(
        Team::White,
        &[Square::A2],
        &[Square::A3, Square::B4, Square::C5],
        &[],
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 3, "Should capture 3 pieces in chain");
}

/// Rule 6.5: Four-piece capture chain
#[test]
fn four_piece_capture_chain() {
    let board = Board::from_squares(
        Team::White,
        &[Square::A2],
        &[Square::A3, Square::B4, Square::C5, Square::D6],
        &[],
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 4, "Should capture 4 pieces in chain");
}

/// Rule 6.5: King chain capture
#[test]
fn king_chain_capture() {
    let board = Board::from_squares(
        Team::White,
        &[Square::A4],
        &[Square::C4, Square::E6],
        &[Square::A4],
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 2, "King should capture 2 pieces in chain");
}

// =============================================================================
// 6.6 Maximum Capture Rule (Majority Rule)
// =============================================================================

/// Rule 6.6: Must choose sequence that captures the most pieces
#[test]
fn maximum_capture_rule_enforced() {
    // White pawn at D4
    // Path 1: capture D5 (1 piece)
    // Path 2: capture C4->B4, then B5->B6 (2 pieces via chain)
    // Must choose path 2
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::D5, Square::C4, Square::B5],
        &[],
    );
    let actions = board.actions();

    // All returned actions should be the maximum length
    for action in &actions {
        let captures = action.delta.pieces[Team::Black.to_usize()].count_ones();
        assert!(captures >= 2, "Must choose maximum capture sequence");
    }
}

/// Rule 6.6: Multiple sequences of same length - can choose any
#[test]
fn equal_length_captures_all_available() {
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::D5, Square::C4, Square::E4],
        &[],
    );
    let actions = board.actions();

    // All should be 1-capture (equal length)
    assert_eq!(
        actions.len(),
        3,
        "Should have 3 equal-length capture options"
    );
    for action in &actions {
        let captures = action.delta.pieces[Team::Black.to_usize()].count_ones();
        assert_eq!(captures, 1, "All captures should be length 1");
    }
}

/// Rule 6.6: No distinction between men and kings when counting
#[test]
fn men_and_kings_count_equally() {
    // D4 pawn, D5 king (1 capture), C4+B5 pawns (2 captures)
    // Must prefer 2 captures even though path 1 captures a king
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::D5, Square::C4, Square::B5],
        &[Square::D5], // D5 is a king
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(
        max_captures, 2,
        "Should prefer 2-pawn capture over 1-king capture"
    );
}

// =============================================================================
// 6.7 180-Degree Turn Prohibition
// =============================================================================

/// Rule 6.7: Cannot turn 180° between consecutive captures (up then down)
#[test]
fn king_cannot_reverse_up_down_during_capture() {
    // King at D5, enemies at D3 and D7
    // Capture D7 (up) -> land D8
    // From D8, capture D3 would require going DOWN (180° turn) - blocked!
    let board = Board::from_squares(
        Team::White,
        &[Square::D5],
        &[Square::D3, Square::D7],
        &[Square::D5],
    );
    let actions = board.actions();

    // Should NOT be able to capture both (would require 180° turn)
    for action in &actions {
        let captures = action.delta.pieces[Team::Black.to_usize()].count_ones();
        assert_eq!(
            captures, 1,
            "Should NOT chain captures requiring 180° turn (up then down)"
        );
    }
}

/// Rule 6.7: Cannot turn 180° between consecutive captures (left then right)
#[test]
fn king_cannot_reverse_left_right_during_capture() {
    // King at D4, enemies at B4 (left) and F4 (right)
    // Capture B4 (left) -> land A4
    // From A4, capture F4 would require going RIGHT (180° turn) - blocked!
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::B4, Square::F4],
        &[Square::D4],
    );
    let actions = board.actions();

    for action in &actions {
        let captures = action.delta.pieces[Team::Black.to_usize()].count_ones();
        assert_eq!(
            captures, 1,
            "Should NOT chain captures requiring 180° turn (left then right)"
        );
    }
}

/// Rule 6.7: 90° turns ARE allowed between captures
#[test]
fn king_can_turn_90_degrees_during_capture() {
    // King at D4, enemy at D6 (up), enemy at F7 (right from D7 area)
    // Capture D6 (up) -> land D7
    // From D7, capture F7 (right) - 90° turn, allowed!
    let board = Board::from_squares(
        Team::White,
        &[Square::D4],
        &[Square::D6, Square::F7],
        &[Square::D4],
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 2, "90° turn should be allowed");
}

/// Rule 6.7: Complex 90° turn scenario (L-shape)
#[test]
fn king_90_degree_l_shape_capture() {
    // King at A1, enemies at A3 (up), C5 (right), E3 (down)
    // Path: A1 -> capture A3 (up) -> land A4
    //       A4 -> not directly to C5... let me recalculate
    // Better: A1 -> A3 (up) -> land A5
    //         A5 -> C5 (right) -> land E5
    //         E5 -> E3 (down) - 90° from right
    let board = Board::from_squares(
        Team::White,
        &[Square::A1],
        &[Square::A3, Square::C5, Square::E3],
        &[Square::A1],
    );
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(max_captures, 3, "Should chain 3 captures with 90° turns");
}

// =============================================================================
// 6.8 Black Team Captures (symmetric behavior)
// =============================================================================

/// Rule 6.2: Black pawn captures forward (down)
#[test]
fn black_pawn_captures_forward_vertical() {
    // Black pawn at D5 captures D4 (below), lands on D3
    let board = Board::from_squares(Team::Black, &[Square::D4], &[Square::D5], &[]);
    let actions = board.actions();

    // Should capture D4, land on D3
    assert_eq!(actions.len(), 1, "Black pawn should have 1 capture");
    assert_eq!(
        actions[0].delta.pieces[Team::White.to_usize()],
        Square::D4.to_mask(),
        "Black pawn should capture D4"
    );
}

/// Rule 6.2: Black pawn vertical chain capture (down)
#[test]
fn black_pawn_vertical_chain_capture() {
    // Black pawn at D6 captures D5, lands D4, then captures D3, lands D2
    let board = Board::from_squares(Team::Black, &[Square::D5, Square::D3], &[Square::D6], &[]);
    let actions = board.actions();

    let max_captures = actions
        .iter()
        .map(|a| a.delta.pieces[Team::White.to_usize()].count_ones())
        .max()
        .unwrap_or(0);

    assert_eq!(
        max_captures, 2,
        "Black pawn should chain 2 vertical captures"
    );
}

/// Rule 6.3: Black king captures in all directions
#[test]
fn black_king_captures_all_directions() {
    // Black king at D4, white pieces in all 4 directions
    let board = Board::from_squares(
        Team::Black,
        &[Square::B4, Square::F4, Square::D2, Square::D6],
        &[Square::D4],
        &[Square::D4], // D4 is a black king
    );
    let actions = board.actions();

    // All actions should be captures (mandatory capture rule)
    assert!(
        !actions.is_empty(),
        "Black king should have capture options"
    );
    for action in &actions {
        assert_ne!(
            action.delta.pieces[Team::White.to_usize()],
            0,
            "All actions should be captures"
        );
    }
}