~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-1/src/pentaline.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
 
 
20
 
/** \file pentaline.c */
21
 
 
22
 
#include <stdio.h>
23
 
#include <string.h>
24
 
#include <assert.h>
25
 
#include <stdlib.h>
26
 
#include <unistd.h>
27
 
 
28
 
#include "game.h"
29
 
#include "aaball.h"
30
 
 
31
 
#define PENTALINE_CELL_SIZE 40
32
 
#define PENTALINE_NUM_PIECES 2
33
 
 
34
 
#define PENTALINE_BOARD_WID 12
35
 
#define PENTALINE_BOARD_HEIT 12
36
 
 
37
 
#define PENTALINE_RP 1
38
 
#define PENTALINE_BP 2
39
 
#define PENTALINE_EMPTY 0
40
 
 
41
 
char pentaline_colors[9] = {200, 220, 200, 200, 220, 200, 0, 0, 0};
42
 
 
43
 
 
44
 
void pentaline_init ();
45
 
 
46
 
Game Pentaline = { PENTALINE_CELL_SIZE, PENTALINE_BOARD_WID, PENTALINE_BOARD_HEIT, 
47
 
        PENTALINE_NUM_PIECES,
48
 
        pentaline_colors,  NULL, NULL, "Pentaline", "k-in-a-row", pentaline_init};
49
 
 
50
 
 
51
 
static int pentaline_getmove (Pos *, int, int, GtkboardEventType, Player, byte **, int **);
52
 
static ResultType pentaline_who_won (Pos *, Player , char **);
53
 
static void pentaline_set_init_pos (Pos *pos);
54
 
unsigned char * pentaline_get_rgbmap (int idx, int color);
55
 
ResultType pentaline_eval_incr (Pos *, Player, byte *, float *);
56
 
byte * pentaline_movegen (Pos *);
57
 
ResultType pentaline_eval (Pos *, Player, float *);
58
 
void *pentaline_newstate (Pos *pos, byte *move);
59
 
 
60
 
typedef struct 
61
 
{
62
 
        // length, open/closed, white/black
63
 
        byte chains[5][2][2];
64
 
} Pentaline_state;
65
 
 
66
 
 
67
 
void pentaline_init ()
68
 
{
69
 
        game_eval = pentaline_eval;
70
 
        game_movegen = pentaline_movegen;
71
 
        game_getmove = pentaline_getmove;
72
 
        game_who_won = pentaline_who_won;
73
 
        game_get_rgbmap = pentaline_get_rgbmap;
74
 
        game_draw_cell_boundaries = TRUE;
75
 
//      game_eval_incr = pentaline_eval_incr;
76
 
        game_white_string = "Red";
77
 
        game_black_string = "Blue";
78
 
        game_stateful = TRUE;
79
 
        game_state_size = sizeof (Pentaline_state);
80
 
        game_newstate = pentaline_newstate;
81
 
        game_allow_flip = TRUE;
82
 
        game_doc_about_status = STATUS_COMPLETE;
83
 
        game_doc_about = 
84
 
                "Pentaline\n"
85
 
                "Two player game\n"
86
 
                "Status: Fully implemented (But AI needs improvement)\n"
87
 
                "URL: "GAME_DEFAULT_URL("pentaline");
88
 
        game_doc_rules = 
89
 
                "Two players take turns in placing balls of either color. The first to get 5 balls in a row wins.\n\n"
90
 
                "This game is the same as the free-style variant of GoMoku.\n";
91
 
}
92
 
 
93
 
byte * pentaline_movegen (Pos *pos)
94
 
