simple_chess 1.0.6

A simple chess game implimentation
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
use crate::chess_game::ChessGame;
use crate::chess_game_builder::ChessGameBuilder;
use crate::piece::ChessPiece;
use crate::piece::PieceType::{Bishop, King, Knight, Pawn, Queen, Rook};
use crate::ChessMoveType;
use crate::ChessMoveType::EnPassant;
use crate::Color::{Black, White};
use game_board::Board;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};

const WHITE_PAWN: char = 'P';
const BLACK_PAWN: char = 'p';
const WHITE_ROOK: char = 'R';
const BLACK_ROOK: char = 'r';
const WHITE_KNIGHT: char = 'N';
const BLACK_KNIGHT: char = 'n';
const WHITE_BISHOP: char = 'B';
const BLACK_BISHOP: char = 'b';
const WHITE_QUEEN: char = 'Q';
const BLACK_QUEEN: char = 'q';
const WHITE_KING: char = 'K';
const BLACK_KING: char = 'k';

/// Encodes the current state of the simple_chess game as a string in FEN (Forsyth-Edwards Notation) format.
///
/// The resulting string consists of the following parts:
///
/// 1. The board layout, represented by rows separated by slashes, where each piece is represented
///    by a character and empty squares are represented by numbers.
/// 2. The current turn, indicated by 'w' for White or 'b' for Black.
/// 3. Castling rights, represented by 'K', 'Q', 'k', and 'q' for White king-side, White queen-side,
///    Black king-side, and Black queen-side castling respectively. If no castling rights are available,
///    a dash '-' is used instead.
/// 4. The en passant target square, represented by the algebraic notation of the target square
///    for en passant capture, such as 'e3'. If no en passant target square is available, a dash '-'
///    is used instead.
/// 5. The number of half-moves since the last capture or pawn advance, for the fifty-move rule.
/// 6. The full move number, starting from 1 and incremented after Black's turn.
///
/// # Arguments
///
/// * `game` - A reference to the `ChessGame` instance representing the current state of the game.
///
/// # Returns
///
/// A `String` representing the current state of the simple_chess game.
pub fn encode_game_as_string(game: &ChessGame) -> String {
    format!(
        "{} {} {} {} {} {}",
        get_board_as_fen_string(game),
        get_current_turn_char(game),
        get_castling_rights(game),
        get_en_passant(game),
        game.get_50_move_rule_counter(),
        game.get_turn_number()
    )
}

/// Builds a `ChessGame` from a string in Forsyth-Edwards Notation (FEN) format.
///
/// This function parses the FEN string and constructs the game state, including the board layout,
/// current turn, castling rights, en passant target square, half-move counter, and full move number.
///
/// # Arguments
///
/// * `fen_string` - A string slice representing the state of the simple_chess game in FEN format.
///
/// # Returns
///
/// A `Result` which is `Ok` if the `ChessGame` was built successfully, or an `Err` containing
/// a `ForsythEdwardsNotationError` if the FEN string is invalid or cannot be parsed.
///
/// # Example
/// ```
/// use simple_chess::codec::forsyth_edwards_notation::build_game_from_string;
///
/// let starting_position_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
/// let game = build_game_from_string(starting_position_string);
/// assert!(game.is_ok());
/// ```
pub fn build_game_from_string(fen_string: &str) -> Result<ChessGame, ForsythEdwardsNotationError> {
    let fen_string = fen_string.trim();
    if fen_string.is_empty() {
        return Err(ForsythEdwardsNotationError::new(
            "argument must be a string in Forsyth–Edwards Notation".to_string(),
        ));
    }

    let steps = [
        parse_board_from_string,
        parse_current_turn_from_string,
        parse_castling_rights_from_string,
        parse_en_passant_option_from_string,
        parse_half_turn_counter_from_string,
        parse_turn_number_from_string,
    ];

    let mut parts = fen_string.split(" ");
    let mut builder = ChessGameBuilder::new();

    for step in steps {
        if let Some(next) = parts.next() {
            builder = match step(builder, next) {
                Ok(g) => g,
                Err(e) => return Err(e),
            };
        } else {
            return Err(ForsythEdwardsNotationError::new(
                "Missing some parts of the string".to_string(),
            ));
        }
    }

    match builder.build() {
        Ok(g) => Ok(g),
        Err(e) => Err(ForsythEdwardsNotationError::new(e.to_string())),
    }
}

