chess-vector-engine 0.5.1

Open source chess engine with hybrid vector-based position analysis, advanced tactical search, and NNUE neural network evaluation
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
use chess::{Board, ChessMove};
use std::collections::HashMap;
use std::str::FromStr;

/// Opening book entry containing position evaluation and recommended moves
#[derive(Debug, Clone)]
pub struct OpeningEntry {
    pub evaluation: f32,
    pub best_moves: Vec<(ChessMove, f32)>, // (move, relative_strength)
    pub name: String,
    pub eco_code: Option<String>, // ECO (Encyclopedia of Chess Openings) code
}

/// Opening book for chess positions
#[derive(Clone)]
pub struct OpeningBook {
    /// Map from FEN string to opening entry
    entries: HashMap<String, OpeningEntry>,
}

impl Default for OpeningBook {
    fn default() -> Self {
        Self::new()
    }
}

impl OpeningBook {
    /// Create a new opening book
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Create a basic opening book with common openings
    pub fn with_standard_openings() -> Self {
        let mut book = Self::new();
        book.add_standard_openings();
        book
    }

    /// Add an opening entry
    pub fn add_opening(
        &mut self,
        fen: &str,
        evaluation: f32,
        best_moves: Vec<(ChessMove, f32)>,
        name: String,
        eco_code: Option<String>,
    ) -> Result<(), String> {
        // Validate FEN by parsing it
        Board::from_str(fen).map_err(|_e| "Invalid FEN".to_string())?;

        let entry = OpeningEntry {
            evaluation,
            best_moves,
            name,
            eco_code,
        };

        self.entries.insert(fen.to_string(), entry);
        Ok(())
    }

    /// Look up position in opening book
    pub fn lookup(&self, board: &Board) -> Option<&OpeningEntry> {
        let fen = board.to_string();
        self.entries.get(&fen)
    }

    /// Check if position is in opening book
    pub fn contains(&self, board: &Board) -> bool {
        let fen = board.to_string();
        self.entries.contains_key(&fen)
    }

    /// Get all known openings
    pub fn get_all_openings(&self) -> &HashMap<String, OpeningEntry> {
        &self.entries
    }

    /// Get a random opening move from the starting position
    pub fn get_random_opening(&self) -> Option<ChessMove> {
        use rand::seq::SliceRandom;
        let board = Board::default();
        if let Some(entry) = self.lookup(&board) {
            let moves: Vec<ChessMove> = entry.best_moves.iter().map(|(mv, _)| *mv).collect();
            moves.choose(&mut rand::thread_rng()).copied()
        } else {
            None
        }
    }