{
95
 
        int i, j, k, l, x, y;
96
 
        byte movbuf [1024];
97
 
        byte *movlist, *movp = movbuf;
98
 
        int val, found = 0;
99
 
        int incx[4] = { 0, 1, 1, -1};
100
 
        int incy[4] = { 1, 0, 1,  1};
101
 
        int nbrx[] = { -1, -1, -1, 0, 0, 1, 1, 1};
102
 
        int nbry[] = { -1, 0, 1, -1, 1, -1, 0, 1};
103
 
        byte *board = pos->board;
104
 
        Player player = pos->player;
105
 
        for (i=0; i<board_wid; i++)
106
 
        for (j=0; j<board_heit; j++)
107
 
        {
108
 
                if (board [j * board_heit + i] == PENTALINE_EMPTY)
109
 
                        continue;
110
 
                for (k=0; k<4; k++)
111
 
                {
112
 
                        int found = 1, val;
113
 
                        for (l=0; l<5; l++)
114
 
                        {
115
 
                                if (j + l * incy[k] >= board_heit || i + l * incx[k] >= board_wid)
116
 
                                { found = 0; break; }
117
 
                                val = board [(j + l * incy[k]) * board_wid + i + l * incx[k]];
118
 
                                if (val == PENTALINE_EMPTY) {found = 0; break;}
119
 
                                if (val != board [j * board_wid + i]) { found = 0; break; }
120
 
                        }
121
 
                        if (found) { break; }
122
 
                }
123
 
        }
124
 
        if (!found)
125
 
        {
126
 
                for (i=0; i<board_wid; i++)
127
 
                for (j=0; j<board_heit; j++)
128
 
                {
129
 
                        if (board[j * board_wid + i] != PENTALINE_EMPTY) continue;
130
 
                        for (k=0; k<8; k++)
131
 
                        {
132
 
                                x = i + nbrx[k];
133
 
                                y = j + nbry[k];
134
 
                                if (x >= 0 && y >= 0 && x < board_wid && y < board_heit 
135
 
                                                && board [y * board_wid + x] != PENTALINE_EMPTY)
136
 
                                {
137
 
                                        *movp++ = i;
138
 
                                        *movp++ = j;
139
 
                                        *movp++ = (player == WHITE ? PENTALINE_RP : PENTALINE_BP);
140
 
                                        *movp++ = -1;
141
 
                                }
142
 
                        }
143
 
                }
144
 
        }
145
 
        if (movp == movbuf) // empty board
146
 
        {
147
 
                *movp++ = board_wid / 2 - random() % 2;
148
 
                *movp++ = board_heit / 2 - random() % 2;
149
 
                *movp++ = (player == WHITE ? PENTALINE_RP : PENTALINE_BP);
150
 
                *movp++ = -1;
151
 
        }
152
 
        *movp++ = -2;
153
 
        movlist = (byte *) (malloc (movp - movbuf));
154
 
        memcpy (movlist, movbuf, (movp - movbuf));
155
 
        return movlist;
156
 
}
157
 
 
158
 
ResultType pentaline_who_won (Pos *pos, Player to_play, char **commp)
159
 
{
160
 
        int i, j, k, l;
161
 
        int incx[4] = { 0, 1, 1, -1};
162
 
        int incy[4] = { 1, 0, 1,  1};
163
 
        for (i=0; i<board_wid; i++)
164
 
        for (j=0; j<board_heit; j++)
165
 
        for (k=0; k<4; k++)
166
 
        {
167
 
                int found = 1, val;
168
 
                for (l=0; l<5; l++)
169
 
                {
170
 
                        if (j + l * incy[k] >= board_heit || i + l * incx[k] >= board_wid)
171
 
                        { found = 0; break; }
172
 
                        val = pos->board [(j + l * incy[k]) * board_wid + i + l * incx[k]];
173
 
                        if (val == PENTALINE_EMPTY) {found = 0; break; }
174
 
                        if (val != pos->board [j * board_wid + i]) { found = 0; break; }
175
 
                }
176
 
                if (found) {*commp = (to_play == WHITE ? "Blue won" : "Red won");
177
 
                        return (to_play == WHITE ? RESULT_BLACK : RESULT_WHITE);}
178
 
        }
179
 
        *commp = NULL;
180
 
/*      {
181
 
                int len, open, color;
182
 
                for (color = 0; color < 2 && pos->state; color++)
183
 
                {
184
 
                        printf ("player = %s:   ", color==0?"Red ":"Blue");
185
 
                        for (open = 0; open < 2; open++)
186
 
                        {
187
 
                                printf ("open=%d: ", open+1);
188
 
                                for (len = 0; len < 5; len++)
189
 
                                printf ("%d ", 
190
 
                                                ((Pentaline_state *)pos->state)->chains[len][open][color]);
191
 
                                printf ("\t");
192
 
                        }
193
 
                        printf ("\n");
194
 
                }
195
 
                printf ("\n");
196
 
        }
197
 
*/
198
 
        return RESULT_NOTYET;
199
 
}
200
 
 
201
 
