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
use super::piece_square_table::PieceSquareTable;
use bb::{BB, EMPTY, END_ROWS};
use castle::Castle;
use mv::*;
use mv_list::MoveList;
use mv_list::CHECK_MATE_SCORE;
use piece::*;
use side::Side;
use square;
use square::Square;
use std::fmt;

/// BestMoveTracker implements MoveList and keeps a track of the best move seen so far
#[derive(Clone)]
pub struct BestMoveTracker<'a> {
  mv: Move,
  score: i16,
  piece_square_table: &'a PieceSquareTable,
  piece_grid: &'a [Piece; 64],
  stm: Side,
  count: usize, // counts the number of moves considered
}

impl<'a> BestMoveTracker<'a> {
  pub fn new(
    piece_square_table: &'a PieceSquareTable,
    piece_grid: &'a [Piece; 64],
    stm: Side,
  ) -> BestMoveTracker<'a> {
    BestMoveTracker {
      mv: NULL_MOVE,
      score: -CHECK_MATE_SCORE,
      piece_grid: piece_grid,
      piece_square_table: piece_square_table,
      stm: stm,
      count: 0,
    }
  }

  pub fn best(&self) -> MoveScore {
    MoveScore::new(self.mv, self.score)
  }

  pub fn score(&self) -> i16 {
    self.score
  }

  pub fn mv(&self) -> Move {
    self.mv
  }

  pub fn count(&self) -> usize {
    self.count
  }

  fn insert(&mut self, mv: Move, score: i16) {
    self.count += 1;
    if score > self.score {
      self.score = score;
      self.mv = mv;
    }
  }
}
impl<'a> fmt::Display for BestMoveTracker<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{} ({})", self.mv, self.score)
  }
}

impl<'a> fmt::Debug for BestMoveTracker<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{} ({})", self.mv, self.score)
  }
}

impl<'a> MoveList for BestMoveTracker<'a> {
  fn add_captures(&mut self, from: Square, targets: BB) {
    if targets == EMPTY {
      return;
    }

    let stm = self.stm;

    let from_kind = unsafe { self.piece_grid.get_unchecked(from.to_usize()).kind() };
    let from_score = self
      .piece_square_table
      .score(from_kind, from.from_side(stm));

    for (to, _) in targets.iter() {
      let to_score = self.piece_square_table.score(from_kind, to.from_side(stm));

      let capture_kind = unsafe { self.piece_grid.get_unchecked(to.to_usize()).kind() };
      let capture_score = self
        .piece_square_table
        .score(capture_kind, to.from_side(stm.flip()));

      self.insert(
        Move::new_capture(from, to),
        -from_score + to_score + capture_score,
      );
    }
  }

  fn add_non_captures(&mut self, from: Square, targets: BB) {
    if targets == EMPTY {
      return;
    }

    let stm = self.stm;

    let from_kind = unsafe { self.piece_grid.get_unchecked(from.to_usize()).kind() };
    let from_score = self
      .piece_square_table
      .score(from_kind, from.from_side(stm));

    for (to, _) in targets.iter() {
      let to_score = self.piece_square_table.score(from_kind, to.from_side(stm));

      self.insert(Move::new_push(from, to), -from_score + to_score);
    }
  }

  fn add_castle(&mut self, castle: Castle) {
    let score = self.piece_square_table.castle_score(castle);
    self.insert(Move::new_castle(castle), score);
  }

  fn add_pawn_ep_capture(&mut self, from: Square, to: Square) {
    let stm = self.stm;
    let from_score = self.piece_square_table.score(PAWN, from.from_side(stm));
    let to_score = self.piece_square_table.score(PAWN, to.from_side(stm));
    // capture square is the file of the 'to' square
    // and the rank of the 'from' square
    let capture_sq = from.along_row_with_col(to);

    let capture_score = self
      .piece_square_table
      .score(PAWN, capture_sq.from_side(stm.flip()));

    let score = -from_score + to_score + capture_score;

    self.insert(Move::new_ep_capture(from, to), score);
  }

  fn add_pawn_pushes(&mut self, shift: usize, targets: BB) {
    let stm = self.stm;
    let from_kind = PAWN;

    for (to, _) in (targets & !END_ROWS).iter() {
      let from = to.rotate_right(shift as square::Internal);
      let from_score = self
        .piece_square_table
        .score(from_kind, from.from_side(stm));
      let to_score = self.piece_square_table.score(from_kind, to.from_side(stm));

      self.insert(Move::new_push(from, to), -from_score + to_score);
    }

    for (to, _) in (targets & END_ROWS).iter() {
      let from = to.rotate_right(shift as square::Internal);
      let from_score = self
        .piece_square_table
        .score(from_kind, from.from_side(stm));

      for to_kind in &[QUEEN, ROOK, BISHOP, KNIGHT] {
        let to_score = self.piece_square_table.score(*to_kind, to.from_side(stm));

        self.insert(
          Move::new_promotion(from, to, *to_kind),
          -from_score + to_score,
        );
      }
    }
  }

  fn add_pawn_captures(&mut self, shift: usize, targets: BB) {
    let stm = self.stm;
    let from_kind = PAWN;

    for (to, _) in (targets & !END_ROWS).iter() {
      let from = to.rotate_right(shift as square::Internal);
      let from_score = self
        .piece_square_table
        .score(from_kind, from.from_side(stm));
      let to_score = self.piece_square_table.score(from_kind, to.from_side(stm));

      let capture_kind = unsafe { self.piece_grid.get_unchecked(to.to_usize()).kind() };
      let capture_score = self
        .piece_square_table
        .score(capture_kind, to.from_side(stm.flip()));

      self.insert(
        Move::new_capture(from, to),
        -from_score + to_score + capture_score,
      );
    }

    for (to, _) in (targets & END_ROWS).iter() {
      let from = to.rotate_right(shift as square::Internal);
      let from_score = self
        .piece_square_table
        .score(from_kind, from.from_side(stm));

      let capture_kind = unsafe { self.piece_grid.get_unchecked(to.to_usize()).kind() };
      let capture_score = self
        .piece_square_table
        .score(capture_kind, to.from_side(stm.flip()));

      for to_kind in &[QUEEN, ROOK, BISHOP, KNIGHT] {
        let to_score = self.piece_square_table.score(*to_kind, to.from_side(stm));

        self.insert(
          Move::new_capture_promotion(from, to, *to_kind),
          -from_score + to_score + capture_score,
        );
      }
    }
  }
}