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
use crate::bitboard::*;
use crate::constants::*;
use crate::piece::*;
use crate::square::*;

/// State records the state of a chess game
#[derive(Clone)]
pub struct State {
    variant: Variant,
    rep: [Piece; BOARD_AREA],
    by_color: [[Bitboard; FIGURE_ARRAY_SIZE]; 2],
}

/// Variant type records the index of the variant
pub type Variant = usize;

/// VariantInfo records variant information
pub struct VariantInfo {
    pub start_fen: &'static str,
    pub display_name: &'static str,
}

/// State implementation
impl State {
    /// parses piece placement
    pub fn parse_piece_placement(fen: &str) -> [Piece; BOARD_AREA] {
        let mut rep = EMPTY_REP;
        let mut rank: Rank = 0;
        let mut file: File = 0;
        for i in 0..fen.len() {
            let c = &fen[i..i + 1];
            if c == " " {
                return rep;
            } else if c == "/" {
                file = 0;
                rank += 1;
            } else if c >= "1" && c <= "8" {
                for _ in 0..c.parse().expect("should not happen") {
                    rep[(LAST_RANK - rank) * NUM_FILES + file] = NO_PIECE;
                    file += 1;
                }
            } else {
                let p = fen_symbol_to_piece(c);
                if p != NO_PIECE {
                    rep[(LAST_RANK - rank) * NUM_FILES + file] = p;
                    file += 1;
                }
            }
        }
        rep
    }

    /// creates a new empty State
    pub fn new() -> State {
        State {
            variant: DEFAULT_VARIANT,
            rep: State::parse_piece_placement(VARIANT_INFOS[DEFAULT_VARIANT].start_fen),
            by_color: [EMTPY_FIGURE_BITBOARDS, EMTPY_FIGURE_BITBOARDS],
        }
    }

    /// sets the piece at a square
    pub fn set_piece_at_square(&mut self, sq: Square, p: Piece) {
        self.rep[sq] = p;
    }

    /// returns the piece at a square
    pub fn piece_at_square(&self, sq: Square) -> Piece {
        self.rep[sq]
    }

    /// returns the state as pretty printable string
    pub fn pretty_print_string(&self) -> String {
        let mut buff = "".to_string();
        for rank in 0..NUM_RANKS {
            for file in 0..NUM_FILES {
                let sq = rank_file(LAST_RANK - rank, file);
                let p = self.piece_at_square(sq);
                buff = format!("{}{:^3}", buff, p.fen_symbol());
                if file == LAST_FILE {
                    buff += "\n";
                }
            }
        }
        buff = format!(
            "{}\nvariant start fen : {}\n",
            buff,
            self.variant_start_fen()
        );
        buff
    }

    /// initialize from variant
    pub fn init_variant(&self) {}

    /// returns the start fen for the variant of the state
    pub fn variant_start_fen(&self) -> &str {
        VARIANT_INFOS[self.variant].start_fen
    }
}