fn parse_board_from_string(
    builder: ChessGameBuilder,
    board_as_fen_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    let mut board = Board::build(8, 8).unwrap();

    let mut files = board_as_fen_string.split("/");

    let mut col = 0;
    for row in (0..8).rev() {
        let file = files.next().unwrap();

        for c in file.chars() {
            match c {
                '1'..='8' => {
                    col += c.to_digit(10).unwrap() as usize;
                }
                _ => {
                    let piece = match c {
                        WHITE_PAWN => ChessPiece::new(Pawn, White),
                        BLACK_PAWN => ChessPiece::new(Pawn, Black),
                        WHITE_ROOK => ChessPiece::new(Rook, White),
                        BLACK_ROOK => ChessPiece::new(Rook, Black),
                        WHITE_KNIGHT => ChessPiece::new(Knight, White),
                        BLACK_KNIGHT => ChessPiece::new(Knight, Black),
                        WHITE_BISHOP => ChessPiece::new(Bishop, White),
                        BLACK_BISHOP => ChessPiece::new(Bishop, Black),
                        WHITE_QUEEN => ChessPiece::new(Queen, White),
                        BLACK_QUEEN => ChessPiece::new(Queen, Black),
                        WHITE_KING => ChessPiece::new(King, White),
                        BLACK_KING => ChessPiece::new(King, Black),
                        _ => {
                            return Err(ForsythEdwardsNotationError::new(format!(
                                "Unexpected char '{c}' in file '{file}' of piece placement data"
                            )))
                        }
                    };
                    board.place_piece(piece, col, row);
                    col += 1;
                }
            }
        }

        if col != 8 {
            return Err(ForsythEdwardsNotationError::new(format!(
                "File '{file}' was not 8 spaces long in piece placement data"
            )));
        }

        col = 0;
    }

    Ok(builder.set_board(board))
}

fn parse_current_turn_from_string(
    builder: ChessGameBuilder,
    current_turn_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    match current_turn_string {
        "w" => Ok(builder.set_current_turn(White)),
        "b" => Ok(builder.set_current_turn(Black)),
        _ => Err(ForsythEdwardsNotationError::new(format!("encountered unexpected token parsing turn from FEN string, Expected 'w' or 'b', received {current_turn_string}")))
    }
}

fn parse_castling_rights_from_string(
    builder: ChessGameBuilder,
    castling_rights_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    let (mut ws, mut wl, mut bs, mut bl) = (false, false, false, false);
    if castling_rights_string != "-" {
        for c in castling_rights_string.chars() {
            match c {
                'K' => ws = true,
                'Q' => wl = true,
                'k' => bs = true,
                'q' => bl = true,
                _ => {
                    return Err(ForsythEdwardsNotationError::new(format!(
                        "Unexpected char '{c}' in castling rights string"
                    )))
                }
            }
        }
    }

    Ok(builder.set_castle_rights(ws, wl, bs, bl))
}

fn parse_en_passant_option_from_string(
    builder: ChessGameBuilder,
    en_passant_option_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    if en_passant_option_string == "-" {
        Ok(builder)
    } else {
        match game_board::get_column_and_row_from_square_name(en_passant_option_string) {
            Ok((col, row)) => {
                let pawn_color = if row < 3 { White } else { Black };
                let (original_row, new_row) = match pawn_color {
                    White => (row - 1, row + 1),
                    Black => (row + 1, row - 1),
                };

                let m = ChessMoveType::Move {
                    original_position: (col, original_row),
                    new_position: (col, new_row),
                    piece: ChessPiece::new(Pawn, pawn_color),
                    taken_piece: None,
                    promotion: None,
                };
                let moves = vec![m];
                Ok(builder.set_moves(moves))
            }
            Err(e) => Err(ForsythEdwardsNotationError::new(format!("unable to parse en passant square '{en_passant_option_string}' into a board position: {}", e)))
        }
    }
}

fn parse_half_turn_counter_from_string(
    builder: ChessGameBuilder,
    half_turn_counter_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    match half_turn_counter_string.parse() {
        Ok(half_turn) => Ok(builder.set_fifty_move_rule_counter(half_turn)),
        Err(_) => Err(ForsythEdwardsNotationError::new(format!(
            "Unable to parse '{half_turn_counter_string}' into unsigned int for half turn count"
        ))),
    }
}

