This is the doxygen documentation for gtkboard.

.
Main Page   Data Structures   File List   Data Fields   Globals  

stack.c

Go to the documentation of this file.
00001 /*  This file is a part of gtkboard, a board games system.
00002     Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
00017 
00018 */
00019 #include "stack.h"
00020 #include "game.h"
00021 #include "move.h"
00022 
00023 #include <assert.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <signal.h>
00028 
00037 
00038 static int movstack_ptr = 0;
00039 
00041 static int movstack_max = 0;
00042 
00044 #define STACK_SIZE 4096
00045 
00047 static byte *movstack[STACK_SIZE];
00048 
00050 static byte *movinvstack[STACK_SIZE];
00051 
00052 void stack_free ()
00053 {
00054         movstack_free ();
00055         statestack_free ();
00056 }
00057 
00058 void movstack_init ()
00059 {
00060         // uh?
00061 }
00062 
00063 int movstack_get_num_moves()
00064 {
00065         return movstack_ptr;
00066 }
00067 
00068 void movstack_push (byte *board, byte *move)
00069 {
00070         assert (movstack_ptr < STACK_SIZE - 1);
00071         movstack[movstack_ptr] = movdup (move);
00072         movinvstack[movstack_ptr] = mov_getinv (board, move);
00073         movstack_ptr++;
00074         if (movstack_ptr > movstack_max)
00075                 movstack_max = movstack_ptr;
00076 }
00077 
00078 byte *movstack_pop ()
00079 {
00080         if (movstack_ptr == 0)
00081                 return NULL;
00082         return movstack[--movstack_ptr];
00083 }
00084 
00086 
00087 void movstack_trunc ()
00088 {
00089         int i;
00090         assert (movstack_ptr <= movstack_max);
00091         for (i = movstack_ptr; i < movstack_max; i++)
00092         {
00093                 free (movstack[i]);
00094                 free (movinvstack[i]);
00095         }
00096         movstack_max = movstack_ptr;
00097 }
00098 
00099 byte * movstack_forw ()
00100 {
00101         if (movstack_ptr < movstack_max)
00102                 movstack_ptr++;
00103         else return NULL;
00104         return movstack[movstack_ptr-1];
00105 }
00106 
00107 byte * movstack_back ()
00108 {
00109         if (movstack_ptr > 0)
00110                 movstack_ptr--;
00111         else return NULL;
00112         return movinvstack[movstack_ptr];
00113 }
00114 
00115 void movstack_free ()
00116 {
00117         int i;
00118         for (i=0; i<movstack_max; i++)
00119         {
00120                 free (movstack[i]);
00121                 free (movinvstack[i]);
00122         }
00123         movstack_max = movstack_ptr = 0;
00124 }
00125 
00126 
00127 /*
00128    state stack
00129 */
00130 
00131 static int statestack_ptr = 0, statestack_max = 0;
00132 
00133 static void *statestack[STACK_SIZE];
00134 
00135 // we create a new copy of state
00136 void statestack_push (void *state)
00137 {
00138         void *newstate;
00139         assert (statestack_ptr < STACK_SIZE - 1);
00140         newstate = malloc (game_state_size);
00141         assert (newstate);
00142         memcpy (newstate, state, game_state_size);
00143         statestack[statestack_ptr] = newstate;
00144         statestack_ptr++;
00145         if (statestack_ptr > statestack_max)
00146                 statestack_max = statestack_ptr;
00147 }
00148 
00149 void *statestack_peek ()
00150 {
00151         if (statestack_ptr == 0)
00152                 return NULL;
00153         return statestack[statestack_ptr-1];
00154 }
00155 
00156 void *statestack_pop ()
00157 {
00158         if (statestack_ptr == 0)
00159                 return NULL;
00160         return statestack[--statestack_ptr];
00161 }
00162 
00163 void statestack_trunc ()
00164 {
00165         int i;
00166         assert (statestack_ptr <= statestack_max);
00167         for (i = statestack_ptr; i < statestack_max; i++)
00168                 free (statestack[i]);
00169         statestack_max = statestack_ptr;
00170 }
00171 
00172 void * statestack_forw ()
00173 {
00174         if (statestack_ptr < statestack_max)
00175                 statestack_ptr++;
00176         else return NULL;
00177         return statestack[statestack_ptr-1];
00178 }
00179 
00180 void * statestack_back ()
00181 {
00182         if (statestack_ptr > 0)
00183                 statestack_ptr--;
00184         else return NULL;
00185         return statestack[statestack_ptr-1];
00186 }
00187 
00188 void statestack_free ()
00189 {
00190         int i;
00191         for (i=0; i<statestack_max; i++)
00192                 free (statestack[i]);
00193         statestack_max = statestack_ptr = 0;
00194 }