int pentaline_getmove (Pos *pos, int x, int y, GtkboardEventType type, Player to_play, byte **movp, int ** rmovep)
202
 
{
203
 
        int val;
204
 
        static byte move[4];
205
 
        if (type != GTKBOARD_BUTTON_RELEASE)
206
 
                return 0;
207
 
        if (pos->board [y * board_wid + x] != PENTALINE_EMPTY)
208
 
                return -1;
209
 
        move[0] = x;
210
 
        move[1] = y;
211
 
        move[2] = to_play == WHITE ? PENTALINE_RP : PENTALINE_BP;
212
 
        move[3] = -1;
213
 
        if (movp)
214
 
                *movp = move;   
215
 
        return 1;
216
 
}
217
 
 
218
 
unsigned char * pentaline_get_rgbmap (int idx, int color)
219
 
{
220
 
        int fg, bg, i;
221
 
        char *colors;
222
 
        static char rgbbuf[3 * PENTALINE_CELL_SIZE * PENTALINE_CELL_SIZE];
223
 
        colors = pentaline_colors;
224
 
        fg = (idx == PENTALINE_RP ? 0xee << 16 : 0xee);
225
 
        if (color == BLACK) colors += 3;
226
 
        for(i=0, bg=0;i<3;i++) 
227
 
        { int col = colors[i]; if (col<0) col += 256; bg += col * (1 << (16-8*i));}
228
 
        rgbmap_ball_shadow_gen(PENTALINE_CELL_SIZE, rgbbuf, fg, bg, 13.0, 30.0, 2);
229
 
        return rgbbuf;
230
 
}
231
 
 
232
 
static float eval_line (byte *board, int x, int y, int incx, int incy)
233
 
{
234
 
        int open = 0;
235
 
        int newx, newy;
236
 
        int len, val, sgn;
237
 
        newx = x - incx, newy = y - incy;
238
 
        val = board [y * board_wid + x];
239
 
        if (val == PENTALINE_EMPTY) return 0;
240
 
        sgn = (val == PENTALINE_RP ? 1 : -1);
241
 
        if (newx >= 0 && newy >= 0 && newx < board_wid && newy < board_heit)
242
 
        {
243
 
                if(board [newy * board_wid + newx] == val)
244
 
                        return 0;
245
 
                if(board [newy * board_wid + newx] == PENTALINE_EMPTY)
246
 
                        open = 1;
247
 
        }
248
 
        for (len = 0; ; x+= incx, y+=incy, len++)
249
 
        {
250
 
                if (x < 0 || y < 0 || x >= board_wid || y >= board_heit) break;
251
 
                if (board [y * board_wid + x] != val) break;
252
 
        }       
253
 
        if (!(x < 0 || y < 0 || x >= board_wid || y >= board_heit)
254
 
                        && board [y * board_wid + x] == PENTALINE_EMPTY) 
255
 
                open++;
256
 
        if (len >= 5) return GAME_EVAL_INFTY * sgn;
257
 
        return open * open * (1 << len) * sgn;
258
 
}
259
 
 
260
 
