Merge branch 'master' of git+ssh://repo.or.cz/srv/git/pachi
[pachi/derm.git] / move.h
blob93bfbb54dde1b3d5d1ad6b1b70cc77bd05caffa4
1 #ifndef ZZGO_MOVE_H
2 #define ZZGO_MOVE_H
4 #include <stdint.h>
5 #include <string.h>
7 #include "util.h"
8 #include "stone.h"
10 typedef int coord_t;
12 #define coord_raw(c) (c)
13 #define coord_x(c, b) ((c) % board_size(b))
14 #define coord_y(c, b) ((c) / board_size(b))
15 #define coord_eq(c1, c2) ((c1) == (c2))
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) (coord_eq(c, pass))
23 #define is_resign(c) (coord_eq(c, resign))
25 /* Initialize existing coord */
26 #define coord_pos(coord, pos_, board) do { (coord) = (pos_); } while (0)
27 #define coord_xy(board, x, y) ((x) + (y) * board_size(board))
28 #define coord_xy_otf(x, y, board) coord_xy(board, x, y) // obsolete
30 #define coord_is_adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(c1 - c2) == board_size(b))
31 #define coord_is_8adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(abs(c1 - c2) - board_size(b)) < 2)
33 /* dyn allocated */
34 static coord_t *coord_init(int x, int y, int size);
35 static coord_t *coord_copy(coord_t c);
36 static coord_t *coord_pass(void);
37 static coord_t *coord_resign(void);
38 static void coord_done(coord_t *c);
40 struct board;
41 /* Return coordinate string in a dynamically allocated buffer. Thread-safe. */
42 char *coord2str(coord_t c, struct board *b);
43 /* Return coordinate string in a static buffer; multiple buffers are shuffled
44 * to enable use for multiple printf() parameters, but it is NOT safe for
45 * anything but debugging - in particular, it is NOT thread-safe! */
46 char *coord2sstr(coord_t c, struct board *b);
47 coord_t *str2coord(char *str, int board_size);
50 struct move {
51 coord_t coord;
52 enum stone color;
57 static inline coord_t *
58 coord_init(int x, int y, int size)
60 coord_t *c = calloc2(1, sizeof(coord_t));
61 *c = x + y * size;
62 return c;
65 static inline coord_t *
66 coord_copy(coord_t c)
68 coord_t *c2 = calloc2(1, sizeof(coord_t));
69 memcpy(c2, &c, sizeof(c));
70 return c2;
73 static inline coord_t *
74 coord_pass()
76 return coord_copy(pass);
79 static inline coord_t *
80 coord_resign()
82 return coord_copy(resign);
85 static inline void
86 coord_done(coord_t *c)
88 free(c);
91 #endif