t-unit: two_eyes test suite
[pachi.git] / move.h
blob5bc6b904f03a213de222886b171e4bc79ceffa84
1 #ifndef PACHI_MOVE_H
2 #define PACHI_MOVE_H
4 #include <ctype.h>
5 #include <stdint.h>
6 #include <string.h>
8 #include "util.h"
9 #include "stone.h"
11 typedef int coord_t;
13 #define coord_xy(board, x, y) ((x) + (y) * board_size(board))
14 #define coord_x(c, b) ((b)->coord[c][0])
15 #define coord_y(c, b) ((b)->coord[c][1])
16 /* TODO: Smarter way to do this? */
17 #define coord_dx(c1, c2, b) (coord_x(c1, b) - coord_x(c2, b))
18 #define coord_dy(c1, c2, b) (coord_y(c1, b) - coord_y(c2, b))
20 static coord_t pass = -1;
21 static coord_t resign = -2;
22 #define is_pass(c) (c == pass)
23 #define is_resign(c) (c == resign)
25 #define coord_is_adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(c1 - c2) == board_size(b))
26 #define coord_is_8adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(abs(c1 - c2) - board_size(b)) < 2)
28 /* Quadrants:
29 * 0 1
30 * 2 3 (vertically reversed from board_print output, of course!)
31 * Middle coordinates are included in lower-valued quadrants. */
32 #define coord_quadrant(c, b) ((coord_x(c, b) > board_size(b) / 2) + 2 * (coord_y(c, b) > board_size(b) / 2))
34 /* dyn allocated */
35 static coord_t *coord_init(int x, int y, int size);
36 static coord_t *coord_copy(coord_t c);
37 static coord_t *coord_pass(void);
38 static coord_t *coord_resign(void);
39 static void coord_done(coord_t *c);
41 struct board;
42 char *coord2bstr(char *buf, coord_t c, struct board *board);
43 /* Return coordinate string in a dynamically allocated buffer. Thread-safe. */
44 char *coord2str(coord_t c, struct board *b);
45 /* Return coordinate string in a static buffer; multiple buffers are shuffled
46 * to enable use for multiple printf() parameters, but it is NOT safe for
47 * anything but debugging - in particular, it is NOT thread-safe! */
48 char *coord2sstr(coord_t c, struct board *b);
49 coord_t *str2coord(char *str, int board_size);
52 struct move {
53 coord_t coord;
54 enum stone color;
59 static inline coord_t *
60 coord_init(int x, int y, int size)
62 coord_t *c = (coord_t*)calloc2(1, sizeof(coord_t));
63 *c = x + y * size;
64 return c;
67 static inline coord_t *
68 coord_copy(coord_t c)
70 coord_t *c2 = (coord_t*)calloc2(1, sizeof(coord_t));
71 memcpy(c2, &c, sizeof(c));
72 return c2;
75 static inline coord_t *
76 coord_pass()
78 return coord_copy(pass);
81 static inline coord_t *
82 coord_resign()
84 return coord_copy(resign);
87 /* No sanity checking */
88 static inline coord_t
89 str2scoord(char *str, int size)
91 if (!strcasecmp(str, "pass")) {
92 return pass;
93 } else if (!strcasecmp(str, "resign")) {
94 return resign;
95 } else {
96 char xc = tolower(str[0]);
97 return xc - 'a' - (xc > 'i') + 1 + atoi(str + 1) * size;
101 static inline void
102 coord_done(coord_t *c)
104 free(c);
107 static inline int
108 move_cmp(struct move *m1, struct move *m2)
110 if (m1->color != m2->color)
111 return m1->color - m2->color;
112 return m1->coord - m2->coord;
116 #endif