OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [shared.h] - Blame information for rev 13

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 sumanta.ch
#ifndef SHARED_H
2
#define SHARED_H
3 4 sumanta.ch
/*
4
 
5
connectk -- a program to play the connect-k family of games
6
Copyright (C) 2007 Michael Levin
7
 
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of the GNU General Public License
10
as published by the Free Software Foundation; either version 2
11
of the License, or (at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
 
22
*/
23
/* Some definitions in case glib is not included */
24
//#ifndef TRUE
25
#define TRUE 1
26
#define FALSE 0
27 7 sumanta.ch
//#define NULL ((void*)0)
28 4 sumanta.ch
//#endif
29
//#ifndef __G_TYPES_H__
30
typedef unsigned int gboolean;
31
#pragma bitsize gboolean 1
32
typedef int gsize;
33
//#endif
34 10 sumanta.ch
#include <limits.h>
35 7 sumanta.ch
#include "pico.h"
36 4 sumanta.ch
 
37
 
38
/*
39
 *      Options
40
 */
41
 
42
/* These are boolean options the user can toggle through the "Options" menu.
43
   Do not modify them directly as the "Options" menu will not reflect your
44
   changes. You can add more options in connectk.c */
45
extern int opt_pause_ai,        /* Pause AI to inspect the board */
46
//           opt_det_ai,          /* No randomness */
47
           opt_print_u,         /* Print utility after every move */
48
           opt_debug_dfsc,      /* Print out debug messages related to the DFS
49
                                   cache */
50
           opt_debug_thread,    /* Print messages related to thread and mutex function */
51
           opt_mark_log,        /* Take log of weights before marking */
52
           opt_mark_norm,       /* Normalize to the largest weight */
53
           opt_debug_stage,     /* Debug priority stages */
54
           opt_grayscale;       /* Use grayscale rendering for print outs */
55
 
56
/*
57
 *      Utility
58
 */
59
 
60
#ifdef _EFISTDARG_H_
61
char *nvav(int *plen, const char *fmt, va_list va);
62
#endif
63
char *nva(int *plen, const char *fmt, ...);
64
char *va(const char *fmt, ...);
65
/* The va family of functions simplify string processing by allowing
66
   printf-style substitution with any string-accepting function.
67
 
68
   For example:
69
     window_status(va("1 + 2 = %d", 1 + 2));
70
 
71
   nva provides additional functionality by outputting the length of the
72
   formatted string into the integer pointed to by plen. nvav accepts a variable
73
   argument indirectly. */
74
 
75
void window_status(const char *msg);
76
/* Sets the status bar text of the main window */
77
 
78
/*
79
 *      Allocation Chain
80
 */
81
 
82
typedef struct AllocChain {
83
        gboolean free;
84
        /* Is this object unallocated? */
85
 
86
        unsigned int id;
87
        /* Each object has a unique id */
88
 
89
        struct AllocChain *next;
90
        /* Next object in the chain */
91
} AllocChain;
92
 
93
typedef AllocChain *(*AllocFunc)(AllocChain *old_ac);
94
 
95
AllocChain *achain_new(AllocChain **root, AllocFunc af);
96
void achain_free(AllocChain *ac);
97
void achain_copy(const AllocChain *src, AllocChain *dest, gsize mem);
98
 
99
/*
100
 *      Game state
101
 */
102
 
103
/* We limit the maximum values of these variables; note that these are not
104
   arbitrary limits and should be modified with care */
105
#define MAX_BOARD_SIZE  59
106
#define MAX_CONNECT_K   12
107
#define MAX_PLACE_P     12
108
#define MAX_START_Q     6
109
#define MAX_DEPTH       9
110
#define MAX_BRANCH      32
111
 
112
 
113
#define board_size 19
114
#define board_stride 21
115
#define move_no 0
116
#define move_last 0
117
#define connect_k 6
118
#define place_p 2
119
#define start_q 1
120
#define opt_det_ai 1
121
//extern int board_size, board_stride, move_no, connect_k, place_p, start_q;
122
/* Board size (for all boards), moves in the game, connect_k to win, place_p
123
   moves at a time, black has start_q moves on the first move; do NOT modify
124
   these directly! */
