This is the doxygen documentation for gtkboard.

.
Main Page   Data Structures   File List   Data Fields   Globals  

eightqueens.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 <assert.h>
00022 #include <stdlib.h>
00023 #include <time.h>
00024 
00025 #include "game.h"
00026 #include "../pixmaps/chess.xpm"
00027 #include "../pixmaps/misc.xpm"
00028 
00029 #define EIGHTQUEENS_CELL_SIZE 54
00030 #define EIGHTQUEENS_NUM_PIECES 2
00031 
00032 #define EIGHTQUEENS_BOARD_WID 8
00033 #define EIGHTQUEENS_BOARD_HEIT 8
00034 
00035 #define EIGHTQUEENS_EMPTY 0
00036 #define EIGHTQUEENS_QUEEN 1
00037 #define EIGHTQUEENS_CONTROLLED 2
00038 
00039 #define ABS(x) ((x) < 0 ? -(x) : (x))
00040 #define ATTACKS(i, j, x, y) ((i)==(x) || (j)==(y) || ABS((i)-(x)) == ABS((j)-(y)))
00041 
00042 char eightqueens_colors[6] = {200, 200, 160, 200, 200, 160};
00043 
00044 void eightqueens_init ();
00045 
00046 char ** eightqueens_pixmaps [] = 
00047 {
00048         chess_wq_54_xpm,
00049         grey_square_54_xpm,
00050 };
00051 
00052 Game Eightqueens = { EIGHTQUEENS_CELL_SIZE, 
00053         EIGHTQUEENS_BOARD_WID, EIGHTQUEENS_BOARD_HEIT, 
00054         EIGHTQUEENS_NUM_PIECES, 
00055         eightqueens_colors, NULL, eightqueens_pixmaps, "Eight queens puzzle", 
00056         eightqueens_init};
00057 
00058 SCORE_FIELD eightqueens_score_fields[] = {SCORE_FIELD_RANK, SCORE_FIELD_USER, SCORE_FIELD_TIME, SCORE_FIELD_DATE, SCORE_FIELD_NONE};
00059 char *eightqueens_score_field_names[] = {"Rank", "User", "Time", "Date", NULL};
00060 
00061 
00062 static int eightqueens_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
00063 static ResultType eightqueens_who_won (Pos *, Player, char **);
00064 
00065 void eightqueens_init ()
00066 {
00067         game_single_player = 1;
00068         game_getmove = eightqueens_getmove;
00069         game_who_won = eightqueens_who_won;
00070         game_scorecmp = game_scorecmp_def_time;
00071         game_score_fields =  eightqueens_score_fields;
00072         game_score_field_names = eightqueens_score_field_names;
00073         game_draw_cell_boundaries = TRUE;
00074         game_doc_about = 
00075                 "Eightqueens\n"
00076                 "Single player game\n"
00077                 "Status: Fully implemented\n"
00078                 "URL: "GAME_DEFAULT_URL("eightqueens");
00079         game_doc_rules = 
00080                 "Eightqueens rules\n\n"
00081                 "Place 8 non-attacking queens on the chessboard";
00082                 ;
00083 }
00084 
00085 ResultType eightqueens_who_won (Pos *pos, Player to_play, char **commp)
00086 {
00087         static char comment[32];
00088         int i, qcount;
00089         for (i=0, qcount = 0; i<board_wid*board_heit; i++)
00090                 qcount += (pos->board [i] == EIGHTQUEENS_QUEEN ? 1 : 0);
00091         snprintf (*commp = comment, 32, "Queens: %d", qcount);
00092         return qcount == 8 ? RESULT_WON : RESULT_NOTYET;
00093 }
00094 
00095 static int num_attacks (byte *board, int x, int y)
00096 {
00097         int i, j, count = 0;
00098         for (i=0; i<board_wid; i++)
00099         for (j=0; j<board_heit; j++)
00100                 if (!(i==x && j==y) && board [j * board_wid + i] == EIGHTQUEENS_QUEEN)
00101                          count += (ATTACKS (i, j, x, y) ? 1 : 0);
00102         return count;
00103 }
00104 
00105 int eightqueens_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player player, byte **movp, int ** rmovep)
00106 {
00107         static byte move[256];
00108         int i, j;
00109         byte *mp = move;
00110         *movp = move;
00111         if (type != GTKBOARD_BUTTON_RELEASE) return 0;
00112         if (pos->board[y * board_wid + x] == EIGHTQUEENS_CONTROLLED) return -1;
00113         if (pos->board[y * board_wid + x] == EIGHTQUEENS_QUEEN)
00114         {
00115                 *mp++ = x, *mp++ = y, *mp++ = EIGHTQUEENS_EMPTY;
00116                 for (i=0; i<board_wid; i++)
00117                 for (j=0; j<board_heit; j++)
00118                         if (pos->board[j * board_wid + i] == EIGHTQUEENS_CONTROLLED
00119                                         && ATTACKS(i, j, x, y)
00120                                         && num_attacks (pos->board, i, j) == 1)
00121                                 *mp++ = i, *mp++ = j, *mp++ = EIGHTQUEENS_EMPTY;
00122                 *mp++ = -1;
00123                 return 1;
00124         }
00125         *mp++ = x, *mp++ = y, *mp++ = EIGHTQUEENS_QUEEN;
00126         for (i=0; i<board_wid; i++)
00127         for (j=0; j<board_heit; j++)
00128                 if (!(i==x && j==y) && pos->board[j * board_wid + i] == EIGHTQUEENS_EMPTY 
00129                                 && ATTACKS (i, j, x, y))
00130                         *mp++ = i, *mp++ = j, *mp++ = EIGHTQUEENS_CONTROLLED;
00131         *mp++ = -1;
00132         return 1;
00133 }
00134