static float eval_line_bidir (byte *board, int x, int y, int incx, int incy)
261
 
{
262
 
        int val = board[y * board_wid + x];
263
 
        do
264
 
        {
265
 
                x -= incx;
266
 
                y -= incy;
267
 
        }
268
 
        while (x >= 0 && y >= 0 && x < board_wid && y < board_heit 
269
 
                        && board [y * board_wid + x] == val);
270
 
        x += incx;
271
 
        y += incy;
272
 
        return eval_line (board, x, y, incx, incy);
273
 
}
274
 
 
275
 
static float eval_runs (byte *board)
276
 
{
277
 
        int i, j, k;
278
 
        int incx[4] = { 0, 1, 1, -1 };
279
 
        int incy[4] = { 1, 0, 1,  1 };
280
 
        float eval = 0;
281
 
        for (i=0; i<board_wid; i++)
282
 
        for (j=0; j<board_heit; j++)
283
 
        {
284
 
                if (board [j * board_wid + i] == PENTALINE_EMPTY)
285
 
                        continue;
286
 
                for (k=0; k<4; k++)
287
 
                        eval += eval_line (board, i, j, incx[k], incy[k]);
288
 
        }
289
 
        return eval;
290
 
}
291
 
 
292
 
static int incx[4] = { 0, 1, 1, -1 };
293
 
static int incy[4] = { 1, 0, 1,  1 };
294
 
 
295
 
ResultType pentaline_eval_incr (Pos *pos, Player to_play, byte *move, float *eval)
296
 
{
297
 
        int  k;
298
 
        float val = 0;
299
 
        pos->board [move[1] * board_wid + move[0]] = move[2];
300
 
        for (k=0; k<4; k++)
301
 
                val += eval_line_bidir (pos->board, move[0], move[1], incx[k], incy[k]);
302
 
        pos->board [move[1] * board_wid + move[0]] = 0;
303
 
        *eval = val;
304
 
        return RESULT_NOTYET;
305
 
}
306
 
 
307
 
ResultType pentaline_eval (Pos *pos, Player player, float *eval)
308
 
{
309
 
#define FIRST_WON { *eval = player == WHITE ? (1 << 20) : - (1 << 20); return player == WHITE ? RESULT_WHITE : RESULT_BLACK; }
310
 
#define SECOND_WON { *eval = player == WHITE ? - (1 << 20) : (1 << 20); return player == WHITE ? RESULT_BLACK : RESULT_WHITE; }
311
 
        int color = player == WHITE ? 0 : 1;
312
 
        int len, open;
313
 
        Pentaline_state *state;
314
 
        *eval = 0;
315
 
        state = ((Pentaline_state *)pos->state);
316
 
        
317
 
        // 5 in a row
318
 
        if (state->chains[4][1][color] > 0 || state->chains[4][0][color] > 0)
319
 
        {
320
 
                *eval = player == WHITE ? (1 << 20) : - (1 << 20);
321
 
                return player == WHITE ? RESULT_WHITE : RESULT_BLACK;
322
 
        }
323
 
        
324
 
        // opponent: 5-in-a-row
325
 
        if (state->chains[4][1][1-color] > 0 || state->chains[4][0][1-color] > 0)
326
 
        {
327
 
                *eval = player == WHITE ? - (1 << 20) : (1 << 20);
328
 
                return player == WHITE ? RESULT_BLACK : RESULT_WHITE;
329
 
        }
330
 
        
331
 
        // 4-in-a-row
332
 
        if (state->chains[3][1][color] > 0 || state->chains[3][0][color] > 0)
333
 
        {
334
 
                *eval = player == WHITE ? (1 << 20) : - (1 << 20);
335
 
                return player == WHITE ? RESULT_WHITE : RESULT_BLACK;
336
 
        }
337
 
        
338
 
        // opponent: 4-in-a-row, both sides open
339
 
        if (state->chains[3][1][1-color] > 0)
340
 
                *eval += (player == WHITE ? -(1 << 18) : (1 << 18));
341
 
        
342
 
        // opponent: 2 4-in-a-row's
343
 
        if (state->chains[3][0][1-color] > 1)
344
 
                *eval += (player == WHITE ? -(1 << 18) : (1 << 18));
345
 
        
346
 
        // 3-in-a-row, both sides open; opponent doesn't have 4-in-a-row
347
 
        if (state->chains[2][1][color] > 0 && state->chains[3][0][1-color] == 0)
348
 
                *eval += (player == WHITE ? (1 << 16) : - (1 << 16));
349
 
        
350
 
        // opponent: 2 3-in-a-row's, both sides open 
351
 
        if (state->chains[2][1][1-color] > 1)
352
 
                *eval += (player == WHITE ? -(1 << 14) : (1 << 14));
353
 
        
354
 
        // opponent: a 4 and a doubly open 3
355
 
        if (state->chains[3][0][1-color] > 0 && state->chains[2][1][1-color] > 0)
356
 
                *eval += (player == WHITE ? - (1 << 12) : (1 << 12));
357
 
 
358
 
        // These seem to be all the winning patterns. Can't find any more.
359
 
        
360
 
        *eval = 0;
361
 
        for (len = 0; len < 4; len++)
362
 
        for (open = 0; open < 2; open++)
363
 
        {
364
 
                *eval += state->chains[len][open][0] * (1 + open) * (1 + open) * (1 << len);
365
 
                *eval -= state->chains[len][open][1] * (1 + open) * (1 + open) * (1 << len);
366
 
        }
367
 
        return RESULT_NOTYET;
368
 
}
369
 
 
370
 
 
371
 
