2
gameinfo.cc Game States
3
Copyright (c) 2004, 2005, 2006 Kriang Lerdsuwanakij
4
email: lerdsuwa@users.sourceforge.net
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
game_info::game_info()
33
game_info::~game_info()
38
void game_info::new_game_remaining(int queue_size)
40
// player must be set before this
41
set_game_play_from_board();
43
// Update history for undo
44
board_history[0] = *board_ptr;
45
player_history[0] = player;
48
// When move queue is used
49
min_num_history = 1 + queue_size;
52
void game_info::set_game_play_from_board()
54
game_play = board_ptr->can_play();
56
game_result = game_result_end;
59
void game_info::new_game_from_begin(int queue_size)
62
first_play_is_pass = false;
65
board_ptr = new byte_board_info;
67
*board_ptr = &board_begin;
70
new_game_remaining(queue_size);
73
void game_info::new_game_from_board(const byte_board_info *b, int p)
76
random_game_pieces = b->get_num_move() + 4;
77
first_play_is_pass = false;
80
board_ptr = new byte_board_info;
85
// First move is a pass
86
if (!board_ptr->can_play(player)
87
&& board_ptr->can_play(switch_color(player))) {
88
player = switch_color(player);
89
first_play_is_pass = true;
92
new_game_remaining(0);
95
bool game_info::is_undoable() const
98
if (i <= min_num_history) // Already at the beginning of the game
99
// Use <= to support move queue
105
bool game_info::is_redoable() const
108
if (i == max_num_history) // Already at the end of the game
114
void game_info::undo()
119
while (i > min_num_history && is_computer_player(player_history[i-2]))
122
if (i > min_num_history) // Undo human move
125
*board_ptr = board_history[i-1];
127
player = player_history[i-1];
128
// player must be set before this
129
set_game_play_from_board();
134
void game_info::redo()
139
while (i < max_num_history && is_computer_player(player_history[i-1]))
142
if (i < max_num_history) // Redo human move
147
while (i < max_num_history
148
&& is_computer_player(player_history[i-1]))
152
*board_ptr = board_history[i-1];
154
player = player_history[i-1];
155
// player must be set before this
156
set_game_play_from_board();
161
void game_info::place_piece(int pos, int time)
164
((player == BLACK && time > clock_history_black[num_history-1])
165
|| (player == WHITE && time > clock_history_white[num_history-1]))) {
170
board_ptr->place_piece(player, pos);
172
int old_player = player;
174
if (board_ptr->can_play(switch_color(player))) {
175
player = switch_color(player);
177
else if (!board_ptr->can_play(player)) {
179
game_result = game_info::game_result_end;
182
// Update history for undo
183
bool keep_history = false;
184
board_history[num_history] = *board_ptr;
186
if (num_history < max_num_history
187
&& move_history[num_history-1] == pos)
190
move_history[num_history-1] = pos;
191
time_history[num_history-1] = time;
193
if (old_player == BLACK) {
194
clock_history_black[num_history] = clock_history_black[num_history-1] - time;
195
clock_history_white[num_history] = clock_history_white[num_history-1];
198
clock_history_black[num_history] = clock_history_black[num_history-1];
199
clock_history_white[num_history] = clock_history_white[num_history-1] - time;
204
player_history[num_history] = player;
206
// Remove redo capability
208
max_num_history = num_history;
211
void game_info::player_timeout()
215
game_result = game_result_timeout_black;
217
game_result = game_result_timeout_white;
220
void game_info::player_resign()
224
game_result = game_result_resign_black;
226
game_result = game_result_resign_white;
229
void game_info::game_end()
232
game_result = game_result_end;
235
int game_info::get_clock()
238
return clock_history_black[num_history-1];
240
return clock_history_white[num_history-1];