Merge branch 'master' of ssh://repo.or.cz/srv/git/pachi
[pachi/derm.git] / move.c
blob0314ecce33b7b75925e8d7057ce040bb9e8a44c9
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 static char *
15 coord2bstr(char *buf, coord_t c, struct board *board)
17 if (is_pass(c)) {
18 return "pass";
19 } else if (is_resign(c)) {
20 return "resign";
21 } else {
22 /* Some GTP servers are broken and won't grok lowercase coords */
23 snprintf(buf, 4, "%c%d", toupper(asdf[coord_x(c, board) - 1]), coord_y(c, board));
24 return buf;
28 /* Return coordinate in dynamically allocated buffer. */
29 char *
30 coord2str(coord_t c, struct board *board)
32 char buf[256];
33 return strdup(coord2bstr(buf, c, board));
36 /* Return coordinate in statically allocated buffer, with some backlog for
37 * multiple independent invocations. Useful for debugging. */
38 char *
39 coord2sstr(coord_t c, struct board *board)
41 static char *b;
42 static char bl[10][4];
43 static int bi;
44 b = bl[bi]; bi = (bi + 1) % 10;
45 return coord2bstr(b, c, board);
48 /* No sanity checking */
49 coord_t *
50 str2coord(char *str, int size)
52 if (!strcasecmp(str, "pass")) {
53 return coord_pass();
54 } else if (!strcasecmp(str, "resign")) {
55 return coord_resign();
56 } else {
57 char xc = tolower(str[0]);
58 return coord_init(xc - 'a' - (xc > 'i') + 1, atoi(str + 1), size);