author | Mikael Berthe <mikael@lilotux.net> |
Sat, 07 Apr 2018 22:52:02 +0200 | |
changeset 16 | 1cde13306e8f |
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 |
||
0 | 5 |
package takuzu |
6 |
||
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
7 |
// This file contains the takuzu validation functions and methods. |
0 | 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 { |
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
34 |
v := c.Value |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
35 |
return full, validationError{ |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
36 |
ErrorType: ErrorTooManyAdjacentValues, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
37 |
CellValue: &v, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
38 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
39 |
} |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
40 |
} else { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
41 |
prevCellCount = 1 |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
42 |
} |
0 | 43 |
|
44 |
} |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
45 |
prevCell = c |
0 | 46 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
47 |
if counters[0] > size/2 { |
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
48 |
v := 0 |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
49 |
return full, validationError{ |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
50 |
ErrorType: ErrorTooManyValues, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
51 |
CellValue: &v, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
52 |
} |
0 | 53 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
54 |
if counters[1] > size/2 { |
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
55 |
v := 1 |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
56 |
return full, validationError{ |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
57 |
ErrorType: ErrorTooManyValues, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
58 |
CellValue: &v, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
59 |
} |
0 | 60 |
} |
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
61 |
return full, nil |
0 | 62 |
} |
63 |
||
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
64 |
// 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
|
65 |
// 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
|
66 |
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
|
67 |
counters := []int{0, 0} |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
68 |
full = true |
2 | 69 |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
70 |
for _, c := range cells { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
71 |
if c.Defined { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
72 |
counters[c.Value]++ |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
73 |
} else { |
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
74 |
full = false |
0 | 75 |
} |
76 |
} |
|
8
955d3add9426
More code refactoring; split takuzu.go and create validate.go
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
77 |
return full, counters[0], counters[1] |
0 | 78 |
} |
79 |
||
80 |
// CheckLine returns an error if the line i fails validation |
|
81 |
func (b Takuzu) CheckLine(i int) error { |
|
82 |
_, err := checkRange(b.GetLine(i)) |
|
83 |
return err |
|
84 |
} |
|
85 |
||
86 |
// CheckColumn returns an error if the column i fails validation |
|
87 |
func (b Takuzu) CheckColumn(i int) error { |
|
88 |
_, err := checkRange(b.GetColumn(i)) |
|
89 |
return err |
|
90 |
} |
|
91 |
||
92 |
// Validate checks a whole board for errors (not completeness) |
|
93 |
// Returns true if all cells are defined. |
|
94 |
func (b Takuzu) Validate() (bool, error) { |
|
95 |
finished := true |
|
96 |
||
97 |
computeVal := func(cells []Cell) (val int) { |
|
98 |
for i := 0; i < len(cells); i++ { |
|
99 |
val += cells[i].Value * 1 << uint(i) |
|
100 |
} |
|
101 |
return |
|
102 |
} |
|
103 |
||
104 |
lineVals := make(map[int]bool) |
|
105 |
colVals := make(map[int]bool) |
|
106 |
||
107 |
for i := 0; i < b.Size; i++ { |
|
108 |
var d []Cell |
|
109 |
var full bool |
|
110 |
var err error |
|
111 |
||
112 |
// Let's check line i |
|
113 |
d = b.GetLine(i) |
|
114 |
full, err = checkRange(d) |
|
115 |
if err != nil { |
|
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
116 |
err := err.(validationError) |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
117 |
err.LineNumber = &i |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
118 |
return false, err |
0 | 119 |
} |
120 |
if full { |
|
121 |
hv := computeVal(d) |
|
122 |
if lineVals[hv] { |
|
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
123 |
err := validationError{ |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
124 |
ErrorType: ErrorDuplicate, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
125 |
LineNumber: &i, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
126 |
} |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
127 |
return false, err |
0 | 128 |
} |
129 |
lineVals[hv] = true |
|
130 |
} else { |
|
131 |
finished = false |
|
132 |
} |
|
133 |
||
134 |
// Let's check column i |
|
135 |
d = b.GetColumn(i) |
|
136 |
full, err = checkRange(d) |
|
137 |
if err != nil { |
|
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
138 |
err := err.(validationError) |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
139 |
err.ColumnNumber = &i |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
140 |
return false, err |
0 | 141 |
} |
142 |
if full { |
|
143 |
hv := computeVal(d) |
|
144 |
if colVals[hv] { |
|
9
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
145 |
err := validationError{ |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
146 |
ErrorType: ErrorDuplicate, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
147 |
ColumnNumber: &i, |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
148 |
} |
4b3436c03726
Switch completely to the new validation "ErrorType"
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
149 |
return false, err |
0 | 150 |
} |
151 |
colVals[hv] = true |
|
152 |
} else { |
|
153 |
finished = false |
|
154 |
} |
|
155 |
} |
|
156 |
return finished, nil |
|
157 |
} |