~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/eightqueens.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 <stdio.h>
 
20
#include <string.h>
 
21
#include <assert.h>
 
22
#include <stdlib.h>
 
23
#include <time.h>
 
24
 
 
25
#include "game.h"
 
26
#include "../pixmaps/chess.xpm"
 
27
#include "../pixmaps/misc.xpm"
 
28
 
 
29
#define EIGHTQUEENS_CELL_SIZE 54
 
30
#define EIGHTQUEENS_NUM_PIECES 2
 
31
 
 
32
#define EIGHTQUEENS_BOARD_WID 8
 
33
#define EIGHTQUEENS_BOARD_HEIT 8
 
34
 
 
35
#define EIGHTQUEENS_EMPTY 0
 
36
#define EIGHTQUEENS_QUEEN 1
 
37
#define EIGHTQUEENS_CONTROLLED 2
 
38
 
 
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)))
 
41
 
 
42
char eightqueens_colors[6] = {200, 200, 160, 200, 200, 160};
 
43
 
 
44
void eightqueens_init ();
 
45
 
 
46
char ** eightqueens_pixmaps [] = 
 
47
{
 
48
        chess_wq_54_xpm,
 
49
        grey_square_54_xpm,
 
50
};
 
51
 
 
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", 
 
56
        "Logic puzzles",
 
57
        eightqueens_init};
 
58
 
 
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};
 
61
 
 
62
 
 
63
static int eightqueens_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
 
64
static ResultType eightqueens_who_won (Pos *, Player, char **);
 
65
 
 
66
void eightqueens_init ()
 
67
{
 
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;
 
76
        game_doc_about = 
 
77
                "Eightqueens\n"
 
78
                "Single player game\n"
 
79
                "Status: Fully implemented\n"
 
80
                "URL: "GAME_DEFAULT_URL("eightqueens");
 
81
        game_doc_rules = 
 
82
                "Place 8 non-attacking queens on the chessboard";
 
83
                ;
 
84
}
 
85
 
 
86
ResultType eightqueens_who_won (Pos *pos, Player to_play, char **commp)
 
87
{
 
88
        static char comment[32];
 
89
        int i, qcount;
 
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;
 
94
}
 
95
 
 
96
static int num_attacks (byte *board, int x, int y)
 
97
{
 
98
        int i, j, count = 0;
 
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);
 
103
        return count;
 
104
}
 
105
 
 
106
int eightqueens_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player player, byte **movp, int ** rmovep)
 
107
{
 
108
        static byte move[256];
 
109
        int i, j;
 
110
        byte *mp = move;
 
111
        *movp = move;
 
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)
 
115
        {
 
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;
 
123
                *mp++ = -1;
 
124
                return 1;
 
125
        }
 
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;
 
132
        *mp++ = -1;
 
133
        return 1;
 
134
}
 
135