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 11

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
//#pragma bitsize PIECE 16
148
 
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
 
193
extern AllocChain *board_root;
194
extern gsize board_mem;
195
/* Variables for the allocation chain */
196
 
197
//extern Board board;
198
/* This is the current board. Do NOT modify it, that's cheating. :) */
199
const char *bcoords_to_string(BCOORD x, BCOORD y);
200
const char *bcoord_to_alpha(BCOORD c);
201
/* Return a static string representing a board coordinate pair */
202
 
203
void string_to_bcoords(const char *string, BCOORD *x, BCOORD *y);
204
/* Attempts to convert a string to board coordinates */
205
 
206
void new_game(Board *board,unsigned int size);
207
 
208
AllocChain *board_alloc(AllocChain *data);
209
#define board_new() ((Board*)achain_new(&board_root, board_alloc))
210
#define board_free(b) achain_free((AllocChain*)b)
211
/* Boards are dynamically allocated and must be free'd */
212
 
213
//#define board_copy(from, to) achain_copy((AllocChain*)from, (AllocChain*)to,\
214
                                         board_mem)
215
void board_copy(const Board *from,  Board *to);
216
/* Overwrite one board with another */
217
 
218
void board_clean(Board *b);
219
/* Remove all threat markers from a board */
220
 
221
int threat_count(const Board *b, PIECE player);
222
/* Gives the number of threats on a board for the current player */
223
 
224
Board *board_at(unsigned int move);
225
/* Returns a static pointer to a board in the history at move */
226
 
227
//int  count_pieces(const Board *b, BCOORD x, BCOORD y, PIECE type,
228
int  count_pieces(const Board *b, BCOORD x, BCOORD y, PIECE type,
229
                      int dx, int dy, PIECE *out);
230
/* Count consecutive pieces of type starting from (x, y) in the direction given
231
   by (dx, dy); includes (x, y) itself in the count and outputs the final
232
   piece to out if it is not NULL */
233
 
234
gboolean check_win_full(const Board *b, BCOORD x, BCOORD y,
235
                        BCOORD *x1, BCOORD *y1, BCOORD *x2, BCOORD *y2);
236
#define check_win(b, x, y) check_win_full(b, x, y, 0, 0, 0, 0)
237
/* Returns non-zero if placing a piece of type at (x, y) on the current board
238
   will result in a win for that player. The start and end coordinates of the
239
   winning line will be placed in (x1, y1) and (x2, y2). */
240
 
241
static inline PIECE piece_at(const Board *b, BCOORD x, BCOORD y)
242
{
243
        //return b->data[(y + 1) * board_stride + x + 1];
244
        return b->data[y+1][x+1];
245
}
246
/* Returns the piece at (x, y) on board b. If the coordinates are out of range,
247
   this function will return PIECE_ERROR. */
248
 
249
char piece_to_char(PIECE piece);
250
/* Returns a one character representation of a piece (e.g. 'W', 'B', etc) */
251
 
252
const char *piece_to_string(PIECE piece);
253
/* Returns a static string representation of a piece (e.g. "White" etc) */
254
 
255
static inline void place_piece_type(Board *b, BCOORD x, BCOORD y, PIECE type)
256
{
257
        //b->data[(y + 1) * board_stride + x + 1] = type;
258
        b->data[y+1][x+1]=type;
259
}
260
#define place_piece(b, x, y) place_piece_type(b, x, y, (b)->turn)
261
#define place_threat(b, x, y, n) place_piece_type(b, x, y, PIECE_THREAT(n))
262
/* Places a piece on board b, overwriting any piece that was previously in that
263
   place */
264
 
265
#define other_player(p) ((p) == PIECE_BLACK ? PIECE_WHITE : PIECE_BLACK)
266
/* Invert a player piece */
267
 
268
/*
269
 *      Move arrays
270
 */
271
 
272
/* Some guideline values for move weights: */
273
#define AIW_MAX         INT_MAX         /* largest weight value */
274
#define AIW_MIN         INT_MIN         /* smallest weight value */
275
#define AIW_WIN         AIW_MAX         /* this move wins the game */
276
#define AIW_DEFEND      (AIW_WIN - 2)   /* defends from an opponent win */
277
#define AIW_NONE        0               /* does nothing */
278
#define AIW_DRAW        AIW_NONE        /* draw game */
279
#define AIW_LOSE        (-AIW_WIN)      /* this move loses the game */
280
#define AIW_THREAT_MAX  262144          /* value of an immediate threat */
281
 
