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
use bb::{BB, END_ROWS};
use castle::Castle;
use mv_list::MoveList;
use square::Square;
use std::ops;

/// MoveCounter implements MoveList and keeps a count of different types of moves added to it.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MoveCounter {
    pub moves: u64,
    pub captures: u64,
    pub castles: u64,
    pub promotions: u32,
    pub ep_captures: u32,
}

impl MoveCounter {
    pub fn new() -> MoveCounter {
        MoveCounter {
            moves: 0,
            captures: 0,
            castles: 0,
            promotions: 0,
            ep_captures: 0,
        }
    }
}

impl MoveList for MoveCounter {
    fn add_moves(&mut self, _: Square, targets: BB, enemy: BB) {
        self.moves += targets.pop_count() as u64;
        self.captures += (targets & enemy).pop_count() as u64;
    }

    fn add_castle(&mut self, _: Castle) {
        self.moves += 1;
        self.castles += 1;
    }

    fn add_pawn_ep_capture(&mut self, _: Square, _: Square) {
        self.moves += 1;
        self.captures += 1;
        self.ep_captures += 1;
    }

    fn add_pawn_pushes(&mut self, _: usize, targets: BB) {
        // non-promotions
        self.moves += (targets & !END_ROWS).pop_count() as u64;

        let promo_count = (targets & END_ROWS).pop_count() * 4;
        self.moves += promo_count as u64;
        self.promotions += promo_count;
    }

    fn add_pawn_captures(&mut self, _: usize, targets: BB) {
        // non-promotions
        let non_promo_count = (targets & !END_ROWS).pop_count();

        let promo_count = (targets & END_ROWS).pop_count() * 4;
        self.promotions += promo_count;

        let total = (promo_count + non_promo_count) as u64;
        self.moves += total;
        self.captures += total;
    }
}

impl ops::Add<MoveCounter> for MoveCounter {
    type Output = Self;

    fn add(self, other: MoveCounter) -> MoveCounter {
        MoveCounter {
            moves: self.moves + other.moves,
            captures: self.captures + other.captures,
            castles: self.castles + other.castles,
            promotions: self.promotions + other.promotions,
            ep_captures: self.ep_captures + other.ep_captures,
        }
    }
}

impl ops::AddAssign<MoveCounter> for MoveCounter {
    fn add_assign(&mut self, other: MoveCounter) {
        self.moves += other.moves;
        self.captures += other.captures;
        self.castles += other.castles;
        self.promotions += other.promotions;
        self.ep_captures += other.ep_captures;
    }
}

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

    #[test]
    fn test_move_counter() {
        let position = &Position::from_fen(STARTING_POSITION_FEN).unwrap();
        let mut counter = MoveCounter::new();

        legal_moves(&position, &mut counter);

        assert_eq!(counter.moves, 20);
    }
}