fn parse_turn_number_from_string(
    builder: ChessGameBuilder,
    turn_number_string: &str,
) -> Result<ChessGameBuilder, ForsythEdwardsNotationError> {
    match turn_number_string.parse() {
        Ok(turn_number) => Ok(builder.set_turn_number(turn_number)),
        Err(_) => Err(ForsythEdwardsNotationError::new(format!(
            "unable to parse '{turn_number_string}' into unsigned int for turn count"
        ))),
    }
}

fn get_board_as_fen_string(game: &ChessGame) -> String {
    let board = game.get_board();

    let board_as_fen_string: String = (0..board.get_height())
        .rev()
        .map(|rank| encode_row(board, rank))
        .collect::<Vec<String>>()
        .join("/");
    board_as_fen_string
}

fn encode_row(board: &Board<ChessPiece>, row: usize) -> String {
    let mut result = String::new();

    let mut empty_space_counter: usize = 0;

    for col in 0..board.get_width() {
        if let Some(piece) = board.get_piece_at_space(col, row) {
            if empty_space_counter != 0 {
                result.push_str(&empty_space_counter.to_string());
                empty_space_counter = 0;
            }
            result.push(encode_piece_as_fen_char(piece));
        } else {
            empty_space_counter += 1;
        }
    }

    if empty_space_counter != 0 {
        result.push_str(&empty_space_counter.to_string());
    }
    result
}

fn encode_piece_as_fen_char(piece: &ChessPiece) -> char {
    match (piece.get_color(), piece.get_piece_type()) {
        (White, Pawn) => WHITE_PAWN,
        (Black, Pawn) => BLACK_PAWN,
        (White, Rook) => WHITE_ROOK,
        (Black, Rook) => BLACK_ROOK,
        (White, Knight) => WHITE_KNIGHT,
        (Black, Knight) => BLACK_KNIGHT,
        (White, Bishop) => WHITE_BISHOP,
        (Black, Bishop) => BLACK_BISHOP,
        (White, Queen) => WHITE_QUEEN,
        (Black, Queen) => BLACK_QUEEN,
        (White, King) => WHITE_KING,
        (Black, King) => BLACK_KING,
    }
}

fn get_current_turn_char(game: &ChessGame) -> char {
    match game.get_current_players_turn() {
        White => 'w',
        Black => 'b',
    }
}

fn get_castling_rights(game: &ChessGame) -> String {
    let mut result = String::new();

    let (wq, wk, bq, bk) = game.get_castling_rights();

    if wk {
        result.push('K');
    }
    if wq {
        result.push('Q');
    }
    if bk {
        result.push('k');
    }
    if bq {
        result.push('q');
    }
    if result.is_empty() {
        result.push('-')
    };

    result
}

fn get_en_passant(game: &ChessGame) -> String {
    if let Some(EnPassant {
        new_position: (col, row),
        ..
    }) = game.get_last_move()
    {
        game_board::get_square_name_from_row_and_col(*col, *row)
    } else {
        String::from("-")
    }
}

pub struct ForsythEdwardsNotationError {
    reason: String,
}

impl ForsythEdwardsNotationError {
    fn new(reason: String) -> Self {
        Self { reason }
    }
}

impl Display for ForsythEdwardsNotationError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Forsyth-Edwards Notation Error: {}", self.reason)
    }
}

impl Debug for ForsythEdwardsNotationError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Forsyth-EdwardsNotationError: {}", self.reason)
    }
}

impl Error for ForsythEdwardsNotationError {}

#[cfg(test)]
mod tests {

    mod decoding_tests {
        use super::super::*;

        #[test]
        fn create_new_game_from_string_and_verify_encoding_of_game() {
            let game =
                build_game_from_string("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
                    .unwrap();
            let fen_string = encode_game_as_string(&game);
            assert_eq!(
                "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
                fen_string
            );
        }
    }

    mod encoding_tests {
        use super::super::*;
        use crate::piece::PieceType::{Bishop, King, Knight, Pawn, Queen, Rook};

        #[test]
        fn building_game_from_empty_string() {
            let res = build_game_from_string("");
            match res {
                Ok(_) => {
                    panic!("expected error")
                }
                Err(e) => {
                    assert_eq!(
                        "argument must be a string in Forsyth–Edwards Notation",
                        e.reason
                    )
                }
            }
        }

