This is the doxygen documentation for gtkboard.

.
Main Page   Data Structures   File List   Data Fields   Globals  

move.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 "move.h"
00020 #include "game.h"
00021 
00022 #include <assert.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <signal.h>
00027 
00028 extern int board_wid, board_heit;
00029 
00030 // FIXME: this is ugly
00031 /*extern void board_refresh_cell (int, int);
00032 extern void board_set_cell (int, int, byte);
00033 */
00034         
00035 int movcpy (byte *dest, byte *src)
00036 {
00037         int i;
00038         for (i=0; i%3 || src[i] != -1; i++)
00039                 dest[i] = src[i];
00040         dest[i] = -1;
00041         return i;
00042 }
00043 
00044 gboolean movcmp_literal (byte *move1, byte *move2)
00045 {
00046         int i;
00047         assert (move1);
00048         assert (move2);
00049         for (i=0; ; i++)
00050         {
00051                 if (move1[i] == -1 && move2[i] == -1) return TRUE;
00052                 if (move1[i] != move2[i]) return FALSE;
00053         }
00054 }
00055 
00056 byte *movlist_next (byte *move)
00057 {
00058         while (*move != -1)
00059                 move += 3;
00060         return move + 1;
00061 }
00062 
00063 void move_apply (byte *board, byte *move)
00064 {
00065         int i, x, y;
00066         if (!move) return;
00067         for (i=0; move[3*i] != -1; i++)
00068         {
00069                 x = move[3*i]; y = move[3*i+1];
00070                 board [y * board_wid + x] = move [3*i+2];
00071         }
00072 }
00073 
00074 
00075 
00076 void move_fwrite (byte *move, FILE *fout)
00077 {
00078         int i;
00079         if (!move)
00080         {
00081                 fprintf (stderr, "move_fwrite got NULL arg\n");
00082                 return;
00083         }
00084         /* write in human friendly format -- start row and col num from 1 not 0*/
00085         for (i=0; move[3*i] != -1; i++)
00086                 fprintf (fout, "%d %d %d ", move[3*i] + 1, 
00087                         move[3*i+1] + 1, move[3*i+2]);
00088         fprintf (fout, "\n");   
00089         fflush (fout);
00090 }
00091 
00092 void move_fwrite_ack (byte *move, FILE *fout)
00093 {
00094         fprintf (fout, "ACK ");
00095         move_fwrite (move, fout);
00096 }
00097 
00098 void move_fwrite_nak (char *str, FILE *fout)
00099 {
00100         fprintf (fout, "NAK ");
00101         if (str)
00102                 fprintf (fout, "%s", str);
00103         fprintf (fout, "\n");
00104         fflush (fout);
00105 }
00106 
00107 byte *move_read (char *line)
00108 {
00109         static byte movbuf[1024];
00110         byte *new, *tmp;
00111         int nc = 0;
00112         tmp = line;
00113         while (1)
00114         {
00115                 movbuf[nc] = (byte) strtol (tmp, (char **) &new, 10);
00116                 if (new == tmp)
00117                         break;
00118                 tmp = new;
00119                 if (nc % 3 == 0)
00120                 {
00121                         assert (movbuf[nc] >= 1 && movbuf[nc] <= board_wid);
00122                         movbuf[nc]--;
00123                 }
00124                 else if (nc % 3 == 1)
00125                 {
00126                         assert (movbuf[nc] >= 1 && movbuf[nc] <= board_heit);
00127                         movbuf[nc]--;
00128                 }
00129                 nc++;
00130         }
00131         assert (nc % 3 == 0);
00132         movbuf[nc] = -1;
00133         return movbuf;
00134 }
00135 
00136 static byte linebuf [4096];
00137 
00138 byte *move_fread (FILE *fin)
00139 {
00140         fgets (linebuf, 4096, fin);
00141         return move_read (linebuf);
00142 }
00143 
00144 byte *move_fread_ack (FILE *fin)
00145 {
00146         fgets (linebuf, 4096, fin);
00147         //printf ("%s\n", linebuf);
00148         if (strncasecmp (linebuf, "ACK", 3))
00149                 return NULL;
00150         return move_read (linebuf + 4);
00151 }
00152 
00153 char *line_read (FILE *fin)
00154 {
00155         fgets (linebuf, 4096, fin);
00156         return linebuf;
00157 }
00158 
00159 
00160 byte *movdup (byte *move)
00161 {
00162         byte *newmov;
00163         int len;
00164         for (len=0; move[3*len] != -1; len++)
00165                 ;
00166         newmov = (byte *) malloc (3 * len + 1);
00167         memcpy (newmov, move, 3 * len + 1);
00168         return newmov;
00169 }
00170 
00171 byte *mov_getinv (byte *board, byte *move)
00172 {
00173         int i;
00174         byte *inv = movdup (move);
00175         for (i=0; move[3*i] != -1; i++)
00176         {
00177                 int x = move [3*i], y = move [3*i+1];
00178                 inv [3*i+2] = board [y * board_wid + x];
00179         }
00180         return inv;
00181 }
00182 
00183