~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/rgb.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
 
 
24
#include "game.h"
 
25
#include "aaball.h"
 
26
 
 
27
#define RGB_CELL_SIZE 55
 
28
#define RGB_NUM_PIECES 3
 
29
 
 
30
#define RGB_BOARD_WID 3
 
31
#define RGB_BOARD_HEIT 3
 
32
 
 
33
#define RGB_RP 1
 
34
#define RGB_GP 2
 
35
#define RGB_BP 3
 
36
#define RGB_EMPTY 0
 
37
 
 
38
char rgb_colors[9] = {200, 200, 200, 200, 200, 200, 0, 0, 0};
 
39
 
 
40
int * rgb_init_pos = NULL;
 
41
 
 
42
void rgb_init ();
 
43
 
 
44
Game Rgb = { RGB_CELL_SIZE, RGB_BOARD_WID, RGB_BOARD_HEIT, 
 
45
        RGB_NUM_PIECES,
 
46
        rgb_colors,  NULL, NULL, "Rgb", "k-in-a-row", rgb_init};
 
47
 
 
48
 
 
49
static int rgb_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
 
50
static ResultType rgb_who_won (Pos *, Player, char **);
 
51
static void rgb_set_init_pos (Pos *pos);
 
52
void rgb_init (void);
 
53
static byte * rgb_movegen (Pos *);
 
54
static ResultType rgb_eval (Pos *, Player, float *eval);
 
55
 
 
56
static unsigned char * rgb_get_rgbmap (int idx, int color);
 
57
 
 
58
void rgb_init ()
 
59
{
 
60
        game_eval = rgb_eval;
 
61
        game_movegen = rgb_movegen;
 
62
        game_getmove = rgb_getmove;
 
63
        game_who_won = rgb_who_won;
 
64
        game_set_init_pos = rgb_set_init_pos;
 
65
        game_get_rgbmap = rgb_get_rgbmap;
 
66
        game_draw_cell_boundaries = TRUE;
 
67
        game_allow_flip = TRUE;
 
68
        game_doc_about_status = STATUS_COMPLETE;
 
69
        game_doc_about = 
 
70
                "Rgb\n"
 
71
                "Two player game\n"
 
72
                "Status: Fully implemented\n"
 
73
                "URL: "GAME_DEFAULT_URL ("rgb");
 
74
        game_doc_rules = 
 
75
                "Rgb, short for red-green-blue, is a harder version of tic-tac-toe. The goal is to get 3 balls in a line (row, column, or diagonal) of any one color. Clicking on an empty square puts a red ball on it, clicking on a red ball turns it green, and clicking on a green ball turns it blue.";
 
76
}
 
77
 
 
78
static void rgb_set_init_pos (Pos *pos)
 
79
{
 
80
        int i;
 
81
        for (i=0; i<board_wid * board_heit; i++)
 
82
                pos->board [i] = RGB_EMPTY;
 
83
}
 
84
 
 
85
static byte * rgb_movegen (Pos *pos)
 
86
{
 
87
        int i, j;
 
88
        byte movbuf [64];
 
89
        byte *movlist, *movp = movbuf;
 
90
        int lines[8][2] = 
 
91
        { 
 
92
                {0, 1}, {3, 1}, {6, 1},
 
93
                {0, 3}, {1, 3}, {2, 3},
 
94
                {0, 4}, {2, 2},
 
95
        };
 
96
        int val, found;
 
97
        for (i=0; i<8; i++)
 
98
        {
 
99
                val = -1; found = 1;
 
100
                for (j=0; j<3; j++)
 
101
                {
 
102
                        if (val >= 0 && pos->board[lines[i][0] + j * lines[i][1]] != val) 
 
103
                        { found = 0; break; }
 
104
                        val = pos->board[lines[i][0] + j * lines[i][1]];
 
105
                        if (val == RGB_EMPTY) { found = 0; break; }
 
106
                }
 
107
                if (found) 
 
108
                        break;
 
109
        }
 
110
        if (!found)
 
111
        {
 
112
                for (i=0; i<board_wid; i++)
 
113
                for (j=0; j<board_heit; j++)
 
114
                        if (pos->board[j * board_wid + i] != RGB_BP)
 
115
                        {
 
116
                                *movp++ = i;
 
117
                                *movp++ = j;
 
118
                                *movp++ = pos->board[j * board_wid + i] + 1;
 
119
                                *movp++ = -1;
 
120
                        }
 
121
        }
 
122
        *movp++ = -2;
 
123
        movlist = (byte *) (malloc (movp - movbuf));
 
124
        memcpy (movlist, movbuf, (movp - movbuf));
 
125
        return movlist;
 
126
}
 