// given a square and a direction, find the length of the chain it defines {0, 1, ... 4}  and the number of ends of the chain that are unoccupied {0, 1, 2}
372
 
static void get_chain_info (byte *board, int x, int y, int dx, int dy,
373
 
                int *len, int *open, int *color)
374
 
{
375
 
        int i;
376
 
        int val = board [y * board_wid + x];
377
 
        *open = 0;
378
 
        *len = 0;
379
 
        if (!ISINBOARD (x, y))
380
 
                return;
381
 
        if (val == PENTALINE_EMPTY)
382
 
                return;
383
 
        *color = (val == PENTALINE_RP ? 0 : 1);
384
 
                
385
 
        do
386
 
        {
387
 
                x -= dx;
388
 
                y -= dy;
389
 
        }
390
 
        while (ISINBOARD (x, y) && board [y * board_wid + x] == val);
391
 
        if (ISINBOARD (x, y) && board [y * board_wid + x] == PENTALINE_EMPTY) (*open)++;
392
 
        
393
 
        do
394
 
        {
395
 
                x += dx;
396
 
                y += dy;
397
 
                (*len)++;
398
 
        }
399
 
        while (ISINBOARD (x, y) && board [y * board_wid + x] == val);
400
 
        if (ISINBOARD (x, y) && board [y * board_wid + x] == PENTALINE_EMPTY) (*open)++;
401
 
        (*len)--;
402
 
}
403
 
 
404
 
static void update_state (byte chains[5][2][2], int len, int open, int color, int inc)
405
 
{
406
 
        if (len == 0) return;
407
 
        if (len >= 5)
408
 
        { 
409
 
                len = 5;
410
 
                open = 1;
411
 
        }
412
 
        if (open == 0) return;
413
 
        if (inc == -1) assert (chains[len-1][open-1][color] > 0);
414
 
        chains[len-1][open-1][color] += inc;
415
 
}
416
 
 
417
 
void *pentaline_newstate (Pos *pos, byte *move)
418
 
