Pachi Doetsu 4.00
[pachi/nmclean.git] / move.c
blob704255c86bbad7728686db3f7b6045da994976f1
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include "board.h"
7 #include "move.h"
10 /* The S_OFFBOARD margin is not addressable by coordinates. */
12 static char asdf[] = "abcdefghjklmnopqrstuvwxyz";
14 char *
15 coord2str(coord_t c, struct board *board)
17 return strdup(coord2sstr(c, board));
20 char *
21 coord2sstr(coord_t c, struct board *board)
23 static char *b;
24 static char bl[10][4];
25 static int bi;
26 if (is_pass(c)) {
27 return "pass";
28 } else if (is_resign(c)) {
29 return "resign";
30 } else {
31 /* Some GTP servers are broken and won't grok lowercase coords */
32 b = bl[bi]; bi = (bi + 1) % 10;
33 snprintf(b, 4, "%c%d", toupper(asdf[coord_x(c, board) - 1]), coord_y(c, board));
34 return b;
38 /* No sanity checking */
39 coord_t *
40 str2coord(char *str, int size)
42 if (!strcasecmp(str, "pass")) {
43 return coord_pass();
44 } else if (!strcasecmp(str, "resign")) {
45 return coord_resign();
46 } else {
47 char xc = tolower(str[0]);
48 return coord_init(xc - 'a' - (xc > 'i') + 1, atoi(str + 1), size);
53 int
54 coord_edge_distance(coord_t c, struct board *b)
56 int x = coord_x(c, b), y = coord_y(c, b);
57 int dx = x > board_size(b) / 2 ? board_size(b) - x : x;
58 int dy = y > board_size(b) / 2 ? board_size(b) - y : y;
59 return (dx < dy ? dx : dy) - 1 /* S_OFFBOARD */;