127
 
 
128
static ResultType rgb_who_won (Pos *pos, Player to_play, char **commp)
 
129
{
 
130
        static char comment[32];
 
131
        char *who_str [] = { "White won", "Black won"};
 
132
        int lines[8][2] = 
 
133
        { 
 
134
                {0, 1}, {3, 1}, {6, 1},
 
135
                {0, 3}, {1, 3}, {2, 3},
 
136
                {0, 4}, {2, 2},
 
137
        };
 
138
        int i, j;
 
139
        int val, found;
 
140
        for (i=0; i<8; i++)
 
141
        {
 
142
                val = -1; found = 1;
 
143
                for (j=0; j<3; j++)
 
144
                {
 
145
                        if (val >= 0 && pos->board[lines[i][0] + j * lines[i][1]] != val) 
 
146
                        { found = 0; break; }
 
147
                        val = pos->board[lines[i][0] + j * lines[i][1]];
 
148
                        if (val == RGB_EMPTY) { found = 0; break; }
 
149
                }
 
150
                if (found) 
 
151
                {
 
152
                        *commp = who_str[to_play == WHITE ? 1 : 0];
 
153
                        return to_play == WHITE ? RESULT_BLACK : RESULT_WHITE;
 
154
                }
 
155
        }
 
156
        *commp = NULL;
 
157
        return RESULT_NOTYET;
 
158
}
 
159
 
 
160
static int rgb_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player to_play, byte **movp, int ** rmovep)
 
161
{
 
162
        int val;
 
163
        static byte move[4];
 
164
        if (type != GTKBOARD_BUTTON_RELEASE)
 
165
                return 0;
 
166
        val = pos->board [y * board_wid + x];
 
167
        if (val == RGB_BP) return -1;
 
168
        move[0] = x;
 
169
        move[1] = y;
 
170
        move[2] = val+1;
 
171
        move[3] = -1;
 
172
        if (movp)
 
173
                *movp = move;   
 
174
        return 1;
 
175
}
 
176
 
 
177
static unsigned char * rgb_get_rgbmap (int idx, int color)
 
178
{
 
179
        int fg = 0, bg, i;
 
180
        char *colors;
 
181
        static char rgbbuf[3 * RGB_CELL_SIZE * RGB_CELL_SIZE];
 
182
        colors = rgb_colors;
 
183
        if (idx == RGB_RP) fg = 255 << 16;
 
184
        else if (idx == RGB_GP) fg = 255 << 8;
 
185
        else if (idx == RGB_BP) fg = 255;
 
186
        else { return NULL;}
 
187
        for(i=0, bg=0;i<3;i++) 
 
188
        { int col = colors[i]; if (col<0) col += 256; bg += col * (1 << (16-8*i));}
 
189
        rgbmap_ball_shadow_gen(55, rgbbuf, fg, bg, 17.0, 35.0, 3);
 
190
        return rgbbuf;
 
191
}
 
192
 
 
193
 
 
194
static ResultType rgb_eval (Pos *pos, Player to_play, float *eval)
 
195
{
 
196
        int i, j;
 
197
        int lines[8][2] = 
 
198
        { 
 
199
                //{start, incr}
 
200
                // rows
 
201
                {0, 1}, {3, 1}, {6, 1},
 
202
                // cols
 
203
                {0, 3}, {1, 3}, {2, 3},
 
204
                // diagonals
 
205
                {0, 4}, {2, 2},
 
206
        };
 
207
        int val, found;
 
208
        for (i=0; i<8; i++)
 
209
        {
 
210
                val = -1; found = 1;
 
211
                for (j=0; j<3; j++)
 
212
                {
 
213
                        if (val >= 0 && pos->board[lines[i][0] + j * lines[i][1]] != val) 
 
214
                        { found = 0; break; }
 
215
                        val = pos->board[lines[i][0] + j * lines[i][1]];
 
216
                        if (val == RGB_EMPTY) { found = 0; break; }
 
217
                }
 
218
                if (found)
 
219
                {
 
220
                        *eval = (to_play == WHITE ? -2*GAME_EVAL_INFTY : 2*GAME_EVAL_INFTY);
 
221
                        return RESULT_NOTYET;
 
222
                }
 
223
        }
 
224
        *eval = 0;
 
225
        return RESULT_NOTYET;
 
226
}