    /// Add standard chess openings
    fn add_standard_openings(&mut self) {
        // Starting position
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
        {
            let moves = vec![
                (ChessMove::from_str("e2e4").unwrap(), 1.0), // King's Pawn
                (ChessMove::from_str("d2d4").unwrap(), 0.9), // Queen's Pawn
                (ChessMove::from_str("g1f3").unwrap(), 0.8), // King's Knight
                (ChessMove::from_str("c2c4").unwrap(), 0.7), // English Opening
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.0,
                moves,
                "Starting Position".to_string(),
                None,
            );
        }

        // King's Pawn Game: 1.e4
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
        {
            let moves = vec![
                (ChessMove::from_str("e7e5").unwrap(), 1.0), // King's Pawn response
                (ChessMove::from_str("c7c5").unwrap(), 0.9), // Sicilian Defense
                (ChessMove::from_str("e7e6").unwrap(), 0.7), // French Defense
                (ChessMove::from_str("c7c6").unwrap(), 0.6), // Caro-Kann Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.25,
                moves,
                "King's Pawn Game".to_string(),
                Some("B00".to_string()),
            );
        }

        // King's Pawn Game: 1.e4 e5
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
        {
            let moves = vec![
                (ChessMove::from_str("g1f3").unwrap(), 1.0), // King's Knight Attack
                (ChessMove::from_str("f2f4").unwrap(), 0.6), // King's Gambit
                (ChessMove::from_str("b1c3").unwrap(), 0.5), // Vienna Game
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.15,
                moves,
                "Open Game".to_string(),
                Some("C20".to_string()),
            );
        }

        // Italian Game: 1.e4 e5 2.Nf3 Nc6 3.Bc4
        if let Ok(board) =
            Board::from_str("r1bqkbnr/pppp1ppp/2n5/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3")
        {
            let moves = vec![
                (ChessMove::from_str("g8f6").unwrap(), 1.0), // Italian Game main line
                (ChessMove::from_str("f7f5").unwrap(), 0.6), // Rousseau Gambit
                (ChessMove::from_str("f8e7").unwrap(), 0.4), // Hungarian Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.25,
                moves,
                "Italian Game".to_string(),
                Some("C50".to_string()),
            );
        }

        // Ruy Lopez: 1.e4 e5 2.Nf3 Nc6 3.Bb5
        if let Ok(board) =
            Board::from_str("r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3")
        {
            let moves = vec![
                (ChessMove::from_str("a7a6").unwrap(), 1.0), // Morphy Defense
                (ChessMove::from_str("g8f6").unwrap(), 0.9), // Berlin Defense
                (ChessMove::from_str("f7f5").unwrap(), 0.4), // Schliemann Defense
                (ChessMove::from_str("b8d4").unwrap(), 0.3), // Bird Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.3,
                moves,
                "Ruy Lopez".to_string(),
                Some("C60".to_string()),
            );
        }

        // Sicilian Defense: 1.e4 c5
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
        {
            let moves = vec![
                (ChessMove::from_str("g1f3").unwrap(), 1.0), // Open Sicilian
                (ChessMove::from_str("b1c3").unwrap(), 0.7), // Closed Sicilian
                (ChessMove::from_str("f2f4").unwrap(), 0.5), // Grand Prix Attack
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.3,
                moves,
                "Sicilian Defense".to_string(),
                Some("B20".to_string()),
            );
        }

        // French Defense: 1.e4 e6
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
        {
            let moves = vec![
                (ChessMove::from_str("d2d4").unwrap(), 1.0), // French Defense main line
                (ChessMove::from_str("d2d3").unwrap(), 0.5), // King's Indian Attack
                (ChessMove::from_str("g1f3").unwrap(), 0.6), // Two Knights Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.35,
                moves,
                "French Defense".to_string(),
                Some("C00".to_string()),
            );
        }

        // Caro-Kann Defense: 1.e4 c6
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pp1ppppp/2p5/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
        {
            let moves = vec![
                (ChessMove::from_str("d2d4").unwrap(), 1.0), // Caro-Kann main line
                (ChessMove::from_str("b1c3").unwrap(), 0.7), // Two Knights Attack
                (ChessMove::from_str("f2f4").unwrap(), 0.4), // Hillbilly Attack
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.3,
                moves,
                "Caro-Kann Defense".to_string(),
                Some("B10".to_string()),
            );
        }

        // Queen's Pawn Game: 1.d4
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1")
        {
            let moves = vec![
                (ChessMove::from_str("d7d5").unwrap(), 1.0), // Queen's Gambit
                (ChessMove::from_str("g8f6").unwrap(), 0.9), // Indian Defenses
                (ChessMove::from_str("f7f5").unwrap(), 0.4), // Dutch Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.2,
                moves,
                "Queen's Pawn Game".to_string(),
                Some("D00".to_string()),
            );
        }

        // Queen's Gambit: 1.d4 d5 2.c4
        if let Ok(board) =
            Board::from_str("rnbqkbnr/ppp1pppp/8/3p4/2PP4/8/PP2PPPP/RNBQKBNR b KQkq - 0 2")
        {
            let moves = vec![
                (ChessMove::from_str("d5c4").unwrap(), 0.7), // Queen's Gambit Accepted
                (ChessMove::from_str("e7e6").unwrap(), 1.0), // Queen's Gambit Declined
                (ChessMove::from_str("c7c6").unwrap(), 0.8), // Slav Defense
                (ChessMove::from_str("d5d4").unwrap(), 0.5), // Marshall Defense
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.25,
                moves,
                "Queen's Gambit".to_string(),
                Some("D06".to_string()),
            );
        }

        // English Opening: 1.c4
        if let Ok(board) =
            Board::from_str("rnbqkbnr/pppppppp/8/8/2P5/8/PP1PPPPP/RNBQKBNR b KQkq - 0 1")
        {
            let moves = vec![
                (ChessMove::from_str("g8f6").unwrap(), 1.0), // Anglo-Indian
                (ChessMove::from_str("e7e5").unwrap(), 0.9), // Reversed Sicilian
                (ChessMove::from_str("c7c5").unwrap(), 0.8), // Symmetrical English
                (ChessMove::from_str("e7e6").unwrap(), 0.7), // Anglo-French
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.15,
                moves,
                "English Opening".to_string(),
                Some("A10".to_string()),
            );
        }

        // Nimzo-Indian Defense: 1.d4 Nf6 2.c4 e6 3.Nc3 Bb4
        if let Ok(board) =
            Board::from_str("rnbqk2r/pppp1ppp/4pn2/8/1bPP4/2N5/PP2PPPP/R1BQKBNR w KQkq - 2 4")
        {
            let moves = vec![
                (ChessMove::from_str("e2e3").unwrap(), 0.9), // Rubinstein System
                (ChessMove::from_str("f2f3").unwrap(), 0.7), // Kmoch Variation
                (ChessMove::from_str("a2a3").unwrap(), 0.8), // Saemisch Variation
                (ChessMove::from_str("d1c2").unwrap(), 1.0), // Classical Variation
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.1,
                moves,
                "Nimzo-Indian Defense".to_string(),
                Some("E20".to_string()),
            );
        }

        // King's Indian Defense setup: 1.d4 Nf6 2.c4 g6
        if let Ok(board) =
            Board::from_str("rnbqkb1r/pppppp1p/5np1/8/2PP4/8/PP2PPPP/RNBQKBNR w KQkq - 0 3")
        {
            let moves = vec![
                (ChessMove::from_str("b1c3").unwrap(), 1.0), // Classical setup
                (ChessMove::from_str("g1f3").unwrap(), 0.9), // Fianchetto setup
                (ChessMove::from_str("f2f3").unwrap(), 0.6), // Saemisch Variation
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.2,
                moves,
                "King's Indian Defense".to_string(),
                Some("E60".to_string()),
            );
        }

        // Sicilian Najdorf: 1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6
        if let Ok(board) =
            Board::from_str("rnbqkb1r/1p2pppp/p2p1n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 0 6")
        {
            let moves = vec![
                (ChessMove::from_str("c1e3").unwrap(), 1.0), // English Attack
                (ChessMove::from_str("f2f3").unwrap(), 0.9), // Be3 system
                (ChessMove::from_str("h2h3").unwrap(), 0.7), // Positional setup
            ];
            let _ = self.add_opening(
                &board.to_string(),
                0.2,
                moves,
                "Sicilian Najdorf".to_string(),
                Some("B90".to_string()),
            );
        }
    }

