Rename '<time.h>' to '<ctime>'
[purplehaze.git] / src / game.cpp
blob4f1f1300cd43a31bc6cc0c8b902179d559391b97
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 const 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 const 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 const int j = pieces.count(c, t); // Last piece's index
74 if (i != j && pieces.count(c, t) > 0) {
75 // Swap i and j and update board
76 Square s = pieces.position(c, t, j); // Last piece's position
77 pieces.set_position(c, t, i, s); // Fill the hole left
78 board[s] = Piece(c, t, i); // Update board
80 // Update Zobrist hash
81 Position& pos = current_position();
82 zobrist.update_piece(pos.hash(), c, t, emptied);
84 // Same hack here for the material hash than in add_piece()
85 zobrist.update_piece(pos.material_hash(), c, t, Square(j + 1));
86 zobrist.update_piece(pos.material_hash(), c, t, Square(j));
89 void Game::new_position()
91 // Save the state of the current position
92 tree.push();
94 // Remove the previous en passant square from the Zobrist hash
95 Position& pos = current_position();
96 zobrist.update_en_passant(pos.hash(), pos.en_passant());
98 // Prepare the position for a new move
99 pos.change_side();
100 zobrist.change_side(pos.hash());
103 void Game::del_position()
105 // Revert back to previous position
106 tree.pop();
110 * Killer move setter for the Killer Heuristic in move ordering.
112 void Game::set_killer_move(int depth, Move move)
114 if (move != killer_moves[depth][0]) {
115 killer_moves[depth][1] = killer_moves[depth][0];
116 killer_moves[depth][0] = move;