Introducing Lazy Move Generation
[purplehaze.git] / src / game.cpp
blobcb363f8df990e21857aaba14fb6905746294d1c1
1 /* PurpleHaze 2.0.0
2 * Copyright (C) 2007-2011 Vincent Ollivier
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <assert.h>
19 #include <iostream>
21 #include "game.h"
23 using namespace std;
25 Game::Game() {
26 nodes_count = 0;
27 output_thinking = false;
28 tt.clear();
30 // Initialize MVV/LVA score array
31 Moves::init_mvv_lva_scores();
34 void Game::add_piece(Color c, PieceType t, Square s) {
35 int i = pieces.get_nb_pieces(c, t);
36 pieces.set_position(c, t, i, s);
37 board.set_piece(Piece(c, t, i), s);
38 pieces.inc_nb_pieces(c, t);
40 // Update Zobrist hash
41 zobrist.update_piece(current_node().hash(), c, t, s);
44 void Game::del_piece(Color c, PieceType t, int i) {
45 // Remove the piece, and put in its place the higher index piece of the
46 // same color and type in order to avoid holes (idea from Mediocre Chess)
47 Square emptied = pieces.get_position(c, t, i); // Get piece's position
48 board.set_piece(Piece(), emptied); // Remove it from board
49 pieces.dec_nb_pieces(c, t); // and from pieces list
50 pieces.set_position(c, t, i, OUT); // TODO: not needed
51 int j = pieces.get_nb_pieces(c, t); // Last piece's index
52 if (pieces.get_nb_pieces(c, t) > 0 && i != j) {
53 // Swap i and j and update board
54 Square s = pieces.get_position(c, t, j); // Last piece's position
55 pieces.set_position(c, t, i, s); // Fill the hole left
56 pieces.set_position(c, t, j, OUT); // TODO: not needed
57 board.set_piece(Piece(c, t, i), s); // Update board
60 // Update Zobrist hash
61 zobrist.update_piece(current_node().hash(), c, t, emptied);
64 void Game::new_node() {
65 // Take a "snapshot" of the current position
66 tree.push();
68 // Remove the previous en passant square from the Zobrist hash
69 zobrist.update_en_passant(current_node().hash(),
70 current_node().get_en_passant());
72 // Update the position for a new move
73 current_node().inc_ply();
74 current_node().change_side();
75 zobrist.change_side(current_node().hash());
78 void Game::del_node() {
79 // Take back the previous "snapshot"
80 tree.pop();
84 * Killer move setter for the Killer Heuristic in move ordering.
86 void Game::set_killer_move(int depth, Move move) {
87 if (move != killer_moves[depth][0]) {
88 killer_moves[depth][1] = killer_moves[depth][0];
89 killer_moves[depth][0] = move;