~ubuntu-branches/ubuntu/natty/gtkboard/natty

« back to all changes in this revision

Viewing changes to .pc/debian-changes-0.11pre0+cvs.2003.11.02-2/src/stack.c

  • Committer: Bazaar Package Importer
  • Author(s): Barak A. Pearlmutter
  • Date: 2011-02-28 11:25:02 UTC
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20110228112502-e9aah248wxelm7ao
Tags: 0.11pre0+cvs.2003.11.02-2
autotools tweaks, most notably -lSDL to supplement -lSDL_mixer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is a part of gtkboard, a board games system.
 
2
    Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
*/
 
19
#include "stack.h"
 
20
#include "game.h"
 
21
#include "move.h"
 
22
 
 
23
#include <assert.h>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <signal.h>
 
28
 
 
29
/** \file stack.c
 
30
   \brief implements a stack for navigating undoing and redoing moves
 
31
 
 
32
   stack is as follows:
 
33
   0 ---> movstack_ptr : "back" list;
 
34
   movstack_ptr ---> movstack_max : "forward" list
 
35
*/
 
36
 
 
37
//! Current position in the stack
 
38
static int movstack_ptr = 0;
 
39
 
 
40
//! Current size of the stack
 
41
static int movstack_max = 0;
 
42
 
 
43
//! Maximum size of stack
 
44
#define STACK_SIZE 4096
 
45
 
 
46
//! Array for moves
 
47
static byte *movstack[STACK_SIZE];
 
48
 
 
49
//! Array for move inverses. See mov_getinv().
 
50
static byte *movinvstack[STACK_SIZE];
 
51
 
 
52
void stack_free ()
 
53
{
 
54
        movstack_free ();
 
55
        statestack_free ();
 
56
}
 
57
 
 
58
void movstack_init ()
 
59
{
 
60
        // uh?
 
61
}
 
62
 
 
63
int movstack_get_num_moves()
 
64
{
 
65
        return movstack_ptr;
 
66
}
 
67
 
 
68
void movstack_push (byte *board, byte *move)
 
69
{
 
70
        assert (movstack_ptr < STACK_SIZE - 1);
 
71
        movstack[movstack_ptr] = movdup (move);
 
72
        movinvstack[movstack_ptr] = mov_getinv (board, move);
 
73
        movstack_ptr++;
 
74
        if (movstack_ptr > movstack_max)
 
75
                movstack_max = movstack_ptr;
 
76
}
 
77
 
 
78
byte *movstack_pop ()
 
79
{
 
80
        if (movstack_ptr == 0)
 
81
                return NULL;
 
82
        return movstack[--movstack_ptr];
 
83
}
 
84
 
 
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 ()
 
88
{
 
89
        int i;
 
90
        assert (movstack_ptr <= movstack_max);
 
91
        for (i = movstack_ptr; i < movstack_max; i++)
 
92
        {
 
93
                free (movstack[i]);
 
94
                free (movinvstack[i]);
 
95
        }
 
96
        movstack_max = movstack_ptr;
 
97
}
 
98
 
 
99
byte * movstack_forw ()
 
100
{
 
101
        if (movstack_ptr < movstack_max)
 
102
                movstack_ptr++;
 
103
        else return NULL;
 
104
        return movstack[movstack_ptr-1];
 
105
}
 
106
 
 
107
byte * movstack_back ()
 
108
{
 
109
        if (movstack_ptr > 0)
 
110
                movstack_ptr--;
 
111
        else return NULL;
 
112
        return movinvstack[movstack_ptr];
 
113
}
 
114
 
 
115
void movstack_free ()
 
116
{
 
117
        int i;
 
118
        for (i=0; i<movstack_max; i++)
 
119
        {
 
120
                free (movstack[i]);
 
121
                free (movinvstack[i]);
 
122
        }
 
123
        movstack_max = movstack_ptr = 0;
 
124
}
 
125
 
 
126
 
 
127
/*
 
128
   state stack
 
129
*/
 
130
 
 
131
static int statestack_ptr = 0, statestack_max = 0;
 
132
 
 
133
static void *statestack[STACK_SIZE];
 
134
 
 
135
// we create a new copy of state
 
136
void statestack_push (void *state)
 
137
{
 
138
        void *newstate;
 
139
        assert (statestack_ptr < STACK_SIZE - 1);
 
140
        newstate = malloc (game_state_size);
 
141
        assert (newstate);
 
142
        memcpy (newstate, state, game_state_size);
 
143
        statestack[statestack_ptr] = newstate;
 
144
        statestack_ptr++;
 
145
        if (statestack_ptr > statestack_max)
 
146
                statestack_max = statestack_ptr;
 
147
}
 
148
 
 
149
void *statestack_peek ()
 
150
{
 
151
        if (statestack_ptr == 0)
 
152
                return NULL;
 
153
        return statestack[statestack_ptr-1];
 
154
}
 
155
 
 
156
void *statestack_pop ()
 
157
{
 
158
        if (statestack_ptr == 0)
 
159
                return NULL;
 
160
        return statestack[--statestack_ptr];
 
161
}
 
162
 
 
163
void statestack_trunc ()
 
164
{
 
165
        int i;
 
166
        assert (statestack_ptr <= statestack_max);
 
167
        for (i = statestack_ptr; i < statestack_max; i++)
 
168
                free (statestack[i]);
 
169
        statestack_max = statestack_ptr;
 
170
}
 
171
 
 
172
void * statestack_forw ()
 
173
{
 
174
        if (statestack_ptr < statestack_max)
 
175
                statestack_ptr++;
 
176
        else return NULL;
 
177
        return statestack[statestack_ptr-1];
 
178
}
 
179
 
 
180
void * statestack_back ()
 
181
{
 
182
        if (statestack_ptr > 0)
 
183
                statestack_ptr--;
 
184
        else return NULL;
 
185
        return statestack[statestack_ptr-1];
 
186
}
 
187
 
 
188
void statestack_free ()
 
189
{
 
190
        int i;
 
191
        for (i=0; i<statestack_max; i++)
 
192
                free (statestack[i]);
 
193
        statestack_max = statestack_ptr = 0;
 
194
}