1
/* This file is a part of gtkboard, a board games system.
2
Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
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 2 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, write to the Free Software
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
30
\brief implements a stack for navigating undoing and redoing moves
33
0 ---> movstack_ptr : "back" list;
34
movstack_ptr ---> movstack_max : "forward" list
37
//! Current position in the stack
38
static int movstack_ptr = 0;
40
//! Current size of the stack
41
static int movstack_max = 0;
43
//! Maximum size of stack
44
#define STACK_SIZE 4096
47
static byte *movstack[STACK_SIZE];
49
//! Array for move inverses. See mov_getinv().
50
static byte *movinvstack[STACK_SIZE];
63
int movstack_get_num_moves()
68
void movstack_push (byte *board, byte *move)
70
assert (movstack_ptr < STACK_SIZE - 1);
71
movstack[movstack_ptr] = movdup (move);
72
movinvstack[movstack_ptr] = mov_getinv (board, move);
74
if (movstack_ptr > movstack_max)
75
movstack_max = movstack_ptr;
80
if (movstack_ptr == 0)
82
return movstack[--movstack_ptr];
85
//! Truncates a stack to the current poisition.
86
/** This will be called when the user makes a move when it is not the final poisition. */
87
void movstack_trunc ()
90
assert (movstack_ptr <= movstack_max);
91
for (i = movstack_ptr; i < movstack_max; i++)
94
free (movinvstack[i]);
96
movstack_max = movstack_ptr;
99
byte * movstack_forw ()
101
if (movstack_ptr < movstack_max)
104
return movstack[movstack_ptr-1];
107
byte * movstack_back ()
109
if (movstack_ptr > 0)
112
return movinvstack[movstack_ptr];
115
void movstack_free ()
118
for (i=0; i<movstack_max; i++)
121
free (movinvstack[i]);
123
movstack_max = movstack_ptr = 0;
131
static int statestack_ptr = 0, statestack_max = 0;
133
static void *statestack[STACK_SIZE];
135
// we create a new copy of state
136
void statestack_push (void *state)
139
assert (statestack_ptr < STACK_SIZE - 1);
140
newstate = malloc (game_state_size);
142
memcpy (newstate, state, game_state_size);
143
statestack[statestack_ptr] = newstate;
145
if (statestack_ptr > statestack_max)
146
statestack_max = statestack_ptr;
149
void *statestack_peek ()
151
if (statestack_ptr == 0)
153
return statestack[statestack_ptr-1];
156
void *statestack_pop ()
158
if (statestack_ptr == 0)
160
return statestack[--statestack_ptr];
163
void statestack_trunc ()
166
assert (statestack_ptr <= statestack_max);
167
for (i = statestack_ptr; i < statestack_max; i++)
168
free (statestack[i]);
169
statestack_max = statestack_ptr;
172
void * statestack_forw ()
174
if (statestack_ptr < statestack_max)
177
return statestack[statestack_ptr-1];
180
void * statestack_back ()
182
if (statestack_ptr > 0)
185
return statestack[statestack_ptr-1];
188
void statestack_free ()
191
for (i=0; i<statestack_max; i++)
192
free (statestack[i]);
193
statestack_max = statestack_ptr = 0;