125
 
126
enum {
127
        PIECE_ERROR = -1,
128
        /* Error pieces form a one tile deep border around the board */
129
 
130
        PIECE_NONE = 0,
131
        PIECE_BLACK,
132
        PIECE_WHITE,
133
        /* Empty and played pieces */
134
 
135
        PIECES,
136
        /* Total number of normal pieces (2) */
137
 
138
        PIECE_SEARCHED,
139
        PIECE_SEARCHED_MAX = PIECE_SEARCHED + MAX_DEPTH,
140
        /* Markers used by the search system */
141
 
142
        PIECE_THREAT0,
143
        PIECE_MARKER = PIECE_THREAT0,
144
        /* These threat markers are usable by the AIs */
145
};
146
typedef int PIECE;
147 13 sumanta.ch
#pragma bitsize PIECE 32
148 4 sumanta.ch
 
149
#define MAX_THREAT (INT_MAX - PIECE_THREAT0)
150
/* Highest value a threat marker can have */
151
 
152
#define PIECE_THREAT(n) (PIECE_THREAT0 + (n))
153
/* This marker represents a threat n-turns (of that player) away */
154
 
155
#define piece_empty(p) ((p) == PIECE_NONE || (p) >= PIECES)
156
/* Checks if a piece is an empty or a marker */
157
 
158
typedef unsigned int PLAYER;
159
/* Type for AIs, this is the index of the AI entry in ai.c */
160
 
161
typedef int BCOORD;
162
#pragma bitsize BCOORD 8
163
/* Type for board coordinates */
164
 
165
typedef struct Board {
166
        int  ac;
167
        /* Allocation chain must be the first member */
168
        #pragma bitsize ac 4
169
        unsigned int moves_left;
170
        /* How many moves the current player has left */
171
        #pragma bitsize  moves_left 8
172
 
173
        //struct Board *parent; //not synthesizable
174
        int parent;
175
        /* The board preceeding this one in history */
176
        #pragma bitsize  parent 4
177
 
178
        gboolean won;
179
        BCOORD win_x1, win_y1, win_x2, win_y2;
180
        /* On won boards, used to indicate where the winning line is */
181
 
182
        PIECE turn;
183
        /* Whose turn it is on this board */
184
 
185
        BCOORD move_x, move_y;
186
        /* The move to the next Board in history */
187
 
188
        PIECE data[board_stride][board_stride];
189
} Board;
190
/* The board structure represents the state of the game board. Do NOT preserve
191
   board pointers across games. */
192 13 sumanta.ch
typedef struct{
193
        unsigned int data[361];
194
                #pragma bitsize data 9
195
} index_array;
196 4 sumanta.ch
 
197
extern AllocChain *board_root;
198
extern gsize board_mem;
199
/* Variables for the allocation chain */
200
 
201
//extern Board board;
202
/* This is the current board. Do NOT modify it, that's cheating. :) */
203
const char *bcoords_to_string(BCOORD x, BCOORD y);
204
const char *bcoord_to_alpha(BCOORD c);
205
/* Return a static string representing a board coordinate pair */
206
 
207
void string_to_bcoords(const char *string, BCOORD *x, BCOORD *y);
208
/* Attempts to convert a string to board coordinates */
209
 
210
void new_game(Board *board,unsigned int size);
211
 
212
AllocChain *board_alloc(AllocChain *data);
213
#define board_new() ((Board*)achain_new(&board_root, board_alloc))
214
#define board_free(b) achain_free((AllocChain*)b)
215
/* Boards are dynamically allocated and must be free'd */
216
 
217
//#define board_copy(from, to) achain_copy((AllocChain*)from, (AllocChain*)to,\
218
                                         board_mem)
219
void board_copy(const Board *from,  Board *to);
220
/* Overwrite one board with another */
221
 
222
void board_clean(Board *b);
223
/* Remove all threat markers from a board */
224
 
225
int threat_count(const Board *b, PIECE player);
226
/* Gives the number of threats on a board for the current player */
227
 