        #[test]
        fn building_game_in_starting_position() {
            let starting_position_as_fen_string =
                "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
            let game = build_game_from_string(starting_position_as_fen_string);
            assert_eq!(game.is_ok(), true);
            let game = game.unwrap();

            let expected_piece_type = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook];
            let board = game.get_board();
            for (col, expected_type) in expected_piece_type.iter().enumerate() {
                assert_eq!(
                    board.get_piece_at_space(col, 0).unwrap(),
                    &ChessPiece::new(*expected_type, White)
                );
                assert_eq!(
                    board.get_piece_at_space(col, 1).unwrap(),
                    &ChessPiece::new(Pawn, White)
                );
                assert!(board.get_piece_at_space(col, 2).is_none());
                assert!(board.get_piece_at_space(col, 3).is_none());
                assert!(board.get_piece_at_space(col, 4).is_none());
                assert!(board.get_piece_at_space(col, 5).is_none());
                assert_eq!(
                    board.get_piece_at_space(col, 6).unwrap(),
                    &ChessPiece::new(Pawn, Black)
                );
                assert_eq!(
                    board.get_piece_at_space(col, 7).unwrap(),
                    &ChessPiece::new(*expected_type, Black)
                );
            }

            assert_eq!(White, game.get_current_players_turn());
            assert_eq!((true, true, true, true), game.get_castling_rights());
            assert_eq!(0, game.get_moves().len());
            assert_eq!(0, game.get_50_move_rule_counter());
            assert_eq!(1, game.get_turn_number());
        }

        #[test]
        fn parse_fen_starting_position_to_board() {
            let mut game_builder = ChessGameBuilder::new();

            game_builder = parse_board_from_string(
                game_builder,
                "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",
            )
            .unwrap();
            game_builder = game_builder.set_current_turn(White);

            let expected_piece_type = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook];

            let game = game_builder.build().unwrap();
            let board = game.get_board();
            for (col, expected_type) in expected_piece_type.iter().enumerate() {
                assert_eq!(
                    board.get_piece_at_space(col, 0).unwrap(),
                    &ChessPiece::new(*expected_type, White)
                );
                assert_eq!(
                    board.get_piece_at_space(col, 1).unwrap(),
                    &ChessPiece::new(Pawn, White)
                );
                assert!(board.get_piece_at_space(col, 2).is_none());
                assert!(board.get_piece_at_space(col, 3).is_none());
                assert!(board.get_piece_at_space(col, 4).is_none());
                assert!(board.get_piece_at_space(col, 5).is_none());
                assert_eq!(
                    board.get_piece_at_space(col, 6).unwrap(),
                    &ChessPiece::new(Pawn, Black)
                );
                assert_eq!(
                    board.get_piece_at_space(col, 7).unwrap(),
                    &ChessPiece::new(*expected_type, Black)
                );
            }
        }

        #[test]
        fn parse_fen_board_from_invalid_string() {
            let game_builder = ChessGameBuilder::new();

            let result =
                parse_board_from_string(game_builder, "rnbqkbnr/ppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR");
            match result {
                Ok(_) => panic!("expected error"),
                Err(e) => {
                    assert_eq!(
                        "File 'ppppppp' was not 8 spaces long in piece placement data",
                        e.reason
                    )
                }
            }

            let starting_position_as_fen_string_missing_pawn =
                "rnbqkbnr/fppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
            let game_builder = ChessGameBuilder::new();

            let result =
                parse_board_from_string(game_builder, starting_position_as_fen_string_missing_pawn);
            match result {
                Ok(_) => panic!("expected error"),
                Err(e) => {
                    assert_eq!(
                        "Unexpected char 'f' in file 'fppppppp' of piece placement data",
                        e.reason
                    )
                }
            }
        }

        #[test]
        fn parse_fen_current_turn_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = parse_current_turn_from_string(game_builder, "w").unwrap();
            game_builder = game_builder.set_board(Board::build(1, 1).unwrap());