{
419
 
        int k=0;
420
 
        static Pentaline_state state;
421
 
        Pentaline_state def_state = 
422
 
                {{{{0, 0},{0, 0}},{{0, 0},{0, 0}},{{0, 0},{0, 0}},{{0, 0},{0, 0}},
423
 
        }};
424
 
        int len, open;
425
 
        int newcolor, oldcolor;
426
 
        int val = move[2];
427
 
        if (pos->state)
428
 
                memcpy (&state, pos->state, sizeof (Pentaline_state));
429
 
        else
430
 
                memcpy (&state, &def_state, sizeof (Pentaline_state));
431
 
        for (k=0; k<4; k++)
432
 
        {
433
 
                get_chain_info (pos->board, move[0] + incx[k], move[1] + incy[k], 
434
 
                                incx[k], incy[k], &len, &open, &oldcolor);
435
 
/*              if (len != 0 && len <= 5 && open != 0)
436
 
                {
437
 
                        if (len > 5) len = 5;
438
 
                        assert (state.chains[len-1][open-1][oldcolor] > 0);
439
 
                        state.chains[len-1][open-1][oldcolor]--;
440
 
                }
441
 
*/
442
 
                update_state (state.chains, len, open, oldcolor, -1);
443
 
                get_chain_info (pos->board, move[0] - incx[k], move[1] - incy[k], 
444
 
                                -incx[k], -incy[k], &len, &open, &oldcolor);
445
 
/*              if (len != 0 && len <= 5 && open != 0)
446
 
                {
447
 
                        if (len > 5) len = 5;
448
 
                        assert (state.chains[len-1][open-1][oldcolor] > 0);
449
 
                        state.chains[len-1][open-1][oldcolor]--;
450
 
                }
451
 
*/
452
 
                update_state (state.chains, len, open, oldcolor, -1);
453
 
        }
454
 
 
455
 
        pos->board [move[1] * board_wid + move[0]] = move[2]; 
456
 
        for (k=0; k<4; k++)
457
 
        {
458
 
                int x = move[0], y = move[1];
459
 
                if (ISINBOARD (x + incx[k], y + incy[k]) 
460
 
                                && pos->board [(y + incy[k]) * board_wid + (x + incx[k])] != val)
461
 
                {
462
 
                        get_chain_info (pos->board, move[0] + incx[k], move[1] + incy[k], 
463
 
                                        incx[k], incy[k], &len, &open, &oldcolor);
464
 
/*                      if (len != 0 && len <= 5 && open != 0)
465
 
                        {
466
 
                                if (len > 5) len = 5;
467
 
                                state.chains[len-1][open-1][oldcolor]++;
468
 
                        }
469
 
*/
470
 
                        update_state (state.chains, len, open, oldcolor, +1);
471
 
                }
472
 
                if (ISINBOARD (x - incx[k], y - incy[k]) 
473
 
                                && pos->board [(y - incy[k]) * board_wid + (x - incx[k])] != val)
474
 
                {
475
 
                        get_chain_info (pos->board, move[0] - incx[k], move[1] - incy[k], 
476
 
                                        -incx[k], -incy[k], &len, &open, &oldcolor);
477
 
/*                      if (len != 0 && len <= 5 && open != 0)
478
 
                        {
479
 
                                if (len > 5) len = 5;
480
 
                                state.chains[len-1][open-1][oldcolor]++;
481
 
                        }
482
 
*/
483
 
                        update_state (state.chains, len, open, oldcolor, +1);
484
 
                }
485
 
                get_chain_info (pos->board, move[0], move[1], 
486
 
                                incx[k], incy[k], &len, &open, &newcolor);
487
 
/*              if (len != 0 && len <= 5 && open != 0)
488
 
                {
489
 
                        if (len > 5) len = 5;
490
 
                        state.chains[len-1][open-1][newcolor]++;
491
 
                }
492
 
*/
493
 
                update_state (state.chains, len, open, newcolor, +1);
494
 
        }
495
 
        pos->board [move[1] * board_wid + move[0]] = 0; 
496
 
        return &state;
497
 
}