PF_*_TRAPPED: Determine safety for correct color to play
[pachi/derm.git] / move.h
blob4139a8b9640b31fc8bfb76d2918001e69342d64e
1 #ifndef ZZGO_MOVE_H
2 #define ZZGO_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 /* dyn allocated */
29 static coord_t *coord_init(int x, int y, int size);
30 static coord_t *coord_copy(coord_t c);
31 static coord_t *coord_pass(void);
32 static coord_t *coord_resign(void);
33 static void coord_done(coord_t *c);
35 struct board;
36 char *coord2bstr(char *buf, coord_t c, struct board *board);
37 /* Return coordinate string in a dynamically allocated buffer. Thread-safe. */
38 char *coord2str(coord_t c, struct board *b);
39 /* Return coordinate string in a static buffer; multiple buffers are shuffled
40 * to enable use for multiple printf() parameters, but it is NOT safe for
41 * anything but debugging - in particular, it is NOT thread-safe! */
42 char *coord2sstr(coord_t c, struct board *b);
43 coord_t *str2coord(char *str, int board_size);
46 struct move {
47 coord_t coord;
48 enum stone color;
53 static inline coord_t *
54 coord_init(int x, int y, int size)
56 coord_t *c = calloc2(1, sizeof(coord_t));
57 *c = x + y * size;
58 return c;
61 static inline coord_t *
62 coord_copy(coord_t c)
64 coord_t *c2 = calloc2(1, sizeof(coord_t));
65 memcpy(c2, &c, sizeof(c));
66 return c2;
69 static inline coord_t *
70 coord_pass()
72 return coord_copy(pass);
75 static inline coord_t *
76 coord_resign()
78 return coord_copy(resign);
81 /* No sanity checking */
82 static inline coord_t
83 str2scoord(char *str, int size)
85 if (!strcasecmp(str, "pass")) {
86 return pass;
87 } else if (!strcasecmp(str, "resign")) {
88 return resign;
89 } else {
90 char xc = tolower(str[0]);
91 return xc - 'a' - (xc > 'i') + 1 + atoi(str + 1) * size;
95 static inline void
96 coord_done(coord_t *c)
98 free(c);
101 #endif