            let game = game_builder.build().unwrap();
            assert_eq!(White, game.get_current_players_turn());
        }

        #[test]
        fn parse_fen_invalid_current_turn_string() {
            let game_builder = ChessGameBuilder::new();
            match parse_current_turn_from_string(game_builder, "J") {
                Ok(_) => panic!("expected error"),
                Err(e) => assert_eq!("encountered unexpected token parsing turn from FEN string, Expected 'w' or 'b', received J", e.reason),
            }
        }

        #[test]
        fn parse_fen_no_castling_rights_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(1, 1).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_castling_rights_from_string(game_builder, "-").unwrap();

            let game = game_builder.build().unwrap();
            assert_eq!((false, false, false, false), game.get_castling_rights());
        }

        #[test]
        fn parse_fen_all_castling_rights_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(1, 1).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_castling_rights_from_string(game_builder, "KQkq").unwrap();

            let game = game_builder.build().unwrap();
            assert_eq!((true, true, true, true), game.get_castling_rights());
        }

        #[test]
        fn parse_fen_invalid_castling_rights_string() {
            let game_builder = ChessGameBuilder::new();
            match parse_castling_rights_from_string(game_builder, "KQn") {
                Ok(_) => panic!("expected error"),
                Err(e) => assert_eq!("Unexpected char 'n' in castling rights string", e.reason),
            }
        }

        #[test]
        fn parse_fen_en_passant_string() {
            // White Pawn
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(8, 8).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_en_passant_option_from_string(game_builder, "e3").unwrap();

            let game = game_builder.build().unwrap();
            if let Some(ChessMoveType::Move {
                original_position,
                new_position,
                piece,
                taken_piece,
                promotion,
            }) = game.get_last_move()
            {
                assert_eq!(&(4, 1), original_position);
                assert_eq!(&(4, 3), new_position);
                assert_eq!(ChessPiece::new(Pawn, White), *piece);
                assert!(taken_piece.is_none());
                assert!(promotion.is_none());
            }

            // Black Pawn
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(8, 8).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_en_passant_option_from_string(game_builder, "e6").unwrap();

            let game = game_builder.build().unwrap();
            if let Some(ChessMoveType::Move {
                original_position,
                new_position,
                piece,
                taken_piece,
                promotion,
            }) = game.get_last_move()
            {
                assert_eq!(&(4, 6), original_position);
                assert_eq!(&(4, 4), new_position);
                assert_eq!(ChessPiece::new(Pawn, Black), *piece);
                assert!(taken_piece.is_none());
                assert!(promotion.is_none());
            }
        }

        #[test]
        fn parse_fen_no_en_passant_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(8, 8).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_en_passant_option_from_string(game_builder, "-").unwrap();

            let game = game_builder.build().unwrap();
            assert!(game.get_last_move().is_none());
        }

        #[test]
        fn parse_fen_invalid_en_passant_string() {
            let game_builder = ChessGameBuilder::new();
            match parse_en_passant_option_from_string(game_builder, "_") {
                Ok(_) => panic!("expected error"),
                Err(e) => assert_eq!(
                    "unable to parse en passant square '_' into a board position: Invalid input",
                    e.reason
                ),
            }
        }

        #[test]
        fn parse_fen_half_move_counter_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(8, 8).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_half_turn_counter_from_string(game_builder, "32").unwrap();

            let game = game_builder.build().unwrap();
            assert_eq!(32, game.get_50_move_rule_counter());
        }

        #[test]
        fn parse_fen_invalid_move_counter_string() {
            let game_builder = ChessGameBuilder::new();
            match parse_half_turn_counter_from_string(game_builder, "_") {
                Ok(_) => panic!("expected error"),
                Err(e) => assert_eq!(
                    "Unable to parse '_' into unsigned int for half turn count",
                    e.reason
                ),
            }
        }

        #[test]
        fn parse_fen_turn_counter_string() {
            let mut game_builder = ChessGameBuilder::new();
            game_builder = game_builder.set_board(Board::build(8, 8).unwrap());
            game_builder = game_builder.set_current_turn(White);
            game_builder = parse_turn_number_from_string(game_builder, "15").unwrap();

            let game = game_builder.build().unwrap();
            assert_eq!(15, game.get_turn_number());
        }

        #[test]
        fn parse_fen_invalid_turn_counter_string() {
            let game_builder = ChessGameBuilder::new();
            match parse_turn_number_from_string(game_builder, "ns") {
                Ok(_) => panic!("expected error"),
                Err(e) => assert_eq!(
                    "unable to parse 'ns' into unsigned int for turn count",
                    e.reason
                ),
            }
        }
    }
}