Skip to content

Commit fdfc522

Browse files
authored
Add basic snapshot data types + symbols (#202)
Basic work on snapshotting logic This PR provides a basic snapshotting library that makes a symbolic representation of a go-board. Very basic initial start on the idea. Partial work on: #13
1 parent 2666aea commit fdfc522

File tree

6 files changed

+233
-0
lines changed

6 files changed

+233
-0
lines changed

core/movetree/node.go

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type Node struct {
4343
// example, handicap stones will be in in placements.
4444
Placements []*move.Move
4545

46+
// TODO(#193): Move GameInfo property to MoveTree struct.
47+
4648
// GameInfo contains properties only found on the root. Should be nil on
4749
// non-root nodes.
4850
GameInfo *GameInfo

core/snapshot/intersection.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package snapshot
2+
3+
import (
4+
"github.com/otrego/clamshell/core/point"
5+
"github.com/otrego/clamshell/core/snapshot/symbol"
6+
)
7+
8+
// Intersection contains information for a single intersection
9+
type Intersection struct {
10+
// The Point for this intersection.
11+
Point *point.Point
12+
13+
// The Base symbol is the first layer of the intesection, indicating the
14+
// underlying board symbol.
15+
Base symbol.Symbol
16+
17+
// The Symbol symbol is the second layer of the intesection, indicating the
18+
// black or white stones.
19+
Stone symbol.Symbol
20+
21+
// The Mark symbol is the third layer of the intesection, indicating the
22+
// marks on top of the board or stones.
23+
Mark symbol.Symbol
24+
25+
// Label for the intersection. Label should only be set when Mark == TextLabel
26+
// or a similar label-mark.
27+
Label string
28+
}
29+
30+
// TopLayerUnicodeString outputs a single character for the intersection, based
31+
// on the "Top" symbol. This method is primarily for debugging.
32+
func (n *Intersection) TopLayerUnicodeString() string {
33+
switch {
34+
case n.Mark != symbol.Empty:
35+
return n.Mark.UnicodeString()
36+
case n.Stone != symbol.Empty:
37+
return n.Stone.UnicodeString()
38+
case n.Base != symbol.Empty:
39+
return n.Base.UnicodeString()
40+
}
41+
return " "
42+
}

core/snapshot/intersection_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package snapshot
2+
3+
import (
4+
"testing"
5+
6+
"github.com/otrego/clamshell/core/snapshot/symbol"
7+
)
8+
9+
func TestIntersection_TopLayerUnicodeChar(t *testing.T) {
10+
testCases := []struct {
11+
in *Intersection
12+
exp string
13+
}{
14+
{
15+
in: &Intersection{
16+
Base: symbol.TopLeft,
17+
},
18+
exp: "┏",
19+
},
20+
{
21+
in: &Intersection{
22+
Base: symbol.TopLeft,
23+
Stone: symbol.BlackStone,
24+
},
25+
exp: "●",
26+
},
27+
{
28+
in: &Intersection{
29+
Base: symbol.TopLeft,
30+
Stone: symbol.BlackStone,
31+
Mark: symbol.Xmark,
32+
},
33+
exp: "☓",
34+
},
35+
{
36+
in: &Intersection{},
37+
exp: " ",
38+
},
39+
}
40+
41+
for _, tc := range testCases {
42+
if out := tc.in.TopLayerUnicodeString(); out != tc.exp {
43+
t.Errorf("(%v).TopLayerUnicodeString()=%s, but expected %s", tc.in, out, tc.exp)
44+
}
45+
}
46+
}

core/snapshot/snapshot.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Package snapshot makes a flattened, abstracted snapshot of a game
2+
// position.
3+
//
4+
// This is based on the flattener logic from glift-core:
5+
// https://github.com/Kashomon/glift-core/tree/master/src/flattener
6+
package snapshot
7+
8+
import (
9+
"github.com/otrego/clamshell/core/bbox"
10+
"github.com/otrego/clamshell/core/movetree"
11+
)
12+
13+
// Options contains options for creating flattened snapshots.
14+
type Options struct {
15+
}
16+
17+
// Create a new Snapshot from a given movetree and path.
18+
func Create(mt *movetree.MoveTree, position movetree.Path, opts *Options) *Snapshot {
19+
return &Snapshot{}
20+
}
21+
22+
// A Snapshot represents a specific board position, which can be used during
23+
// image generation.
24+
type Snapshot struct {
25+
// Comment for this position.
26+
Comment string
27+
28+
// Borad contains the symbolic representation of the display of the board.
29+
Board *Board
30+
}
31+
32+
// Board is a snapshot-board representation. It can be cropped.
33+
type Board struct {
34+
// Intersections contain the intersections for the board.
35+
Intersections [][]*Intersection
36+
37+
// The CropBox for the board, specifying the original size and the cropped
38+
// bounding box.
39+
CropBox *bbox.CropBox
40+
}

core/snapshot/symbol/symbol.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Package symbol contains an inventory of intersection symbol.
2+
package symbol
3+
4+
// Symbol represents an intersection on the board.
5+
type Symbol int
6+
7+
const (
8+
// Empty is the default symbol.
9+
Empty Symbol = 0
10+
11+
// TopLeft corner of the board.
12+
TopLeft Symbol = 10
13+
// TopRight corner of the board.
14+
TopRight Symbol = 11
15+
// BottomLeft corner of the board.
16+
BottomLeft Symbol = 12
17+
// BottomRight corner of the board.
18+
BottomRight Symbol = 13
19+
// TopEdge of the board.
20+
TopEdge Symbol = 14
21+
// BottomEdge of the board.
22+
BottomEdge Symbol = 15
23+
// LeftEdge of the board.
24+
LeftEdge Symbol = 16
25+
// RightEdge of the board.
26+
RightEdge Symbol = 17
27+
// Center of the board.
28+
Center Symbol = 18
29+
// StarPoint of the board.
30+
StarPoint Symbol = 19
31+
32+
// BlackStone is a black stone.
33+
BlackStone Symbol = 20
34+
// WhiteStone is a white stone.
35+
WhiteStone Symbol = 21
36+
37+
// Triangle is a triangle-mark.
38+
Triangle Symbol = 30
39+
// Square is a square-mark.
40+
Square Symbol = 31
41+
// Circle is a circle-mark.
42+
Circle Symbol = 32
43+
// Xmark is an x-mark.
44+
Xmark Symbol = 33
45+
46+
// TextLabel represents some arbitrary text-label mark.
47+
TextLabel Symbol = 34
48+
)
49+
50+
// UnicodeString converts a symbol representation to a single char. This method is
51+
// primarily for debugging.
52+
func (s Symbol) UnicodeString() string {
53+
switch s {
54+
case TopLeft:
55+
return "┏"
56+
case TopRight:
57+
return "┓"
58+
case BottomLeft:
59+
return "┗"
60+
case BottomRight:
61+
return "┛"
62+
case TopEdge:
63+
return "┳"
64+
case BottomEdge:
65+
return "┻"
66+
case LeftEdge:
67+
return "┣"
68+
case RightEdge:
69+
return "┫"
70+
case Center:
71+
return "╋"
72+
case StarPoint:
73+
return "✻"
74+
case BlackStone:
75+
return "●"
76+
case WhiteStone:
77+
return "○"
78+
case Triangle:
79+
return "▴"
80+
case Square:
81+
return "□"
82+
case Circle:
83+
return "⊙"
84+
case Xmark:
85+
return "☓"
86+
case TextLabel:
87+
return "☒"
88+
}
89+
return "?"
90+
}

core/snapshot/symbol/symbol_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package symbol
2+
3+
import "testing"
4+
5+
func TestUnicodeString(t *testing.T) {
6+
if got := TopLeft.UnicodeString(); got != "┏" {
7+
t.Errorf("expected TopLeft.UnicodeString() == \"\", but was %q", got)
8+
}
9+
10+
if got := Symbol(100).UnicodeString(); got != "?" {
11+
t.Errorf("expected unknown.UnicodeString() == \"?\", but was %q", got)
12+
}
13+
}

0 commit comments

Comments
 (0)