-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgf.lua
66 lines (58 loc) · 1.54 KB
/
sgf.lua
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
-- SGF FF[4] format is used, branches to be introduced eventually
SGF_BLACK = "B"
SGF_WHITE = "W"
-- "aa" --> {x=1, y=1}
function sgf_to_coords(sgf)
local x = string.byte(sgf:sub(1, 1)) - string.byte("a") + 1
local y = string.byte(sgf:sub(2, 2)) - string.byte("a") + 1
return x, y
end
-- {x=1, y=1} --> "aa"
function sgf_from_coord(x, y)
if not x and not y then
return ""
end
return string.char(x + 96) .. string.char(y + 96)
end
function sgf_set_size(sgf_in)
local size = sgf_in:match("SZ%[(%d+)%]")
if size then
SIZE = tonumber(size)
STONES = generate_stones()
end
end
function sgf_to_stone(sgf_in)
if not STONES then
STONES = generate_stones()
end
for prop, coord in sgf_in:gmatch("([BW])%[(%a%a)%]") do
local x, y = sgf_to_coords(coord)
if prop == SGF_BLACK then
STONES[x][y] = { color = BLACK, x = x, y = y }
elseif prop == SGF_WHITE then
STONES[x][y] = { color = WHITE, x = x, y = y }
end
end
-- return STONES
end
-- RECORD is easier to work with than SGF
-- SZ[2]B[aa] --> B...
function sgf_to_record() end
-- B... --> SZ[2]B[aa]
function sgf_from_record() end
-- input must be well-ordered, RECORD, not STONES
function sgf_from_stone(stones)
local sgf = "(;FF[4]GM[1]SZ[" .. SIZE .. "]"
local player
for _, stone in pairs(stones) do
if stone.color == BLACK then
player = SGF_BLACK
else
player = SGF_WHITE
end
local coord = sgf_from_coord(stone.x, stone.y)
sgf = sgf .. ";" .. player .. "[" .. coord .. "]"
end
sgf = sgf .. ")"
return sgf
end