tree_init: exit cleanly instead of core dump when running out of memory.
[pachi/ann.git] / move.h
blobefdbd6ec523d4b772f3e8147de7ee4f96b20b3ee
1 #ifndef ZZGO_MOVE_H
2 #define ZZGO_MOVE_H
4 #include <stdint.h>
5 #include <string.h>
7 #include "stone.h"
9 typedef int coord_t;
11 #define coord_raw(c) (c)
12 #define coord_x(c, b) ((c) % board_size(b))
13 #define coord_y(c, b) ((c) / board_size(b))
14 #define coord_eq(c1, c2) ((c1) == (c2))
15 /* TODO: Smarter way to do this? */
16 #define coord_dx(c1, c2, b) (coord_x(c1, b) - coord_x(c2, b))
17 #define coord_dy(c1, c2, b) (coord_y(c1, b) - coord_y(c2, b))
19 static coord_t pass = -1;
20 static coord_t resign = -2;
21 #define is_pass(c) (coord_eq(c, pass))
22 #define is_resign(c) (coord_eq(c, resign))
24 /* Initialize existing coord */
25 #define coord_pos(coord, pos_, board) do { (coord) = (pos_); } while (0)
26 #define coord_xy(board, x, y) ((x) + (y) * board_size(board))
27 #define coord_xy_otf(x, y, board) coord_xy(board, x, y) // obsolete
29 #define coord_is_adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(c1 - c2) == board_size(b))
30 #define coord_is_8adjecent(c1, c2, b) (abs(c1 - c2) == 1 || abs(abs(c1 - c2) - board_size(b)) < 2)
32 /* dyn allocated */
33 static coord_t *coord_init(int x, int y, int size);
34 static coord_t *coord_copy(coord_t c);
35 static coord_t *coord_pass(void);
36 static coord_t *coord_resign(void);
37 static void coord_done(coord_t *c);
39 struct board;
40 /* Return coordinate string in a dynamically allocated buffer. Thread-safe. */
41 char *coord2str(coord_t c, struct board *b);
42 /* Return coordinate string in a static buffer; multiple buffers are shuffled
43 * to enable use for multiple printf() parameters, but it is NOT safe for
44 * anything but debugging - in particular, it is NOT thread-safe! */
45 char *coord2sstr(coord_t c, struct board *b);
46 coord_t *str2coord(char *str, int board_size);
49 struct move {
50 coord_t coord;
51 enum stone color;
56 static inline coord_t *
57 coord_init(int x, int y, int size)
59 coord_t *c = calloc(1, sizeof(coord_t));
60 *c = x + y * size;
61 return c;
64 static inline coord_t *
65 coord_copy(coord_t c)
67 coord_t *c2 = calloc(1, sizeof(coord_t));
68 memcpy(c2, &c, sizeof(c));
69 return c2;
72 static inline coord_t *
73 coord_pass()
75 return coord_copy(pass);
78 static inline coord_t *
79 coord_resign()
81 return coord_copy(resign);
84 static inline void
85 coord_done(coord_t *c)
87 free(c);
90 #endif