Merge branch 'release-2.1.0'
[purplehaze.git] / src / game.cpp
blob0683e8f39d10b622aa589b93fbbbd110013d8126
1 /* Copyright (C) 2007-2012 Vincent Ollivier
3 * Purple Haze is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Purple Haze is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <cassert>
18 #include <iostream>
20 #include "game.h"
22 void Game::clear_killers()
24 for (int i = 0; i < MAX_PLY; ++i) {
25 for (int j = 0; j < MAX_KILLERS; ++j) {
26 killer_moves[i][j] = Move();
31 void Game::add_piece(Color c, PieceType t, Square s)
33 const int i = pieces.count(c, t);
34 pieces.set_position(c, t, i, s);
35 board[s] = Piece(c, t, i);
36 pieces.inc_nb_pieces(c, t);
38 // Update Zobrist hash
39 Position& pos = current_position();
40 zobrist.update_piece(pos.hash(), c, t, s);
42 // Hack: for the material hash, the position is irrelevant but each piece
43 // needs a unique hash so we are using Square(i) in place of the position.
44 // Remove the previous total
45 zobrist.update_piece(pos.material_hash(), c, t, Square(i));
46 // Add the new total
47 zobrist.update_piece(pos.material_hash(), c, t, Square(i + 1));
50 void Game::del_piece(Color c, PieceType t, int i)
52 // Remove the piece, and put in its place the higher index piece of the
53 // same color and type in order to avoid holes (idea from Mediocre Chess)
54 const Square emptied = pieces.position(c, t, i); // Get piece's position
55 board[emptied] = Piece(); // Remove it from board
56 pieces.dec_nb_pieces(c, t); // and from pieces list
57 const int j = pieces.count(c, t); // Last piece's index
58 if (i != j && pieces.count(c, t) > 0) {
59 // Swap i and j and update board
60 Square s = pieces.position(c, t, j); // Last piece's position
61 pieces.set_position(c, t, i, s); // Fill the hole left
62 board[s] = Piece(c, t, i); // Update board
64 // Update Zobrist hash
65 Position& pos = current_position();
66 zobrist.update_piece(pos.hash(), c, t, emptied);
68 // Same hack here for the material hash than in add_piece()
69 zobrist.update_piece(pos.material_hash(), c, t, Square(j + 1));
70 zobrist.update_piece(pos.material_hash(), c, t, Square(j));
73 void Game::new_position()
75 // Save the state of the current position
76 tree.push();
78 // Remove the previous en passant square from the Zobrist hash
79 Position& pos = current_position();
80 zobrist.update_en_passant(pos.hash(), pos.en_passant());
82 // Prepare the position for a new move
83 pos.change_side();
84 zobrist.change_side(pos.hash());
87 void Game::del_position()
89 // Revert back to previous position
90 tree.pop();
94 * Killer move setter for the Killer Heuristic in move ordering.
96 void Game::set_killer(const Move move, const int depth)
98 if (move != killer_moves[depth][0]) {
99 killer_moves[depth][1] = killer_moves[depth][0];
100 killer_moves[depth][0] = move;