    /// Get opening book statistics for evaluation
    pub fn get_statistics(&self) -> OpeningBookStats {
        let total_openings = self.entries.len();
        let eco_coverage = self
            .entries
            .values()
            .filter(|entry| entry.eco_code.is_some())
            .count();

        OpeningBookStats {
            total_openings,
            eco_coverage,
            avg_moves_per_opening: if total_openings > 0 {
                self.entries
                    .values()
                    .map(|entry| entry.best_moves.len())
                    .sum::<usize>() as f32
                    / total_openings as f32
            } else {
                0.0
            },
        }
    }
}

/// Statistics about the opening book coverage
#[derive(Debug, Clone)]
pub struct OpeningBookStats {
    pub total_openings: usize,
    pub eco_coverage: usize,
    pub avg_moves_per_opening: f32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_opening_book_creation() {
        let book = OpeningBook::new();
        assert_eq!(book.entries.len(), 0);
    }

    #[test]
    fn test_standard_openings() {
        let book = OpeningBook::with_standard_openings();
        assert!(!book.entries.is_empty());

        // Test starting position lookup
        let board = Board::default();
        let entry = book.lookup(&board);
        assert!(entry.is_some());
        assert_eq!(entry.unwrap().name, "Starting Position");
    }

    #[test]
    fn test_opening_lookup() {
        let mut book = OpeningBook::new();
        let board = Board::default();

        // Should not be found initially
        assert!(!book.contains(&board));

        // Add entry
        let moves = vec![(ChessMove::from_str("e2e4").unwrap(), 1.0)];
        book.add_opening(
            &board.to_string(),
            0.0,
            moves,
            "Test Opening".to_string(),
            None,
        )
        .unwrap();

        // Should be found now
        assert!(book.contains(&board));
        let entry = book.lookup(&board).unwrap();
        assert_eq!(entry.name, "Test Opening");
        assert_eq!(entry.best_moves.len(), 1);
    }

    #[test]
    fn test_comprehensive_opening_coverage() {
        let book = OpeningBook::with_standard_openings();
        let stats = book.get_statistics();

        // Should have substantial opening coverage
        assert!(
            stats.total_openings >= 12,
            "Expected at least 12 openings, got {}",
            stats.total_openings
        );
        assert!(
            stats.eco_coverage >= 8,
            "Expected at least 8 ECO codes, got {}",
            stats.eco_coverage
        );
        assert!(
            stats.avg_moves_per_opening >= 2.0,
            "Expected average 2+ moves per opening"
        );

        // Test that major openings are covered
        let opening_names: Vec<_> = book.entries.values().map(|entry| &entry.name).collect();

        let has_sicilian = opening_names.iter().any(|name| name.contains("Sicilian"));
        let has_italian = opening_names.iter().any(|name| name.contains("Italian"));
        let has_ruy_lopez = opening_names.iter().any(|name| name.contains("Ruy Lopez"));
        let has_french = opening_names.iter().any(|name| name.contains("French"));

        assert!(has_sicilian, "Should have Sicilian Defense");
        assert!(has_italian, "Should have Italian Game");
        assert!(has_ruy_lopez, "Should have Ruy Lopez");
        assert!(has_french, "Should have French Defense");
    }
}