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
26
#include "../pixmaps/chess.xpm"
27
#include "../pixmaps/misc.xpm"
29
#define EIGHTQUEENS_CELL_SIZE 54
30
#define EIGHTQUEENS_NUM_PIECES 2
32
#define EIGHTQUEENS_BOARD_WID 8
33
#define EIGHTQUEENS_BOARD_HEIT 8
35
#define EIGHTQUEENS_EMPTY 0
36
#define EIGHTQUEENS_QUEEN 1
37
#define EIGHTQUEENS_CONTROLLED 2
39
#define abs(x) ((x) < 0 ? -(x) : (x))
40
#define ATTACKS(i, j, x, y) ((i)==(x) || (j)==(y) || abs((i)-(x)) == abs((j)-(y)))
42
char eightqueens_colors[6] = {200, 200, 160, 200, 200, 160};
44
void eightqueens_init ();
46
char ** eightqueens_pixmaps [] =
52
Game Eightqueens = { EIGHTQUEENS_CELL_SIZE,
53
EIGHTQUEENS_BOARD_WID, EIGHTQUEENS_BOARD_HEIT,
54
EIGHTQUEENS_NUM_PIECES,
55
eightqueens_colors, NULL, eightqueens_pixmaps, "Eight Queens Puzzle",
59
SCORE_FIELD eightqueens_score_fields[] = {SCORE_FIELD_RANK, SCORE_FIELD_USER, SCORE_FIELD_TIME, SCORE_FIELD_DATE, SCORE_FIELD_NONE};
60
char *eightqueens_score_field_names[] = {"Rank", "User", "Time", "Date", NULL};
63
static int eightqueens_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
64
static ResultType eightqueens_who_won (Pos *, Player, char **);
66
void eightqueens_init ()
68
game_single_player = 1;
69
game_getmove = eightqueens_getmove;
70
game_who_won = eightqueens_who_won;
71
game_scorecmp = game_scorecmp_def_time;
72
game_score_fields = eightqueens_score_fields;
73
game_score_field_names = eightqueens_score_field_names;
74
game_draw_cell_boundaries = TRUE;
75
game_doc_about_status = STATUS_COMPLETE;
78
"Single player game\n"
79
"Status: Fully implemented\n"
80
"URL: "GAME_DEFAULT_URL("eightqueens");
82
"Place 8 non-attacking queens on the chessboard";
86
ResultType eightqueens_who_won (Pos *pos, Player to_play, char **commp)
88
static char comment[32];
90
for (i=0, qcount = 0; i<board_wid*board_heit; i++)
91
qcount += (pos->board [i] == EIGHTQUEENS_QUEEN ? 1 : 0);
92
snprintf (*commp = comment, 32, "Queens: %d", qcount);
93
return qcount == 8 ? RESULT_WON : RESULT_NOTYET;
96
static int num_attacks (byte *board, int x, int y)
99
for (i=0; i<board_wid; i++)
100
for (j=0; j<board_heit; j++)
101
if (!(i==x && j==y) && board [j * board_wid + i] == EIGHTQUEENS_QUEEN)
102
count += (ATTACKS (i, j, x, y) ? 1 : 0);
106
int eightqueens_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player player, byte **movp, int ** rmovep)
108
static byte move[256];
112
if (type != GTKBOARD_BUTTON_RELEASE) return 0;
113
if (pos->board[y * board_wid + x] == EIGHTQUEENS_CONTROLLED) return -1;
114
if (pos->board[y * board_wid + x] == EIGHTQUEENS_QUEEN)
116
*mp++ = x, *mp++ = y, *mp++ = EIGHTQUEENS_EMPTY;
117
for (i=0; i<board_wid; i++)
118
for (j=0; j<board_heit; j++)
119
if (pos->board[j * board_wid + i] == EIGHTQUEENS_CONTROLLED
120
&& ATTACKS(i, j, x, y)
121
&& num_attacks (pos->board, i, j) == 1)
122
*mp++ = i, *mp++ = j, *mp++ = EIGHTQUEENS_EMPTY;
126
*mp++ = x, *mp++ = y, *mp++ = EIGHTQUEENS_QUEEN;
127
for (i=0; i<board_wid; i++)
128
for (j=0; j<board_heit; j++)
129
if (!(i==x && j==y) && pos->board[j * board_wid + i] == EIGHTQUEENS_EMPTY
130
&& ATTACKS (i, j, x, y))
131
*mp++ = i, *mp++ = j, *mp++ = EIGHTQUEENS_CONTROLLED;