-
Notifications
You must be signed in to change notification settings - Fork 116
/
move.c
91 lines (75 loc) · 1.82 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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)
{
if (is_pass(c)) return "pass";
if (is_resign(c)) return "resign";
/* Some GTP servers are broken and won't grok lowercase coords */
snprintf(buf, 4, "%c%i", toupper(asdf[coord_x(c) - 1]), coord_y(c) % 100);
return buf;
}
/* Return coordinate in dynamically allocated buffer. */
char *
coord2str(coord_t c)
{
char buf[256];
return strdup(coord2bstr(buf, c));
}
/* Return coordinate in statically allocated buffer, with some backlog for
* multiple independent invocations. Useful for debugging. */
char *
coord2sstr(coord_t c)
{
static char *b;
static char bl[10][4];
static int bi;
b = bl[bi]; bi = (bi + 1) % 10;
return coord2bstr(b, c);
}
/* No sanity checking */
coord_t
str2coord_for(char *str, int size)
{
if (!strcasecmp(str, "pass")) return pass;
if (!strcasecmp(str, "resign")) return resign;
char xc = tolower(str[0]);
int stride = size + 2;
return xc - 'a' - (xc > 'i') + 1 + atoi(str + 1) * stride;
}
coord_t
str2coord(char *str)
{
return str2coord_for(str, the_board_rsize());
}
bool
valid_str_coord(char *s)
{
if (!s || !s[0])
return false;
char c1 = toupper(s[0]);
char c2 = s[1];
int n = atoi(s + 1);
return (c1 >= 'A' && c1 <= 'T' &&
c2 >= '0' && c2 <= '9' &&
n >= 1 && n <= 19);
}
/* Must match rotations in pthashes_init() */
coord_t
rotate_coord(coord_t c, int rot)
{
assert(c != pass);
int size = the_board_rsize();
int x = coord_x(c);
int y = coord_y(c);
if (rot & 1) y = size - y + 1;
if (rot & 2) x = size - x + 1;
if (rot & 4) { int tmp = x; x = size - y + 1; y = tmp; }
return coord_xy(x, y);
}