282
typedef int AIWEIGHT;
283
/* Type for AI move weights (utilities) */
284 11 sumanta.ch
#pragma bitsize AIWEIGHT 64
285 4 sumanta.ch
 
286
typedef struct {
287
        AIWEIGHT weight;
288
        BCOORD x, y;
289
} AIMove;
290
/* AIs return an array filled with these */
291
 
292
typedef struct AIMoves {
293
        int ac;
294
        /* Allocation chain must be the first member */
295
        #pragma bitsize ac 4
296
 
297
        unsigned int len;
298
        /* Number of members in data */
299
        #pragma bitsize len 8
300
 
301
        AIWEIGHT utility;
302
        /* A composite utility value set by some AIs when producing a moves
303
           list */
304
 
305
        AIMove data[361];
306
        /* Array of AIMove structures */
307
} AIMoves;
308
/* An array type for holding move lists */
309
 
310 7 sumanta.ch
 
311
typedef struct {
312
        int threat[2];
313
        PIECE turn[2];
314
} Line;
315
typedef struct{
316
        int data[MAX_CONNECT_K + 1][2];
317
}threat_count_array;
318
 
319
 
320
 
321
 
322
 
323
 
324
 
325
 
326 4 sumanta.ch
AllocChain *aimoves_alloc(AllocChain *data);
327
#define aimoves_new() ((AIMoves*)achain_new(&aimoves_root, aimoves_alloc))
328
//#define aimoves_free(m) achain_free((AllocChain*)(m))
329
static inline void aimoves_free(AIMoves *m) {
330
        m->len=0;
331
}
332
///////////* Move arrays are dynamically allocated and must be free'd */
333
//////////
334
//////////#define aimoves_copy(from, to) achain_copy((AllocChain*)(from),\
335
//////////                                           (AllocChain*)(to), aimoves_mem)
336
///////////* Overwrite one array with another */
337
//////////
338
//////////void aimoves_add(AIMoves *moves, const AIMove *aim);
339
///////////* Add an AIMove to an AIMoves array; move weights will be added to existing
340
//////////   weights */
341
//////////
342
void aimoves_append(AIMoves *moves, const AIMove *aim);
343
#define aimoves_set aimoves_append
344
///////////* Add an AIMove to an AIMoves array; existing moves weights will be
345
//////////   overwritten */
346
//////////
347 7 sumanta.ch
int aimoves_choose(AIMoves *moves, AIMove *move, unsigned int *index);
348 4 sumanta.ch
/* Will choose one of the best moves from a GArray of AIMove structures at
349
   random. Returns non-zero if a move was chosen or zero if a move could not
350
   be chosen for some reason. */
351
//////////
352
int aimoves_compare(const void *a, const void *b);
353
/* A comparison function for sorting move lists by weight */
354
//////////
355
//////////void aimoves_crop(AIMoves *moves, unsigned int n);
356
///////////* Reduce a moves list to the top-n by weight */
357
//////////
358
//////////void aimoves_concat(AIMoves *m1, const AIMoves *m2);
359
///////////* Concatenates m2 to m1 without checking for duplicates */
360
//////////
361
//////////AIMoves *aimoves_dup(const AIMoves *moves);
362
///////////* Duplicate a GArray of moves */
363
//////////
364
int aimoves_find(const AIMoves *moves, BCOORD x, BCOORD y);
365
///////////* Returns the index of (x, y) if it is in moves or -1 otherwise */
366
//////////
367
//////////void aimoves_range(AIMoves *moves, AIWEIGHT *min, AIWEIGHT *max);
368
///////////* Find the smallest and largest weight in the move array */
369
//////////
370
//////////void aimoves_merge(AIMoves *m1, const AIMoves *m2);
371
///////////* Merges m2 into m1, the highest weight is used for duplicates */
372
//////////
373
//////////void aimoves_print(const AIMoves *moves);
374
///////////* Prints out an array of moves */
375
//////////
376
//////////void aimoves_remove(AIMoves *moves, BCOORD x, BCOORD y);
377
///////////* Remove an AIMove from a GArray of AIMoves */
378
//////////
379
//////////void aimoves_remove_index_fast(AIMoves *moves, int i);
380
///////////* Remove a move from the list by overwriting it by the last move and
381
//////////   decrementing the length */
382
//////////
383
void aimoves_shuffle(AIMoves *moves,unsigned int current_random);
384
/* Shuffle a list of moves */
385
 
386
void aimoves_sort(AIMoves *moves);
387
/* Sort a list of moves by descending weight */
388
 
