This is the doxygen documentation for gtkboard.

.
Main Page   Data Structures   File List   Data Fields   Globals  

infiltrate.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 <stdio.h>
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <math.h>
00023 
00024 #include "game.h"
00025 #include "aaball.h"
00026 
00027 #define INFILTRATE_CELL_SIZE 40
00028 #define INFILTRATE_NUM_PIECES 2
00029 
00030 #define INFILTRATE_BOARD_WID 7
00031 #define INFILTRATE_BOARD_HEIT 7
00032 
00033 #define INFILTRATE_EMPTY 0
00034 #define INFILTRATE_WP 1
00035 #define INFILTRATE_BP 2
00036 
00037 char infiltrate_colors[] = 
00038         {180, 180, 180, 
00039         200, 200, 200};
00040         
00041 int     infiltrate_init_pos[] = 
00042 {
00043          2 , 0 , 2 , 0 , 2 , 0 , 2 , 
00044          0 , 2 , 0 , 2 , 0 , 2 , 0 , 
00045          0 , 0 , 0 , 0 , 0 , 0 , 0 , 
00046          0 , 0 , 0 , 0 , 0 , 0 , 0 , 
00047          0 , 0 , 0 , 0 , 0 , 0 , 0 , 
00048          0 , 1 , 0 , 1 , 0 , 1 , 0 , 
00049          1 , 0 , 1 , 0 , 1 , 0 , 1 , 
00050 };
00051 
00052 static int infiltrate_max_moves = 200;
00053 
00054 
00055 void infiltrate_init ();
00056 int infiltrate_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
00057 //ResultType infiltrate_who_won (byte *, int, char **);
00058 byte *infiltrate_movegen (Pos *);
00059 ResultType infiltrate_eval (Pos *, Player, float *);
00060 char ** infiltrate_get_pixmap (int idx, int color);
00061 void infiltrate_reset_uistate ();
00062         
00063 Game Infiltrate = 
00064         { INFILTRATE_CELL_SIZE, INFILTRATE_BOARD_WID, INFILTRATE_BOARD_HEIT, 
00065         INFILTRATE_NUM_PIECES,
00066         infiltrate_colors, infiltrate_init_pos, NULL, "Infiltrate",
00067         infiltrate_init};
00068 
00069 void infiltrate_init ()
00070 {
00071         game_getmove = infiltrate_getmove;
00072         game_movegen = infiltrate_movegen;
00073         //game_who_won = infiltrate_who_won;
00074         game_eval = infiltrate_eval;
00075         game_get_pixmap = infiltrate_get_pixmap;
00076         game_reset_uistate = infiltrate_reset_uistate;
00077         game_doc_about = 
00078                 "Infiltrate\n"
00079                 "Two player game\n"
00080                 "Status: Partially implemented\n"
00081                 "URL: "GAME_DEFAULT_URL ("infiltrate");
00082         game_doc_rules = 
00083                 "Infiltrate rules\n"
00084                 "\n"
00085                 "The pieces move diagonally, one square at a time. The objective is to get all your pieces to the starting squares of your opponent's pieces.\n";
00086 }
00087 
00088 byte * infiltrate_movegen (Pos *pos)
00089 {
00090         int i, j, diffx, diffy;
00091         byte movbuf [256];
00092         byte *movlist, *mp = movbuf;
00093         byte *board = pos->board;
00094         Player player = pos->player;
00095         for (i=0; i<board_wid; i++)
00096         for (j=0; j<board_heit; j++)
00097         {
00098                 if (player == BLACK && (board [j * board_wid + i] != INFILTRATE_BP))
00099                         continue;
00100                 if (player == WHITE && (board [j * board_wid + i] != INFILTRATE_WP))
00101                         continue;
00102                 for (diffx = -1; diffx <= 1; diffx += 2)
00103                 for (diffy = -1; diffy <= 1; diffy += 2)
00104                 {
00105                         int x, y;
00106                         if (!ISINBOARD(x = i+diffx, y = j+diffy)) continue;
00107                         if (board [y * board_wid + x] != 0) continue;
00108                         *mp++ = i; *mp++ = j; *mp++ = 0;
00109                         *mp++ = i + diffx; *mp++ = j + diffy; 
00110                         *mp++ = (player == WHITE ? INFILTRATE_WP : INFILTRATE_BP);
00111                         *mp++ = -1;
00112                 }
00113         }
00114         if (mp == movbuf)
00115                 *mp++ = -1;
00116         *mp++ = -2;
00117         movlist = (byte *) (malloc (mp - movbuf));
00118         memcpy (movlist, movbuf, (mp - movbuf));
00119         return movlist;
00120 }
00121 
00122 ResultType infiltrate_eval (Pos *pos, Player to_play, float *eval)
00123 {
00124         float sum = 0;
00125         int i, j;
00126         for (i=0; i<board_wid; i++)
00127         for (j=0; j<board_heit; j++)
00128                 if (pos->board [j * board_wid + i])
00129                         sum += j;
00130         *eval = sum;
00131         return RESULT_NOTYET;
00132 
00133 }
00134 
00135 static int oldx = -1, oldy = -1;
00136 
00137 void infiltrate_reset_uistate ()
00138 {
00139         oldx = -1, oldy = -1;
00140 }
00141         
00142 int infiltrate_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player to_play, 
00143                 byte **movp, int ** rmovep)
00144 {
00145         static byte move[10];
00146         byte *mp = move;
00147         byte *board = pos->board;
00148         if (type != GTKBOARD_BUTTON_RELEASE) return 0;
00149         if (oldx < 0)
00150         {
00151                 int val = board [y * board_wid + x];
00152                 if ((val != INFILTRATE_BP && !(to_play == WHITE)) ||
00153                 (val != INFILTRATE_WP && !(to_play == BLACK)))
00154                         return -1;
00155                 oldx = x; oldy = y;
00156                 return 0;
00157         }
00158         else
00159         {
00160                 int diffx, diffy;
00161                 diffx = x - oldx;
00162                 diffy = y - oldy;
00163                 if (abs (diffx) != 1 || abs (diffy) != 1)
00164                 {
00165                         oldx = -1; oldy = -1;
00166                         return -1;
00167                 }
00168                 if (board [y * board_wid + x] != INFILTRATE_EMPTY)
00169                 {
00170                         oldx = -1; oldy = -1;
00171                         return -1;
00172                 }
00173                 *mp++ = oldx; *mp++ = oldy; *mp++ = 0;
00174                 *mp++ = x; *mp++ = y; *mp++ = board [oldy * board_wid + oldx];
00175                 *mp++ = -1;
00176                 *movp = move;
00177                 oldx = oldy = -1;
00178                 return 1;
00179 
00180         }
00181 }
00182 
00183 char ** infiltrate_get_pixmap (int idx, int color)
00184 {
00185         int bg;
00186         int i;
00187         static char pixbuf[INFILTRATE_CELL_SIZE * (INFILTRATE_CELL_SIZE+1)];
00188         for(i=0, bg=0;i<3;i++) 
00189         { int col = infiltrate_colors[i]; 
00190                 if (col<0) col += 256; bg += col * (1 << (16-8*i));}
00191         return pixmap_ball_gen (INFILTRATE_CELL_SIZE, pixbuf,
00192                         idx == INFILTRATE_WP ? 0xffffff : 0x0000ff, bg, 
00193                         8, 24);
00194 }