-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.cs
234 lines (215 loc) · 8.28 KB
/
Worker.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System.Collections.Generic;
namespace Binoxxo_Solver
{
class Worker
{
public void Run(object param)
{
List<List<Field>> tuples = (List<List<Field>>)param;
do
{
foreach (List<Field> tuple in tuples)
{
CheckPairs(tuple);
FillGaps(tuple);
PreventTriplets(tuple);
EqualizeXAndO(tuple);
PreventIdenticalLines(tuple);
FarNeighbors(tuple);
FarSiblings(tuple);
}
} while (!Solver.IsSolved());
}
#region Check Pairs
// _XX_ -> OXXO
private void CheckPairs(List<Field> tuple)
{
int countO = 0;
int countX = 0;
for (int i = 0; i < tuple.Count; i++)
{
countO = tuple[i].value == 0 ? ++countO : 0;
countX = tuple[i].value == 1 ? ++countX : 0;
if (countO == 2)
{
if ((i - 2) >= 0 && tuple[i - 2].value == null)
{
tuple[i - 2].value = 1;
}
if ((i + 1) < tuple.Count && tuple[i + 1].value == null)
{
tuple[i + 1].value = 1;
}
}
if (countX == 2)
{
if ((i - 2) >= 0 && tuple[i - 2].value == null)
{
tuple[i - 2].value = 0;
}
if ((i + 1) < tuple.Count && tuple[i + 1].value == null)
{
tuple[i + 1].value = 0;
}
}
}
}
#endregion
#region Fill Gaps
// X_X -> XOX
private void FillGaps(List<Field> tuple)
{
for (int i = 0; i < tuple.Count; i++)
{
int? value = tuple[i].value;
if (value != null && (i + 2) < tuple.Count && tuple[i + 1].value == null && tuple[i + 2].value == value)
{
tuple[i + 1].value = 1 - value;
}
}
}
#endregion
#region Prevent Triplets
// _X__OX -> OX__OX
private void PreventTriplets(List<Field> tuple)
{
int value = Solver.CountElement(tuple, 0) > Solver.CountElement(tuple, 1) ? 1 : 0;
int[] indexes = Solver.GetIndexesOf(tuple, null);
if (indexes.Length != 3) { return; }
int doubleNullIndex;
int singleNullIndex;
if ((indexes[0] + 1) == indexes[1])
{
doubleNullIndex = indexes[0];
singleNullIndex = indexes[2];
}
else if ((indexes[1] + 1) == indexes[2])
{
doubleNullIndex = indexes[1];
singleNullIndex = indexes[0];
}
else { return; }
if ((doubleNullIndex - 1) >= 0 && tuple[doubleNullIndex - 1].value == value)
{
tuple[singleNullIndex].value = value;
}
else if ((doubleNullIndex + 2) < tuple.Count && tuple[doubleNullIndex + 2].value == value)
{
tuple[singleNullIndex].value = value;
}
}
#endregion
#region Equalize X and O
// _X__XX -> OXOOXX
private void EqualizeXAndO(List<Field> tuple)
{
if (Solver.CountElement(tuple, null) > 0)
{
if ((Solver.CountElement(tuple, 0) * 2) == tuple.Count)
{
foreach (Field f in tuple)
{
if (f.value == null) { f.value = 1; }
}
}
else if ((Solver.CountElement(tuple, 1) * 2) == tuple.Count)
{
foreach (Field f in tuple)
{
if (f.value == null) { f.value = 0; }
}
}
}
}
#endregion
#region Prevent Identical Lines
private void PreventIdenticalLines(List<Field> tuple)
{
if (Solver.CountElement(tuple, null) != 2 && Solver.CountElement(tuple, null) != 3) { return; }
PreventIdenticalLinesIterator(tuple, Solver.GetAllColumns());
PreventIdenticalLinesIterator(tuple, Solver.GetAllRows());
}
private void PreventIdenticalLinesIterator(List<Field> tuple, List<List<Field>> compareTo)
{
foreach (List<Field> line in compareTo)
{
if (Solver.CountElement(line, null) != 0) { continue; }
int nullCount = Solver.CountElement(tuple, null);
int differences = Solver.CompareLines(tuple, line);
if (nullCount == 2 && differences == 2)
{
int[] indexes = Solver.GetIndexesOf(tuple, null);
if (indexes.Length != 3) { return; }
if (tuple[indexes[0]].value != tuple[indexes[1]].value)
{
tuple[indexes[0]].value = 1 - line[indexes[0]].value;
tuple[indexes[1]].value = 1 - line[indexes[1]].value;
}
}
// XOOX XOOX
// _O__ -> _OX_
else if (nullCount == 3 && differences == 3 && Solver.GetNullMargin(tuple, 3) == 4)
{
int index = -1;
for (int i = 0; i < tuple.Count; i++)
{
if (tuple[i].value == null)
{
index = i;
break;
}
}
if (tuple[index + 1].value == null)
{
tuple[index + 1].value = 1 - line[index + 1].value;
}
else if (tuple[index + 2].value == null)
{
tuple[index + 2].value = 1 - line[index + 2].value;
}
}
}
}
#endregion
#region Far Neighbors
// O__O__X__O -> OX_O__X__O
private void FarNeighbors(List<Field> tuple)
{
if (Solver.CountElement(tuple, null) != 6) { return; }
int countO = Solver.CountElement(tuple, 0);
int countX = Solver.CountElement(tuple, 1);
if (countO != (countX + 2) && countX != (countO + 2)) { return; }
int[] positions = new int[4];
positions[0] = Solver.TupleContainsPattern(tuple, "o__o__x__o");
positions[1] = Solver.TupleContainsPattern(tuple, "o__x__o__o");
positions[2] = Solver.TupleContainsPattern(tuple, "x__x__o__x");
positions[3] = Solver.TupleContainsPattern(tuple, "x__o__x__x");
if (positions[0] != -1) { tuple[positions[0] + 1].value = 1; }
if (positions[1] != -1) { tuple[positions[1] + 8].value = 1; }
if (positions[2] != -1) { tuple[positions[2] + 1].value = 0; }
if (positions[3] != -1) { tuple[positions[3] + 8].value = 0; }
}
#endregion
#region Far Siblings
// O____O -> OX__XO
private void FarSiblings(List<Field> tuple)
{
int[] indexes = Solver.GetIndexesOf(tuple, null);
if (indexes.Length != 4) { return; }
if ((indexes[0] + 3) != indexes[3]) { return; }
int? value;
if ((indexes[0] - 1) >= 0 && (indexes[3] + 1) < tuple.Count && tuple[indexes[0] - 1].value == tuple[indexes[3] + 1].value)
{
value = 1 - tuple[indexes[0] - 1].value;
int countO = Solver.CountElement(tuple, 0);
int countX = Solver.CountElement(tuple, 1);
if ((Solver.CountElement(tuple, (1 - value)) - Solver.CountElement(tuple, value)) == 2)
{
tuple[indexes[0]].value = 1 - tuple[indexes[0] - 1].value;
tuple[indexes[3]].value = 1 - tuple[indexes[0] - 1].value;
}
}
}
#endregion
}
}