228
Board *board_at(unsigned int move);
229
/* Returns a static pointer to a board in the history at move */
230
 
231
//int  count_pieces(const Board *b, BCOORD x, BCOORD y, PIECE type,
232
int  count_pieces(const Board *b, BCOORD x, BCOORD y, PIECE type,
233
                      int dx, int dy, PIECE *out);
234
/* Count consecutive pieces of type starting from (x, y) in the direction given
235
   by (dx, dy); includes (x, y) itself in the count and outputs the final
236
   piece to out if it is not NULL */
237
 
238
gboolean check_win_full(const Board *b, BCOORD x, BCOORD y,
239
                        BCOORD *x1, BCOORD *y1, BCOORD *x2, BCOORD *y2);
240
#define check_win(b, x, y) check_win_full(b, x, y, 0, 0, 0, 0)
241
/* Returns non-zero if placing a piece of type at (x, y) on the current board
242
   will result in a win for that player. The start and end coordinates of the
243
   winning line will be placed in (x1, y1) and (x2, y2). */
244
 
245
static inline PIECE piece_at(const Board *b, BCOORD x, BCOORD y)
246
{
247
        //return b->data[(y + 1) * board_stride + x + 1];
248
        return b->data[y+1][x+1];
249
}
250
/* Returns the piece at (x, y) on board b. If the coordinates are out of range,
251
   this function will return PIECE_ERROR. */
252
 
253
char piece_to_char(PIECE piece);
254
/* Returns a one character representation of a piece (e.g. 'W', 'B', etc) */
255
 
256
const char *piece_to_string(PIECE piece);
257
/* Returns a static string representation of a piece (e.g. "White" etc) */
258
 
259
static inline void place_piece_type(Board *b, BCOORD x, BCOORD y, PIECE type)
260
{
261
        //b->data[(y + 1) * board_stride + x + 1] = type;
262
        b->data[y+1][x+1]=type;
263
}
264
#define place_piece(b, x, y) place_piece_type(b, x, y, (b)->turn)
265
#define place_threat(b, x, y, n) place_piece_type(b, x, y, PIECE_THREAT(n))
266
/* Places a piece on board b, overwriting any piece that was previously in that
267
   place */
268
 
269
#define other_player(p) ((p) == PIECE_BLACK ? PIECE_WHITE : PIECE_BLACK)
270
/* Invert a player piece */
271
 
272
/*
273
 *      Move arrays
274
 */
275
 
276
/* Some guideline values for move weights: */
277
#define AIW_MAX         INT_MAX         /* largest weight value */
278
#define AIW_MIN         INT_MIN         /* smallest weight value */
279
#define AIW_WIN         AIW_MAX         /* this move wins the game */
280
#define AIW_DEFEND      (AIW_WIN - 2)   /* defends from an opponent win */
281
#define AIW_NONE        0               /* does nothing */
282
#define AIW_DRAW        AIW_NONE        /* draw game */
283
#define AIW_LOSE        (-AIW_WIN)      /* this move loses the game */
284
#define AIW_THREAT_MAX  262144          /* value of an immediate threat */
285
 
286
typedef int AIWEIGHT;
287
/* Type for AI move weights (utilities) */
288 12 sumanta.ch
#pragma bitsize AIWEIGHT 32
289 4 sumanta.ch
 
290
typedef struct {
291
        AIWEIGHT weight;
292
        BCOORD x, y;
293
} AIMove;
294
/* AIs return an array filled with these */
295
 
296
typedef struct AIMoves {
297
        int ac;
298
        /* Allocation chain must be the first member */
299
        #pragma bitsize ac 4
300
 
301
        unsigned int len;
302
        /* Number of members in data */
303
        #pragma bitsize len 8
304
 
305
        AIWEIGHT utility;
306
        /* A composite utility value set by some AIs when producing a moves
307
           list */
308
 
309
        AIMove data[361];
310
        /* Array of AIMove structures */
311
} AIMoves;
312
/* An array type for holding move lists */
313
 
314 7 sumanta.ch
 
