aboutsummaryrefslogtreecommitdiff
path: root/src/bitboard.rs
blob: a2d34cbc9e026768f553a4891e1ad695cd74a23e (plain)
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
use std::fmt;

pub struct Bitboard(pub u64);

impl fmt::Display for Bitboard {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for index in 0..64 {
            f.write_str(if &self.0 >> index & 1 == 1 { "1" } else { "." });
            if (index + 1) % 8 == 0 {
                f.write_str("\n");
            }
        }
        return write!(f, "\n")
    }
}

impl Bitboard {
    pub fn pop_count(&self) -> i32 {
        if self.0 == 0 {
            return 0;
        }
        return Bitboard::pop_count(&Bitboard(self.0 >> 1)) + (self.0 & 1) as i32;
    }
}

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

    #[test]
    fn test_print() {
        const bb: Bitboard = Bitboard(127);
        println!("{}", bb);
    }

    #[test]
    fn test_pop_count() {
        const bb: Bitboard = Bitboard(127);
        assert_eq!(bb.pop_count(), 7);
    }
}