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
28
extern int board_wid, board_heit;
30
// FIXME: this is ugly
31
/*extern void board_refresh_cell (int, int);
32
extern void board_set_cell (int, int, byte);
35
int movcpy (byte *dest, byte *src)
38
for (i=0; i%3 || src[i] != -1; i++)
44
gboolean movcmp_literal (byte *move1, byte *move2)
51
if (move1[i] == -1 && move2[i] == -1) return TRUE;
52
if (move1[i] != move2[i]) return FALSE;
56
byte *movlist_next (byte *move)
63
void move_apply (byte *board, byte *move)
67
for (i=0; move[3*i] != -1; i++)
69
x = move[3*i]; y = move[3*i+1];
70
board [y * board_wid + x] = move [3*i+2];
76
void move_fwrite (byte *move, FILE *fout)
81
fprintf (stderr, "move_fwrite got NULL arg\n");
84
/* write in human friendly format -- start row and col num from 1 not 0*/
85
for (i=0; move[3*i] != -1; i++)
86
fprintf (fout, "%d %d %d ", move[3*i] + 1,
87
move[3*i+1] + 1, move[3*i+2]);
92
void move_fwrite_ack (byte *move, FILE *fout)
94
fprintf (fout, "ACK ");
95
move_fwrite (move, fout);
98
void move_fwrite_nak (char *str, FILE *fout)
100
fprintf (fout, "NAK ");
102
fprintf (fout, "%s", str);
103
fprintf (fout, "\n");
107
byte *move_read (char *line)
109
static byte movbuf[1024];
115
movbuf[nc] = (byte) strtol (tmp, (char **) &new, 10);
121
assert (movbuf[nc] >= 1 && movbuf[nc] <= board_wid);
124
else if (nc % 3 == 1)
126
assert (movbuf[nc] >= 1 && movbuf[nc] <= board_heit);
131
assert (nc % 3 == 0);
136
static byte linebuf [4096];
138
byte *move_fread (FILE *fin)
140
fgets (linebuf, 4096, fin);
141
return move_read (linebuf);
144
byte *move_fread_ack (FILE *fin)
146
// FIXME: this is an ugly workaround for a bug which causes some junk data prefixing every move data.
149
fgets (linebuf, 4096, fin);
150
start = strstr (linebuf, "ACK");
151
if (!start) return NULL;
152
return move_read (start + 4);
155
fgets (linebuf, 4096, fin);
156
//printf ("%s\n", linebuf);
157
if (strncasecmp (linebuf, "ACK", 3))
159
return move_read (linebuf + 4);
163
char *line_read (FILE *fin)
165
fgets (linebuf, 4096, fin);
170
byte *movdup (byte *move)
174
for (len=0; move[3*len] != -1; len++)
176
newmov = (byte *) malloc (3 * len + 1);
177
memcpy (newmov, move, 3 * len + 1);
181
byte *mov_getinv (byte *board, byte *move)
184
byte *inv = movdup (move);
185
for (i=0; move[3*i] != -1; i++)
187
int x = move [3*i], y = move [3*i+1];
188
inv [3*i+2] = board [y * board_wid + x];