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 7

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

powered by: WebSVN 2.1.0

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