forked from pasky/pachi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.c
60 lines (52 loc) · 1.32 KB
/
move.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "board.h"
#include "move.h"
/* The S_OFFBOARD margin is not addressable by coordinates. */
static char asdf[] = "abcdefghjklmnopqrstuvwxyz";
char *
coord2bstr(char *buf, coord_t c, struct board *board)
{
if (is_pass(c)) {
return "pass";
} else if (is_resign(c)) {
return "resign";
} else {
/* Some GTP servers are broken and won't grok lowercase coords */
snprintf(buf, 4, "%c%d", toupper(asdf[coord_x(c, board) - 1]), coord_y(c, board));
return buf;
}
}
/* Return coordinate in dynamically allocated buffer. */
char *
coord2str(coord_t c, struct board *board)
{
char buf[256];
return strdup(coord2bstr(buf, c, board));
}
/* Return coordinate in statically allocated buffer, with some backlog for
* multiple independent invocations. Useful for debugging. */
char *
coord2sstr(coord_t c, struct board *board)
{
static char *b;
static char bl[10][4];
static int bi;
b = bl[bi]; bi = (bi + 1) % 10;
return coord2bstr(b, c, board);
}
/* No sanity checking */
coord_t *
str2coord(char *str, int size)
{
if (!strcasecmp(str, "pass")) {
return coord_pass();
} else if (!strcasecmp(str, "resign")) {
return coord_resign();
} else {
char xc = tolower(str[0]);
return coord_init(xc - 'a' - (xc > 'i') + 1, atoi(str + 1), size);
}
}