315
typedef struct {
316
        int threat[2];
317
        PIECE turn[2];
318
} Line;
319
typedef struct{
320
        int data[MAX_CONNECT_K + 1][2];
321
}threat_count_array;
322
 
323
 
324
 
325
 
326
 
327
 
328
 
329
 
330 4 sumanta.ch
AllocChain *aimoves_alloc(AllocChain *data);
331
#define aimoves_new() ((AIMoves*)achain_new(&aimoves_root, aimoves_alloc))
332
//#define aimoves_free(m) achain_free((AllocChain*)(m))
333
static inline void aimoves_free(AIMoves *m) {
334
        m->len=0;
335
}
336
///////////* Move arrays are dynamically allocated and must be free'd */
337
//////////
338
//////////#define aimoves_copy(from, to) achain_copy((AllocChain*)(from),\
339
//////////                                           (AllocChain*)(to), aimoves_mem)
340
///////////* Overwrite one array with another */
341
//////////
342
//////////void aimoves_add(AIMoves *moves, const AIMove *aim);
343
///////////* Add an AIMove to an AIMoves array; move weights will be added to existing
344
//////////   weights */
345
//////////
346
void aimoves_append(AIMoves *moves, const AIMove *aim);
347
#define aimoves_set aimoves_append
348
///////////* Add an AIMove to an AIMoves array; existing moves weights will be
349
//////////   overwritten */
350
//////////
351 13 sumanta.ch
int aimoves_choose(AIMoves *moves, AIMove *move, index_array *index);
352 4 sumanta.ch
/* Will choose one of the best moves from a GArray of AIMove structures at
353
   random. Returns non-zero if a move was chosen or zero if a move could not
354
   be chosen for some reason. */
355
//////////
356
int aimoves_compare(const void *a, const void *b);
357
/* A comparison function for sorting move lists by weight */
358
//////////
359
//////////void aimoves_crop(AIMoves *moves, unsigned int n);
360
///////////* Reduce a moves list to the top-n by weight */
361
//////////
362
//////////void aimoves_concat(AIMoves *m1, const AIMoves *m2);
363
///////////* Concatenates m2 to m1 without checking for duplicates */
364
//////////
365
//////////AIMoves *aimoves_dup(const AIMoves *moves);
366
///////////* Duplicate a GArray of moves */
367
//////////
368
int aimoves_find(const AIMoves *moves, BCOORD x, BCOORD y);
369
///////////* Returns the index of (x, y) if it is in moves or -1 otherwise */
370
//////////
371
//////////void aimoves_range(AIMoves *moves, AIWEIGHT *min, AIWEIGHT *max);
372
///////////* Find the smallest and largest weight in the move array */
373
//////////
374
//////////void aimoves_merge(AIMoves *m1, const AIMoves *m2);
375
///////////* Merges m2 into m1, the highest weight is used for duplicates */
376
//////////
377
//////////void aimoves_print(const AIMoves *moves);
378
///////////* Prints out an array of moves */
379
//////////
380
//////////void aimoves_remove(AIMoves *moves, BCOORD x, BCOORD y);
381
///////////* Remove an AIMove from a GArray of AIMoves */
382
//////////
383
//////////void aimoves_remove_index_fast(AIMoves *moves, int i);
384
///////////* Remove a move from the list by overwriting it by the last move and
385
//////////   decrementing the length */
386
//////////
387
void aimoves_shuffle(AIMoves *moves,unsigned int current_random);
388
/* Shuffle a list of moves */
389
 
390
void aimoves_sort(AIMoves *moves);
391
/* Sort a list of moves by descending weight */
392
 
