~ubuntu-branches/ubuntu/gutsy/grhino/gutsy

« back to all changes in this revision

Viewing changes to gameinfo.cc

  • Committer: Bazaar Package Importer
  • Author(s): Bart Martens
  • Date: 2007-04-02 20:38:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070402203809-fxyqqtkhj9ou03dy
Tags: upstream-0.16.0
ImportĀ upstreamĀ versionĀ 0.16.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        gameinfo.cc             Game States
 
3
        Copyright (c) 2004, 2005, 2006 Kriang Lerdsuwanakij
 
4
        email:          lerdsuwa@users.sourceforge.net
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
*/
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include "gameinfo.h"
 
24
#include "game.h"
 
25
 
 
26
game_info::game_info()
 
27
{
 
28
        board_ptr = 0;
 
29
        game_play = false;
 
30
        use_clock = false;
 
31
}
 
32
 
 
33
game_info::~game_info()
 
34
{
 
35
        delete board_ptr;
 
36
}
 
37
 
 
38
void game_info::new_game_remaining(int queue_size)
 
39
{
 
40
                        // player must be set before this
 
41
        set_game_play_from_board();
 
42
 
 
43
                                // Update history for undo
 
44
        board_history[0] = *board_ptr;
 
45
        player_history[0] = player;
 
46
        num_history = 1;
 
47
        max_num_history = 1;
 
48
                                // When move queue is used
 
49
        min_num_history = 1 + queue_size;
 
50
}
 
51
 
 
52
void game_info::set_game_play_from_board()
 
53
{
 
54
        game_play = board_ptr->can_play();
 
55
        if (!game_play)
 
56
                game_result = game_result_end;
 
57
}
 
58
 
 
59
void game_info::new_game_from_begin(int queue_size)
 
60
{
 
61
        random_game = false;
 
62
        first_play_is_pass = false;
 
63
 
 
64
        if (!board_ptr)
 
65
                board_ptr = new byte_board_info;
 
66
 
 
67
        *board_ptr = &board_begin;
 
68
        player = BLACK;
 
69
 
 
70
        new_game_remaining(queue_size);
 
71
}
 
72
 
 
73
void game_info::new_game_from_board(const byte_board_info *b, int p)
 
74
{
 
75
        random_game = true;
 
76
        random_game_pieces =  b->get_num_move() + 4;
 
77
        first_play_is_pass = false;
 
78
 
 
79
        if (!board_ptr)
 
80
                board_ptr = new byte_board_info;
 
81
 
 
82
        *board_ptr = *b;
 
83
        player = p;
 
84
 
 
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;
 
90
        }
 
91
 
 
92
        new_game_remaining(0);
 
93
}
 
94
 
 
95
bool game_info::is_undoable() const
 
96
{
 
97
        int     i = num_history;
 
98
        if (i <= min_num_history)       // Already at the beginning of the game
 
99
                                        // Use <= to support move queue
 
100
                return false;
 
101
 
 
102
        return true;
 
103
}
 
104
 
 
105
bool game_info::is_redoable() const
 
106
{
 
107
        int     i = num_history;
 
108
        if (i == max_num_history)       // Already at the end of the game
 
109
                return false;
 
110
 
 
111
        return true;
 
112
}
 
113
 
 
114
void game_info::undo()
 
115
{
 
116
        int     i = num_history;
 
117
 
 
118
                                // Skip AI's turns
 
119
        while (i > min_num_history && is_computer_player(player_history[i-2]))
 
120
                i--;
 
121
 
 
122
        if (i > min_num_history)                // Undo human move
 
123
                i--;
 
124
 
 
125
        *board_ptr = board_history[i-1];
 
126
 
 
127
        player = player_history[i-1];
 
128
                        // player must be set before this
 
129
        set_game_play_from_board();
 
130
 
 
131
        num_history = i;
 
132
}
 
133
 
 
134
void game_info::redo()
 
135
{
 
136
        int     i = num_history;
 
137
 
 
138
                                // Skip AI's turns
 
139
        while (i < max_num_history && is_computer_player(player_history[i-1]))
 
140
                i++;
 
141
 
 
142
        if (i < max_num_history)                // Redo human move
 
143
                i++;
 
144
 
 
145
        if (redo_ai_move) {
 
146
                                // Skip AI's turns
 
147
                while (i < max_num_history 
 
148
                       && is_computer_player(player_history[i-1]))
 
149
                        i++;
 
150
        }
 
151
 
 
152
        *board_ptr = board_history[i-1];
 
153
 
 
154
        player = player_history[i-1];
 
155
                        // player must be set before this
 
156
        set_game_play_from_board();
 
157
 
 
158
        num_history = i;
 
159
}
 
160
 
 
161
void game_info::place_piece(int pos, int time)
 
162
{
 
163
        if (use_clock &&
 
164
            ((player == BLACK && time > clock_history_black[num_history-1])
 
165
             || (player == WHITE && time > clock_history_white[num_history-1]))) {
 
166
                player_timeout();
 
167
                return;
 
168
        }
 
169
 
 
170
        board_ptr->place_piece(player, pos);
 
171
 
 
172
        int     old_player = player;
 
173
 
 
174
        if (board_ptr->can_play(switch_color(player))) {
 
175
                player = switch_color(player);
 
176
        }
 
177
        else if (!board_ptr->can_play(player)) {
 
178
                game_play = false;
 
179
                game_result = game_info::game_result_end;
 
180
        }
 
181
 
 
182
                                // Update history for undo
 
183
        bool keep_history = false;
 
184
        board_history[num_history] = *board_ptr;
 
185
                                // Repeat same move
 
186
        if (num_history < max_num_history 
 
187
            && move_history[num_history-1] == pos)
 
188
                keep_history = true;
 
189
 
 
190
        move_history[num_history-1] = pos;
 
191
        time_history[num_history-1] = time;
 
192
        if (use_clock) {
 
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];
 
196
                }
 
197
                else {
 
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;
 
200
                }
 
201
        }
 
202
 
 
203
                                // Next move
 
204
        player_history[num_history] = player;
 
205
        num_history++;
 
206
                                // Remove redo capability
 
207
        if (!keep_history)
 
208
                max_num_history = num_history;
 
209
}
 
210
 
 
211
void game_info::player_timeout()
 
212
{
 
213
        game_play = false;
 
214
        if (player == BLACK)
 
215
                game_result = game_result_timeout_black;
 
216
        else
 
217
                game_result = game_result_timeout_white;
 
218
}
 
219
 
 
220
void game_info::player_resign()
 
221
{
 
222
        game_play = false;
 
223
        if (player == BLACK)
 
224
                game_result = game_result_resign_black;
 
225
        else
 
226
                game_result = game_result_resign_white;
 
227
}
 
228
 
 
229
void game_info::game_end()
 
230
{
 
231
        game_play = false;
 
232
        game_result = game_result_end;
 
233
}
 
234
 
 
235
int game_info::get_clock()
 
236
{
 
237
        if (player == BLACK)
 
238
                return clock_history_black[num_history-1];
 
239
        else
 
240
                return clock_history_white[num_history-1];
 
241
}