389
//////////void aimoves_subtract(AIMoves *m1, const AIMoves *m2);
390
///////////* Subtracts members of m2 from m1; O(n^2) */
391
//////////
392
extern AllocChain *aimoves_root;
393
//////////extern gsize aimoves_mem;
394
///////////* Allocation chain variables */
395
//////////
396
//////////const char *aiw_to_string(AIWEIGHT w);
397
///////////* Convert a weight to a string representation */
398
//////////
399
//////////char *aimove_to_string(const AIMove *move);
400
///////////* Convert a move to a string representation */
401
//////////
402
///////////*
403
////////// *      AI helper functions
404
////////// */
405
//////////
406 10 sumanta.ch
extern int ai_stop;
407 4 sumanta.ch
///////////* If this variable is non-zero, the system is trying to stop the AI thread
408
//////////   and your AI should exit. Do not set this yourself. */
409
//////////
410
//////////typedef AIMoves *(*AIFunc)(const Board *b);
411
///////////* AIs are defined as functions that output an unsorted, weighted list of board
412
//////////   coordinates for an arbitrary board. To create an AI in a file other than
413
//////////   ai.c, add a prototype of the function here and in ai.c. */
414
//////////
415
//////////AIMoves *enum_top_n(const Board *b, int n);
416
///////////* Returns an array containing the top n moves according to the utility
417
//////////   function */
418
//////////
419
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random);
420
/* Enumerate empty tiles at most dist away from some other piece on the board */
421
 
422 7 sumanta.ch
void streamsort(AIMoves *moves,unsigned int *index);
423 4 sumanta.ch
/*AIMoves **/void ai_marks(Board *b, PIECE min,AIMoves *moves);
424
/* Fills a moves list with tiles marked at least PIECE_THREAT(min) */
425
 
426
/*
427
 *      AI
428
 */
429
 
430
/* This table holds the information about all of the AIs in the program. Each
431
   has a short and long description. The short description will be used for
432
   the command line interface and the long description appears in the UI menu.
433
   Each AI has an associated AIFunc which outputs a move for the current
434
   board. */
435
///////////typedef struct AI {
436
///////////        char *s_desc, *l_desc;
437
///////////        AIFunc func;
438
///////////} AI;
439
///////////
440
///////////AIMoves *ai_sequences(const Board *b);
441
////////////* The square of the number of pieces in a window */
442
///////////
443
///////////AIMoves *ai_mesh(const Board *b);
444
////////////* The board as a mesh weighed down by the pieces */
445
///////////
446
///////////AIMoves *ai_monte_carlo(const Board *b);
447
////////////* Chooses the best move based on which one wins the most random games */
448
///////////
449
///////////AIMoves *ai_random(const Board *b);
450
////////////* Plays in a random tile */
451
///////////
452
/*AIMoves */ void ai_adjacent(Board *b,AIMove *move,unsigned int current_random);
453
/* Plays in a random tile adjacent to any piece on the board */
454
 
455
///////////AIMoves *ai_windows(const Board *b);
456
////////////* Plays in the best defensive position */
457
///////////
458
///////////AIMoves *ai_utility(const Board *b);
459
///////////AIMoves *ai_dfs_utility(const Board *b);
460
////////////* Utility function */
461
///////////
462 10 sumanta.ch
/*AIMoves **/int ai_threats(Board *board,AIMoves *moves,unsigned int *index);
463 4 sumanta.ch
AIMoves *ai_priority(const Board *b);
464
/* Multi-level threats */
465
 
466
 
467
 
468
void my_srandom(int seed,unsigned int *current_random);
469
int my_irand(int imax,unsigned int current_random);
470
//void backup_move(Board *board, AIMoves *moves,AIMove *move);
471 7 sumanta.ch
//AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound);
472
//int threat_window(int x, int y, int dx, int dy,
473
//                         PIECE *ptype, int *pdouble,Board *b);
474 4 sumanta.ch
int connect6ai_synth(int firstmove,char movein[8], char colour, char moveout[8]);
475 7 sumanta.ch
//extern "C" AIMove pico_stream_input_queue();
476
//extern "C" void pico_stream_output_queue(AIMove);
477
//extern "C" AIMove pico_ips_fifo_read_queue();
478
//extern "C" void pico_ips_fifo_write_queue(AIMove);
479
//extern int id;
480
//extern int ready; 
481 10 sumanta.ch
typedef struct {
482
        PLAYER ai;
483
        //SEARCH search;
484
        int depth, branch, cache, tss;
485
} Player;
486
/*AIMoves **/int search(const Board *board,AIMove *move, Player *player);
487 7 sumanta.ch
#endif

powered by: WebSVN 2.1.0

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