MQ: Add support for tagging moves
[pachi.git] / mq.h
blob8f483dfff3a69e3a11ddf553c9f596862f1e4bdc
1 #ifndef ZZGO_MQ_H
2 #define ZZGO_MQ_H
4 /* Move queues; in fact, they are more like move lists, usually used
5 * to accumulate equally good move candidates, then choosing from them
6 * randomly. But they are also used to juggle group lists (using the
7 * fact that coord_t == group_t). */
9 #include "move.h"
10 #include "random.h"
12 #define MQL 512 /* XXX: On larger board this might not be enough. */
13 struct move_queue {
14 unsigned int moves;
15 coord_t move[MQL];
16 /* Each move can have an optional tag or set of tags.
17 * The usage of these is user-dependent. */
18 unsigned char tag[MQL];
21 /* Pick a random move from the queue. */
22 static coord_t mq_pick(struct move_queue *q);
24 /* Add a move to the queue. */
25 static void mq_add(struct move_queue *q, coord_t c, unsigned char tag);
27 /* Cat two queues together. */
28 static void mq_append(struct move_queue *qd, struct move_queue *qs);
30 /* Check if the last move in queue is not a dupe, and remove it
31 * in that case. */
32 static void mq_nodup(struct move_queue *q);
34 /* Print queue contents on stderr. */
35 static void mq_print(struct move_queue *q, struct board *b, char *label);
38 static inline coord_t
39 mq_pick(struct move_queue *q)
41 return q->moves ? q->move[fast_random(q->moves)] : pass;
44 static inline void
45 mq_add(struct move_queue *q, coord_t c, unsigned char tag)
47 assert(q->moves < MQL);
48 q->tag[q->moves] = tag;
49 q->move[q->moves++] = c;
52 static inline void
53 mq_append(struct move_queue *qd, struct move_queue *qs)
55 assert(qd->moves + qs->moves < MQL);
56 memcpy(&qd->tag[qd->moves], qs->tag, qs->moves * sizeof(*qs->tag));
57 memcpy(&qd->move[qd->moves], qs->move, qs->moves * sizeof(*qs->move));
58 qd->moves += qs->moves;
61 static inline void
62 mq_nodup(struct move_queue *q)
64 if ((q->moves > 1 && q->move[q->moves - 2] == q->move[q->moves - 1])
65 || (q->moves > 2 && q->move[q->moves - 3] == q->move[q->moves - 1])
66 || (q->moves > 3 && q->move[q->moves - 4] == q->move[q->moves - 1]))
67 q->moves--;
70 static inline void
71 mq_print(struct move_queue *q, struct board *b, char *label)
73 fprintf(stderr, "%s candidate moves: ", label);
74 for (unsigned int i = 0; i < q->moves; i++) {
75 fprintf(stderr, "%s ", coord2sstr(q->move[i], b));
77 fprintf(stderr, "\n");
80 #endif