MQ: Add mq_pick()
[pachi.git] / mq.h
blob0aabd220c772d68da6f4e87c5787de7c6aec0003
1 #ifndef ZZGO_MQ_H
2 #define ZZGO_MQ_H
4 /* Move queues; in fact, they are more like move lists, used to accumulate
5 * equally good move candidates, then choosing from them randomly. */
7 #include "move.h"
8 #include "random.h"
10 #define MQL 64
11 struct move_queue {
12 int moves;
13 coord_t move[MQL];
16 /* Pick a random move from the queue. */
17 static coord_t mq_pick(struct move_queue *q);
19 /* Add a move to the queue. */
20 static void mq_add(struct move_queue *q, coord_t c);
22 /* Cat two queues together. */
23 static void mq_append(struct move_queue *qd, struct move_queue *qs);
25 /* Check if the last move in queue is not a dupe, and remove it
26 * in that case. */
27 static void mq_nodup(struct move_queue *q);
30 static inline coord_t
31 mq_pick(struct move_queue *q)
33 return q->moves ? q->move[fast_random(q->moves)] : pass;
36 static inline void
37 mq_nodup(struct move_queue *q)
39 if ((q->moves > 1 && q->move[q->moves - 2] == q->move[q->moves - 1])
40 || (q->moves > 2 && q->move[q->moves - 3] == q->move[q->moves - 1])
41 || (q->moves > 3 && q->move[q->moves - 4] == q->move[q->moves - 1]))
42 q->moves--;
45 static inline void
46 mq_append(struct move_queue *qd, struct move_queue *qs)
48 assert(qd->moves + qs->moves < MQL);
49 memcpy(&qd->move[qd->moves], qs->move, qs->moves * sizeof(*qs->move));
50 qd->moves += qs->moves;
53 static inline void
54 mq_add(struct move_queue *q, coord_t c)
56 assert(q->moves < MQL);
57 q->move[q->moves++] = c;
60 #endif