forked from acityinohio/baduk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaduk.go
125 lines (118 loc) · 2.57 KB
/
baduk.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Package baduk implements a library for playing games
//of Baduk/Go. It's optimized for code simplicity,
//and doesn't include any AI support.
package baduk
import "errors"
//A Board represents information about the state
//of a Go game. Size represents the size of the board,
//Grid is the storage of Pieces.
type Board struct {
Size int
Grid [][]Piece
}
//A Piece represents information about a piece on the
//Board. Contains pointers to adjacent pieces. If it's
//a border, the pointer is nil.
type Piece struct {
Black bool
White bool
Empty bool
Up *Piece //y-1
Down *Piece //y+1
Left *Piece //x-1
Right *Piece //x+1
}
//Initializes an empty Board
func (b *Board) Init(size int) (err error) {
if size < 4 || size > 19 {
err = errors.New("Size of board must be between 4 and 19")
return
}
b.Size = size
//Allocate the top-level slice
b.Grid = make([][]Piece, size)
for i := range b.Grid {
//Allocate the intermediate slices
b.Grid[i] = make([]Piece, size)
}
//Set Pieces to Empty, connect them via pointers
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
up, down, left, right := true, true, true, true
b.Grid[y][x].Empty = true
//If border, don't connect
if y == 0 {
up = false
}
if y == size-1 {
down = false
}
if x == 0 {
left = false
}
if x == size-1 {
right = false
}
if up {
b.Grid[y][x].Up = &b.Grid[y-1][x]
}
if down {
b.Grid[y][x].Down = &b.Grid[y+1][x]
}
if left {
b.Grid[y][x].Left = &b.Grid[y][x-1]
}
if right {
b.Grid[y][x].Right = &b.Grid[y][x+1]
}
}
}
return
}
//Sets a Piece to black on the Board
//x, y in range from 1 to Board.Size
func (b *Board) SetB(x, y int) (err error) {
err = b.set(x, y, true)
return
}
//Sets a Piece to white on the Board
//x, y in range from 1 to Board.Size
func (b *Board) SetW(x, y int) (err error) {
err = b.set(x, y, false)
return
}
//Creates pretty string, suitable for use
//by fmt.Printf or any logging functions.
//Note that black and white circles are
//reversed compared to their unicode code points;
//this assumes your terminal has a dark background.
func (b *Board) PrettyString() (str string) {
str = "\n"
blk := "\u25cb"
wht := "\u25cf"
for y := 0; y < b.Size; y++ {
for x := 0; x < b.Size; x++ {
p := b.Grid[y][x]
switch {
case p.Black:
str += blk
case p.White:
str += wht
default:
str += " "
}
if x != b.Size-1 {
str += " - "
}
}
str += "\n"
if y != b.Size-1 {
for x := 0; x < b.Size-1; x++ {
str += "| - "
}
str += "|\n"
}
}
str += "\n"
return
}