Improve HashTable initialization
[purplehaze.git] / src / game.cpp
blobce43987c907253f7263775511320b1ad40ad30fe
1 /* Copyright (C) 2007-2011 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 <assert.h>
18 #include <iostream>
20 #include "game.h"
22 Game::Game()
24 nodes_count = 0;
25 output_thinking = false;
26 //tt.clear();
27 //material_table.clear();
28 clear_killers();
29 search_moves.clear();
31 // Make PST
32 init_eval();
34 // Initialize MVV/LVA score array
35 Moves::init_mvv_lva_scores();
38 void Game::clear_killers()
40 for (int i = 0; i < MAX_PLY; ++i) {
41 for (int j = 0; j < MAX_KILLERS; ++j) {
42 killer_moves[i][j] = Move();
47 void Game::add_piece(Color c, PieceType t, Square s)
49 int i = pieces.count(c, t);
50 pieces.set_position(c, t, i, s);
51 board[s] = Piece(c, t, i);
52 pieces.inc_nb_pieces(c, t);
54 // Update Zobrist hash
55 Position& pos = current_position();
56 zobrist.update_piece(pos.hash(), c, t, s);
58 // Hack: for the material hash, the position is irrelevant but each piece
59 // needs a unique hash so we are using Square(i) in place of the position.
60 // Remove the previous total
61 zobrist.update_piece(pos.material_hash(), c, t, Square(i));
62 // Add the new total
63 zobrist.update_piece(pos.material_hash(), c, t, Square(i + 1));
66 void Game::del_piece(Color c, PieceType t, int i)
68 // Remove the piece, and put in its place the higher index piece of the
69 // same color and type in order to avoid holes (idea from Mediocre Chess)
70 Square emptied = pieces.position(c, t, i); // Get piece's position
71 board[emptied] = Piece(); // Remove it from board
72 pieces.dec_nb_pieces(c, t); // and from pieces list
73 pieces.set_position(c, t, i, OUT); // TODO: not needed
74 int j = pieces.count(c, t); // Last piece's index
75 if (pieces.count(c, t) > 0 && i != j) {
76 // Swap i and j and update board
77 Square s = pieces.position(c, t, j); // Last piece's position
78 pieces.set_position(c, t, i, s); // Fill the hole left
79 pieces.set_position(c, t, j, OUT); // TODO: not needed
80 board[s] = Piece(c, t, i); // Update board
82 // Update Zobrist hash
83 Position& pos = current_position();
84 zobrist.update_piece(pos.hash(), c, t, emptied);
86 // Same hack here for the material hash than in add_piece()
87 zobrist.update_piece(pos.material_hash(), c, t, Square(j + 1));
88 zobrist.update_piece(pos.material_hash(), c, t, Square(j));
91 void Game::new_position()
93 // Take a "snapshot" of the current position
94 tree.push();
96 // Remove the previous en passant square from the Zobrist hash
97 zobrist.update_en_passant(current_position().hash(),
98 current_position().en_passant());
100 // Update the position for a new move
101 current_position().change_side();
102 zobrist.change_side(current_position().hash());
105 void Game::del_position()
107 // Take back the previous "snapshot"
108 tree.pop();
112 * Killer move setter for the Killer Heuristic in move ordering.
114 void Game::set_killer_move(int depth, Move move)
116 if (move != killer_moves[depth][0]) {
117 killer_moves[depth][1] = killer_moves[depth][0];
118 killer_moves[depth][0] = move;