author | Mikael Berthe <mikael@lilotux.net> |
Sun, 16 Oct 2016 12:47:49 +0200 | |
changeset 11 | cd1296df76f1 |
parent 10 | 8dc05ff5dbe2 |
permissions | -rw-r--r-- |
10 | 1 |
// Copyright (C) 2016 Mikael Berthe <mikael@lilotux.net>. All rights reserved. |
2 |
// Use of this source code is governed by the MIT license, |
|
3 |
// which can be found in the LICENSE file. |
|
4 |
||
5 |
// Package takuzu provides functions to solve, build or validate takuzu |
|
6 |
// puzzles. |
|
0 | 7 |
package takuzu |
8 |
||
9 |
import ( |
|
10 |
"bytes" |
|
11 |
"fmt" |
|
12 |
"math" |
|
13 |
||
14 |
"github.com/pkg/errors" |
|
15 |
) |
|
16 |
||
17 |
// Cell is a single cell of a Takuzu game board |
|
18 |
type Cell struct { |
|
19 |
Defined bool |
|
20 |
Value int |
|
21 |
} |
|
22 |
||
23 |
// Takuzu is a Takuzu game board (Size x Size) |
|
24 |
type Takuzu struct { |
|
25 |
Size int |
|
26 |
Board [][]Cell |
|
27 |
} |
|
28 |
||
29 |
// New creates a new Takuzu board |
|
30 |
func New(size int) Takuzu { |
|
31 |
t := Takuzu{Size: size} |
|
32 |
t.Board = make([][]Cell, size) |
|
33 |
for l := range t.Board { |
|
34 |
t.Board[l] = make([]Cell, size) |
|
35 |
} |
|
36 |
return t |
|
37 |
} |
|
38 |
||
39 |
// NewFromString creates a new Takuzu board from a string definition |
|
40 |
func NewFromString(s string) (*Takuzu, error) { |
|
41 |
l := len(s) |
|
2 | 42 |
if l < 4 { |
43 |
return nil, errors.New("bad string length") |
|
44 |
} |
|
45 |
||
0 | 46 |
size := int(math.Sqrt(float64(l))) |
47 |
if size*size != l { |
|
48 |
return nil, errors.New("bad string length") |
|
49 |
} |
|
50 |
||
51 |
i := 0 |
|
52 |
t := New(size) |
|
53 |
||
54 |
for line := 0; line < size; line++ { |
|
55 |
for col := 0; col < size; col++ { |
|
56 |
switch s[i] { |
|
57 |
case '0', 'O': |
|
58 |
t.Board[line][col].Defined = true |
|
59 |
t.Board[line][col].Value = 0 |
|
60 |
case '1', 'I': |
|
61 |
t.Board[line][col].Defined = true |
|
62 |
t.Board[line][col].Value = 1 |
|
63 |
case '.': |
|
64 |
default: |
|
65 |
return nil, errors.New("invalid char in string") |
|
66 |
} |
|
67 |
i++ |
|
68 |
} |
|
69 |
} |
|
70 |
return &t, nil |
|
71 |
} |
|
72 |
||
73 |
// ToString converts a takuzu board to its string representation |
|
74 |
func (b Takuzu) ToString() string { |
|
75 |
var sbuf bytes.Buffer |
|
76 |
for line := 0; line < b.Size; line++ { |
|
77 |
for col := 0; col < b.Size; col++ { |
|
78 |
if b.Board[line][col].Defined { |
|
79 |
sbuf.WriteString(fmt.Sprintf("%d", b.Board[line][col].Value)) |
|
80 |
continue |
|
81 |
} |
|
82 |
sbuf.WriteByte('.') |
|
83 |
} |
|
84 |
} |
|
85 |
return sbuf.String() |
|
86 |
} |
|
87 |
||
6
110d38ae22cd
Refactor code; split takuzu.go and create solve.go
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
88 |
// DumpString writes the content of the board as a string |
0 | 89 |
func (b Takuzu) DumpString() { |
90 |
fmt.Println(b.ToString()) |
|
91 |
} |
|
92 |
||
93 |
// Clone returns a copy of the Takuzu board |
|
94 |
func (b Takuzu) Clone() Takuzu { |
|
95 |
c := New(b.Size) |
|
96 |
for line := range b.Board { |
|
97 |
for col := range b.Board[line] { |
|
98 |
c.Board[line][col] = b.Board[line][col] |
|
99 |
} |
|
100 |
} |
|
101 |
return c |
|
102 |
} |
|
103 |
||
104 |
// Copy copies a Takuzu board to another existing board |
|
105 |
func Copy(src, dst *Takuzu) error { |
|
106 |
if src.Size != dst.Size { |
|
107 |
return errors.New("sizes do not match") |
|
108 |
} |
|
109 |
for line := range src.Board { |
|
110 |
for col := range src.Board[line] { |
|
111 |
dst.Board[line][col] = src.Board[line][col] |
|
112 |
} |
|
113 |
} |
|
114 |
return nil |
|
115 |
} |
|
116 |
||
117 |
// BoardsMatch compares a Takuzu board to another, optionally ignoring |
|
118 |
// empty cells. Returns true if the two boards match. |
|
119 |
func BoardsMatch(t1, t2 *Takuzu, ignoreUndefined bool) (match bool, line, col int) { |
|
120 |
match = true |
|
121 |
||
2 | 122 |
if t1 == nil || t2 == nil { |
123 |
line, col = -1, -1 |
|
124 |
match = false |
|
125 |
return |
|
126 |
} |
|
127 |
||
0 | 128 |
if t1.Size != t2.Size { |
129 |
line, col = -1, -1 |
|
130 |
match = false |
|
131 |
return |
|
132 |
} |
|
2 | 133 |
|
0 | 134 |
for line = range t1.Board { |
135 |
for col = range t1.Board[line] { |
|
136 |
if !t1.Board[line][col].Defined || !t2.Board[line][col].Defined { |
|
137 |
// At least one of the cells is empty |
|
138 |
if ignoreUndefined || |
|
139 |
!(t1.Board[line][col].Defined || t2.Board[line][col].Defined) { |
|
140 |
// Both cells are empty or we ignore empty cells |
|
141 |
continue |
|
142 |
} |
|
143 |
match = false |
|
144 |
return |
|
145 |
} |
|
146 |
// Both cells are defined |
|
147 |
if t1.Board[line][col].Value != t2.Board[line][col].Value { |
|
148 |
match = false |
|
149 |
return |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
line, col = -1, -1 |
|
155 |
return |
|
156 |
} |
|
157 |
||
158 |
// Set sets the value of the cell; a value -1 will set the cell as undefined |
|
159 |
func (c *Cell) Set(value int) { |
|
160 |
if value != 0 && value != 1 { |
|
161 |
c.Defined = false |
|
162 |
return |
|
163 |
} |
|
164 |
c.Defined = true |
|
165 |
c.Value = value |
|
166 |
} |
|
167 |
||
168 |
// Set sets the value of a specific cell |
|
169 |
// A value -1 will undefine the cell |
|
170 |
func (b Takuzu) Set(l, c, value int) { |
|
171 |
if value != 0 && value != 1 { |
|
172 |
b.Board[l][c].Defined = false |
|
173 |
return |
|
174 |
} |
|
175 |
b.Board[l][c].Defined = true |
|
176 |
b.Board[l][c].Value = value |
|
177 |
} |
|
178 |
||
179 |
// GetLine returns a slice of cells containing the ith line of the board |
|
180 |
func (b Takuzu) GetLine(i int) []Cell { |
|
181 |
return b.Board[i] |
|
182 |
} |
|
183 |
||
184 |
// GetColumn returns a slice of cells containing the ith column of the board |
|
185 |
func (b Takuzu) GetColumn(i int) []Cell { |
|
186 |
c := make([]Cell, b.Size) |
|
187 |
for l := range b.Board { |
|
188 |
c[l] = b.Board[l][i] |
|
189 |
} |
|
190 |
return c |
|
191 |
} |
|
192 |
||
193 |
// GetLinePointers returns a slice of pointers to the cells of the ith line of the board |
|
194 |
func (b Takuzu) GetLinePointers(i int) []*Cell { |
|
195 |
r := make([]*Cell, b.Size) |
|
196 |
for l := range b.Board[i] { |
|
197 |
r[l] = &b.Board[i][l] |
|
198 |
} |
|
199 |
return r |
|
200 |
} |
|
201 |
||
202 |
// GetColumnPointers returns a slice of pointers to the cells of the ith column of the board |
|
203 |
func (b Takuzu) GetColumnPointers(i int) []*Cell { |
|
204 |
r := make([]*Cell, b.Size) |
|
205 |
for l := range b.Board { |
|
206 |
r[l] = &b.Board[l][i] |
|
207 |
} |
|
208 |
return r |
|
209 |
} |
|
210 |
||
211 |
// FillLineColumn add missing 0s or 1s if all 1s or 0s are there. |
|
212 |
// Note: This method can update b. |
|
213 |
func (b Takuzu) FillLineColumn(l, c int) { |
|
214 |
fillRange := func(r []*Cell) { |
|
215 |
size := len(r) |
|
216 |
var notFull bool |
|
217 |
var n [2]int |
|
218 |
for x := 0; x < size; x++ { |
|
219 |
if r[x].Defined { |
|
220 |
n[r[x].Value]++ |
|
221 |
} else { |
|
222 |
notFull = true |
|
223 |
} |
|
224 |
} |
|
225 |
if !notFull { |
|
226 |
return |
|
227 |
} |
|
228 |
if n[0] == size/2 { |
|
229 |
// Let's fill the 1s |
|
230 |
for _, x := range r { |
|
231 |
if !x.Defined { |
|
232 |
x.Defined = true |
|
233 |
x.Value = 1 |
|
234 |
} |
|
235 |
} |
|
236 |
} else if n[1] == size/2 { |
|
237 |
// Let's fill the 0s |
|
238 |
for _, x := range r { |
|
239 |
if !x.Defined { |
|
240 |
x.Defined = true |
|
241 |
x.Value = 0 |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
var cells []*Cell |
|
248 |
||
249 |
// Fill line |
|
250 |
cells = b.GetLinePointers(l) |
|
251 |
fillRange(cells) |
|
252 |
// Fill column |
|
253 |
cells = b.GetColumnPointers(c) |
|
254 |
fillRange(cells) |
|
255 |
} |
|
256 |
||
257 |
// DumpBoard displays the Takuzu board |
|
258 |
func (b Takuzu) DumpBoard() { |
|
259 |
fmt.Println() |
|
260 |
for i := range b.Board { |
|
261 |
dumpRange(b.Board[i]) |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
func dumpRange(cells []Cell) { |
|
266 |
for _, c := range cells { |
|
267 |
if !c.Defined { |
|
268 |
fmt.Printf(". ") |
|
269 |
continue |
|
270 |
} |
|
271 |
fmt.Printf("%d ", c.Value) |
|
272 |
} |
|
273 |
fmt.Println() |
|
274 |
} |