~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/move.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 "move.h"
 
20
#include "game.h"
 
21
 
 
22
#include <assert.h>
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <signal.h>
 
27
 
 
28
extern int board_wid, board_heit;
 
29
 
 
30
// FIXME: this is ugly
 
31
/*extern void board_refresh_cell (int, int);
 
32
extern void board_set_cell (int, int, byte);
 
33
*/
 
34
        
 
35
int movcpy (byte *dest, byte *src)
 
36
{
 
37
        int i;
 
38
        for (i=0; i%3 || src[i] != -1; i++)
 
39
                dest[i] = src[i];
 
40
        dest[i] = -1;
 
41
        return i;
 
42
}
 
43
 
 
44
gboolean movcmp_literal (byte *move1, byte *move2)
 
45
{
 
46
        int i;
 
47
        assert (move1);
 
48
        assert (move2);
 
49
        for (i=0; ; i++)
 
50
        {
 
51
                if (move1[i] == -1 && move2[i] == -1) return TRUE;
 
52
                if (move1[i] != move2[i]) return FALSE;
 
53
        }
 
54
}
 
55
 
 
56
byte *movlist_next (byte *move)
 
57
{
 
58
        while (*move != -1)
 
59
                move += 3;
 
60
        return move + 1;
 
61
}
 
62
 
 
63
void move_apply (byte *board, byte *move)
 
64
{
 
65
        int i, x, y;
 
66
        if (!move) return;
 
67
        for (i=0; move[3*i] != -1; i++)
 
68
        {
 
69
                x = move[3*i]; y = move[3*i+1];
 
70
                board [y * board_wid + x] = move [3*i+2];
 
71
        }
 
72
}
 
73
 
 
74
 
 
75
 
 
76
void move_fwrite (byte *move, FILE *fout)
 
77
{
 
78
        int i;
 
79
        if (!move)
 
80
        {
 
81
                fprintf (stderr, "move_fwrite got NULL arg\n");
 
82
                return;
 
83
        }
 
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]);
 
88
        fprintf (fout, "\n");   
 
89
        fflush (fout);
 
90
}
 
91
 
 
92
void move_fwrite_ack (byte *move, FILE *fout)
 
93
{
 
94
        fprintf (fout, "ACK ");
 
95
        move_fwrite (move, fout);
 
96
}
 
97
 
 
98
void move_fwrite_nak (char *str, FILE *fout)
 
99
{
 
100
        fprintf (fout, "NAK ");
 
101
        if (str)
 
102
                fprintf (fout, "%s", str);
 
103
        fprintf (fout, "\n");
 
104
        fflush (fout);
 
105
}
 
106
 
 
107
byte *move_read (char *line)
 
108
{
 
109
        static byte movbuf[1024];
 
110
        byte *new, *tmp;
 
111
        int nc = 0;
 
112
        tmp = line;
 
113
        while (1)
 
114
        {
 
115
                movbuf[nc] = (byte) strtol (tmp, (char **) &new, 10);
 
116
                if (new == tmp)
 
117
                        break;
 
118
                tmp = new;
 
119
                if (nc % 3 == 0)
 
120
                {
 
121
                        assert (movbuf[nc] >= 1 && movbuf[nc] <= board_wid);
 
122
                        movbuf[nc]--;
 
123
                }
 
124
                else if (nc % 3 == 1)
 
125
                {
 
126
                        assert (movbuf[nc] >= 1 && movbuf[nc] <= board_heit);
 
127
                        movbuf[nc]--;
 
128
                }
 
129
                nc++;
 
130
        }
 
131
        assert (nc % 3 == 0);
 
132
        movbuf[nc] = -1;
 
133
        return movbuf;
 
134
}
 
135
 
 
136
static byte linebuf [4096];
 
137
 
 
138
byte *move_fread (FILE *fin)
 
139
{
 
140
        fgets (linebuf, 4096, fin);
 
141
        return move_read (linebuf);
 
142
}
 
143
 
 
144
byte *move_fread_ack (FILE *fin)
 
145
{
 
146
        // FIXME: this is an ugly workaround for a bug which causes some junk data prefixing every move data.
 
147
        // Needs a real fix
 
148
        char *start;
 
149
        fgets (linebuf, 4096, fin);
 
150
        start = strstr (linebuf, "ACK");
 
151
        if (!start) return NULL;
 
152
        return move_read (start + 4);
 
153
 
 
154
/*      
 
155
        fgets (linebuf, 4096, fin);
 
156
        //printf ("%s\n", linebuf);
 
157
        if (strncasecmp (linebuf, "ACK", 3))
 
158
                return NULL;
 
159
        return move_read (linebuf + 4);
 
160
*/
 
161
}
 
162
 
 
163
char *line_read (FILE *fin)
 
164
{
 
165
        fgets (linebuf, 4096, fin);
 
166
        return linebuf;
 
167
}
 
168
 
 
169
 
 
170
byte *movdup (byte *move)
 
171
{
 
172
        byte *newmov;
 
173
        int len;
 
174
        for (len=0; move[3*len] != -1; len++)
 
175
                ;
 
176
        newmov = (byte *) malloc (3 * len + 1);
 
177
        memcpy (newmov, move, 3 * len + 1);
 
178
        return newmov;
 
179
}
 
180
 
 
181
byte *mov_getinv (byte *board, byte *move)
 
182
{
 
183
        int i;
 
184
        byte *inv = movdup (move);
 
185
        for (i=0; move[3*i] != -1; i++)
 
186
        {
 
187
                int x = move [3*i], y = move [3*i+1];
 
188
                inv [3*i+2] = board [y * board_wid + x];
 
189
        }
 
190
        return inv;
 
191
}
 
192
 
 
193