-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.cs
227 lines (196 loc) · 6.92 KB
/
Solver.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Binoxxo_Solver
{
class Solver
{
private static Binoxxo binoxxo;
private readonly List<Thread> threads = new List<Thread>();
private readonly int lifespan = 1000 * 5;
public Solver(Binoxxo binoxxo)
{
Solver.binoxxo = binoxxo;
}
public void Solve()
{
Console.Clear();
Console.WriteLine("Trying to solve the following Binoxxo:\n");
Thread painter = new Thread(new Painter().ContinuousPaintBinoxxo);
painter.Start(binoxxo);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Worker worker = new Worker();
threads.Add(new Thread(worker.Run));
threads.Add(new Thread(worker.Run));
threads[0].IsBackground = true;
threads[1].IsBackground = true;
threads[0].Start(binoxxo.GetAllRows());
threads[1].Start(binoxxo.GetAllColumns());
WaitForAllThreads(lifespan);
painter.Abort();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Console.Clear();
Console.WriteLine("\nRunTime {0}.{1:D3} Seconds", ts.Seconds, ts.Milliseconds);
if (ValidateBinoxxo())
{
Console.WriteLine("Solved correctly\n");
}
else
{
Console.WriteLine("I am way too dumb to solve this binoxxo\n");
}
Console.WriteLine(binoxxo.PrintBinoxxo());
}
private void WaitForAllThreads(int lifespan)
{
foreach (Thread thread in threads)
{
Stopwatch timeout = Stopwatch.StartNew();
if (!thread.Join(lifespan)) { break; }
timeout.Stop();
lifespan -= (int)timeout.ElapsedMilliseconds;
}
}
#region Support Methods
public static List<List<Field>> GetAllRows()
{
return binoxxo.GetAllRows();
}
public static List<List<Field>> GetAllColumns()
{
return binoxxo.GetAllColumns();
}
public static int CompareLines(List<Field> list1, List<Field> list2)
{
return binoxxo.CompareLines(list1, list2);
}
public static int CountElement(List<Field> tuple, int? countMe)
{
return tuple.Count(e => e.value == countMe);
}
public static int TupleContainsPattern(List<Field> tuple, string pattern)
{
char[] patternArray = pattern.ToArray();
int?[] patternIntArray = new int?[patternArray.Length];
for (int i = 0; i < patternIntArray.Length; i++)
{
if (patternArray[i].Equals(' ') || patternArray[i].Equals('_'))
{
patternIntArray[i] = null;
}
else if (patternArray[i].Equals('0') || patternArray[i].Equals('o') || patternArray[i].Equals('O'))
{
patternIntArray[i] = 0;
}
else if (patternArray[i].Equals('1') || patternArray[i].Equals('x') || patternArray[i].Equals('X'))
{
patternIntArray[i] = 1;
}
}
int arrayPos = 0;
for (int i = 0; i < tuple.Count; i++)
{
if (tuple[i].value == patternIntArray[arrayPos])
{
arrayPos++;
}
if (arrayPos == patternIntArray.Length)
{
return i - patternIntArray.Length + 1;
}
}
return -1;
}
public static int GetNullMargin(List<Field> tuple, int nullCount)
{
int margin = 0;
int count = 0;
foreach (Field f in tuple)
{
if (f.value == null || margin > 0) { margin++; }
if (f.value == null) { count++; }
if (count == nullCount) { break; }
}
return margin;
}
public static int[] GetIndexesOf(List<Field> tuple, int? value)
{
int[] indexes = new int[CountElement(tuple, value)];
int count = 0;
for (int i = 0; i < tuple.Count; i++)
{
if (tuple[i].value == value)
{
indexes[count++] = i;
}
}
return indexes;
}
#endregion
#region Validation Methods
public static bool IsSolved()
{
for (int i = 0; i < Math.Pow(binoxxo.size, 2); i++)
{
if (binoxxo.Get(i).value == null) { return false; }
}
return true;
}
public static bool ValidateBinoxxo()
{
if (!IsSolved()) { return false; }
List<List<Field>> rows = binoxxo.GetAllRows();
List<List<Field>> columns = binoxxo.GetAllColumns();
// Check uniqueness of each row and column
for (int i = 0; i < rows.Count; i++)
{
for (int j = i + 1; j < rows.Count; j++)
{
if (rows[i].SequenceEqual(rows[j])) { return false; }
}
foreach (List<Field> column in columns)
{
if (rows[i].SequenceEqual(column)) { return false; }
}
}
// Check for other rules
int requiredCount = binoxxo.size / 2;
foreach (List<Field> row in rows)
{
// Check for equal distribution of X and O
int count = CountElement(row, 0);
if (count != requiredCount) { return false; }
// Check for pairs
int countO = 0;
int countX = 0;
foreach (Field l in row)
{
countO = l.value == 0 ? ++countO : 0;
countX = l.value == 1 ? ++countX : 0;
if (countO > 2 || countX > 2) { return false; }
}
}
foreach (List<Field> column in columns)
{
// Check for equal distribution of X and O
int count = CountElement(column, 0);
if (count != requiredCount) { return false; }
// Check for pairs
int countO = 0;
int countX = 0;
foreach (Field l in column)
{
countO = l.value == 0 ? ++countO : 0;
countX = l.value == 1 ? ++countX : 0;
if (countO > 2 || countX > 2) { return false; }
}
}
return true;
}
#endregion
}
}