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