-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
52 lines (46 loc) · 1.03 KB
/
loader.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
package sudoku
import (
"encoding/json"
"fmt"
"os"
"strings"
)
type State struct {
Dim uint `json:"dim"`
Puzzle [][]uint `json:"puzzle"`
}
func LoadStateFromFile(path string) (s State, err error) {
var f *os.File
if f, err = os.Open(path); err != nil {
return
}
defer f.Close()
err = json.NewDecoder(f).Decode(&s)
return
}
func (s State) String() string {
boxStride := s.Dim
stride := boxStride * boxStride
sb := new(strings.Builder)
sb.WriteString("\n--------------------------------\n")
for y := uint(0); y < stride; y += boxStride {
for by := y; by < y+boxStride; by++ {
row := s.Puzzle[by]
sb.WriteString("| ")
for x := uint(0); x < stride; x += boxStride {
for bx := x; bx < x+boxStride; bx++ {
fmt.Fprintf(sb, "%0.2X ", row[bx])
}
if x+boxStride < stride {
sb.WriteByte(' ')
}
}
sb.WriteString("|\n")
}
if y+boxStride < stride {
sb.WriteString("| |\n")
}
}
sb.WriteString("--------------------------------\n")
return sb.String()
}