author | Mikael Berthe <mikael@lilotux.net> |
Sun, 16 Oct 2016 10:08:11 +0200 | |
changeset 8 | 955d3add9426 |
parent 7 | takuzu.go@e284e3ad2800 |
child 9 | 4b3436c03726 |
permissions | -rw-r--r-- |
0 | 1 |
package takuzu |
2 |
||
3 |
import ( |
|
4 |
"fmt" |
|
5 |
||
6 |
"github.com/pkg/errors" |
|
7 |
) |
|
8 |
||
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
9 |
// checkRange returns true if the range is completely defined, and an error |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
10 |
// if it doesn't follow the rules for a takuzu line or column |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
11 |
// Note that the boolean might be invalid if the error is not nil. |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
12 |
func checkRange(cells []Cell) (bool, error) { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
13 |
full := true |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
14 |
size := len(cells) |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
15 |
counters := []int{0, 0} |
0 | 16 |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
17 |
var prevCell Cell |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
18 |
var prevCellCount int |
0 | 19 |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
20 |
for _, c := range cells { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
21 |
if !c.Defined { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
22 |
full = false |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
23 |
prevCell.Defined = false |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
24 |
prevCellCount = 0 |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
25 |
continue |
0 | 26 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
27 |
counters[c.Value]++ |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
28 |
if prevCellCount == 0 { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
29 |
prevCellCount = 1 |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
30 |
} else { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
31 |
if c.Value == prevCell.Value { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
32 |
prevCellCount++ |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
33 |
if prevCellCount > 2 { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
34 |
return full, errors.Errorf("3+ same values %d", c.Value) |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
35 |
} |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
36 |
} else { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
37 |
prevCellCount = 1 |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
38 |
} |
0 | 39 |
|
40 |
} |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
41 |
prevCell = c |
0 | 42 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
43 |
if counters[0] > size/2 { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
44 |
return full, errors.Errorf("too many zeroes") |
0 | 45 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
46 |
if counters[1] > size/2 { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
47 |
return full, errors.Errorf("too many ones") |
0 | 48 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
49 |
return full, nil |
0 | 50 |
} |
51 |
||
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
52 |
// CheckRangeCounts returns true if all cells of the provided range are defined, |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
53 |
// as well as the number of 0s and the number of 1s in the range. |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
54 |
func CheckRangeCounts(cells []Cell) (full bool, n0, n1 int) { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
55 |
counters := []int{0, 0} |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
56 |
full = true |
2 | 57 |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
58 |
for _, c := range cells { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
59 |
if c.Defined { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
60 |
counters[c.Value]++ |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
61 |
} else { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
62 |
full = false |
0 | 63 |
} |
64 |
} |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
65 |
return full, counters[0], counters[1] |
0 | 66 |
} |
67 |
||
68 |
// CheckLine returns an error if the line i fails validation |
|
69 |
func (b Takuzu) CheckLine(i int) error { |
|
70 |
_, err := checkRange(b.GetLine(i)) |
|
71 |
return err |
|
72 |
} |
|
73 |
||
74 |
// CheckColumn returns an error if the column i fails validation |
|
75 |
func (b Takuzu) CheckColumn(i int) error { |
|
76 |
_, err := checkRange(b.GetColumn(i)) |
|
77 |
return err |
|
78 |
} |
|
79 |
||
80 |
// Validate checks a whole board for errors (not completeness) |
|
81 |
// Returns true if all cells are defined. |
|
82 |
func (b Takuzu) Validate() (bool, error) { |
|
83 |
finished := true |
|
84 |
||
85 |
computeVal := func(cells []Cell) (val int) { |
|
86 |
for i := 0; i < len(cells); i++ { |
|
87 |
val += cells[i].Value * 1 << uint(i) |
|
88 |
} |
|
89 |
return |
|
90 |
} |
|
91 |
||
92 |
lineVals := make(map[int]bool) |
|
93 |
colVals := make(map[int]bool) |
|
94 |
||
95 |
for i := 0; i < b.Size; i++ { |
|
96 |
var d []Cell |
|
97 |
var full bool |
|
98 |
var err error |
|
99 |
||
100 |
// Let's check line i |
|
101 |
d = b.GetLine(i) |
|
102 |
full, err = checkRange(d) |
|
103 |
if err != nil { |
|
104 |
return false, errors.Wrapf(err, "line %d", i) |
|
105 |
} |
|
106 |
if full { |
|
107 |
hv := computeVal(d) |
|
108 |
if lineVals[hv] { |
|
109 |
return false, fmt.Errorf("duplicate lines (%d)", i) |
|
110 |
} |
|
111 |
lineVals[hv] = true |
|
112 |
} else { |
|
113 |
finished = false |
|
114 |
} |
|
115 |
||
116 |
// Let's check column i |
|
117 |
d = b.GetColumn(i) |
|
118 |
full, err = checkRange(d) |
|
119 |
if err != nil { |
|
120 |
return false, errors.Wrapf(err, "column %d", i) |
|
121 |
} |
|
122 |
if full { |
|
123 |
hv := computeVal(d) |
|
124 |
if colVals[hv] { |
|
3 | 125 |
return false, fmt.Errorf("duplicate columns (%d)", i) |
0 | 126 |
} |
127 |
colVals[hv] = true |
|
128 |
} else { |
|
129 |
finished = false |
|
130 |
} |
|
131 |
} |
|
132 |
return finished, nil |
|
133 |
} |