393
//////////void aimoves_subtract(AIMoves *m1, const AIMoves *m2);
394
///////////* Subtracts members of m2 from m1; O(n^2) */
395
//////////
396
extern AllocChain *aimoves_root;
397
//////////extern gsize aimoves_mem;
398
///////////* Allocation chain variables */
399
//////////
400
//////////const char *aiw_to_string(AIWEIGHT w);
401
///////////* Convert a weight to a string representation */
402
//////////
403
//////////char *aimove_to_string(const AIMove *move);
404
///////////* Convert a move to a string representation */
405
//////////
406
///////////*
407
////////// *      AI helper functions
408
////////// */
409
//////////
410 10 sumanta.ch
extern int ai_stop;
411 4 sumanta.ch
///////////* If this variable is non-zero, the system is trying to stop the AI thread
412
//////////   and your AI should exit. Do not set this yourself. */
413
//////////
414
//////////typedef AIMoves *(*AIFunc)(const Board *b);
415
///////////* AIs are defined as functions that output an unsorted, weighted list of board
416
//////////   coordinates for an arbitrary board. To create an AI in a file other than
417
//////////   ai.c, add a prototype of the function here and in ai.c. */
418
//////////
419
//////////AIMoves *enum_top_n(const Board *b, int n);
420
///////////* Returns an array containing the top n moves according to the utility
421
//////////   function */
422
//////////
423
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random);
424
/* Enumerate empty tiles at most dist away from some other piece on the board */
425
 
426 13 sumanta.ch
void streamsort(AIMoves *moves,index_array *index);
427 4 sumanta.ch
/*AIMoves **/void ai_marks(Board *b, PIECE min,AIMoves *moves);
428
/* Fills a moves list with tiles marked at least PIECE_THREAT(min) */
429
 
430
/*
431
 *      AI
432
 */
433
 
434
/* This table holds the information about all of the AIs in the program. Each
435
   has a short and long description. The short description will be used for
436
   the command line interface and the long description appears in the UI menu.
437
   Each AI has an associated AIFunc which outputs a move for the current
438
   board. */
439
///////////typedef struct AI {
440
///////////        char *s_desc, *l_desc;
441
///////////        AIFunc func;
442
///////////} AI;
443
///////////
444
///////////AIMoves *ai_sequences(const Board *b);
445
////////////* The square of the number of pieces in a window */
446
///////////
447
///////////AIMoves *ai_mesh(const Board *b);
448
////////////* The board as a mesh weighed down by the pieces */
449
///////////
450
///////////AIMoves *ai_monte_carlo(const Board *b);
451
////////////* Chooses the best move based on which one wins the most random games */
452
///////////
453
///////////AIMoves *ai_random(const Board *b);
454
////////////* Plays in a random tile */
455
///////////
456
/*AIMoves */ void ai_adjacent(Board *b,AIMove *move,unsigned int current_random);
457
/* Plays in a random tile adjacent to any piece on the board */
458
 
459
///////////AIMoves *ai_windows(const Board *b);
460
////////////* Plays in the best defensive position */
461
///////////
462
///////////AIMoves *ai_utility(const Board *b);
463
///////////AIMoves *ai_dfs_utility(const Board *b);
464
////////////* Utility function */
465
///////////
466 13 sumanta.ch
/*AIMoves **/int ai_threats(Board board[5][16],int depth,int branch,AIMoves moves[5][16],index_array *index);
467 4 sumanta.ch
AIMoves *ai_priority(const Board *b);
468
/* Multi-level threats */
469
 
470
 
471
 
472
void my_srandom(int seed,unsigned int *current_random);
473
int my_irand(int imax,unsigned int current_random);
474
//void backup_move(Board *board, AIMoves *moves,AIMove *move);
475 7 sumanta.ch
//AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound);
476
//int threat_window(int x, int y, int dx, int dy,
477
//                         PIECE *ptype, int *pdouble,Board *b);
478 4 sumanta.ch
int connect6ai_synth(int firstmove,char movein[8], char colour, char moveout[8]);
479 7 sumanta.ch
//extern "C" AIMove pico_stream_input_queue();
480
//extern "C" void pico_stream_output_queue(AIMove);
481
//extern "C" AIMove pico_ips_fifo_read_queue();
482
//extern "C" void pico_ips_fifo_write_queue(AIMove);
483
//extern int id;
484
//extern int ready; 
485 10 sumanta.ch
typedef struct {
486
        PLAYER ai;
487
        //SEARCH search;
488
        int depth, branch, cache, tss;
489
} Player;
490 13 sumanta.ch
/*AIMoves **/int search(Board *board,AIMove *move, Player *player);
491 7 sumanta.ch
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.