-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeBreakerAlgorithms.cs
More file actions
325 lines (284 loc) · 11.7 KB
/
CodeBreakerAlgorithms.cs
File metadata and controls
325 lines (284 loc) · 11.7 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System.Runtime.CompilerServices;
using Codebreaker.GameAPIs.Client.Models;
namespace CodeBreaker.Bot;
public record struct KeyPegWithFlag(int Value, bool Used);
public static class CodeBreakerAlgorithms
{
// definitions to mask the different pegs
private const int C0001 = 0b_111111;
private const int C0010 = 0b_111111_000000;
private const int C0100 = 0b_111111_000000_000000;
private const int C1000 = 0b_111111_000000_000000_000000;
// Convert the int representation of pegs to an array of color names
public static string[] IntToColors(this int value, GameType gameType, Dictionary<int, string>? colorNames)
{
return gameType switch
{
GameType.Game6x4 => IntToColors6x4(value, colorNames),
GameType.Game8x5 => IntToColors8x5(value, colorNames),
GameType.Game5x5x4 => IntToColors5x5x4(value, colorNames),
GameType.Game5x3 => IntToColors5x3(value, colorNames),
_ => IntToColors6x4(value, colorNames)
};
}
private static string[] IntToColors6x4(int value, Dictionary<int, string>? colorNames)
{
int i1 = (value >> 0) & 0b111111;
int i2 = (value >> 6) & 0b111111;
int i3 = (value >> 12) & 0b111111;
int i4 = (value >> 18) & 0b111111;
string[] colorNamesArray =
[
colorNames?[i4] ?? $"Unknown{i4}",
colorNames?[i3] ?? $"Unknown{i3}",
colorNames?[i2] ?? $"Unknown{i2}",
colorNames?[i1] ?? $"Unknown{i1}"
];
return colorNamesArray;
}
private static string[] IntToColors8x5(int value, Dictionary<int, string>? colorNames)
{
int i1 = (value >> 0) & 0b111111;
int i2 = (value >> 6) & 0b111111;
int i3 = (value >> 12) & 0b111111;
int i4 = (value >> 18) & 0b111111;
int i5 = (value >> 24) & 0b111111;
string[] colorNamesArray =
[
colorNames?[i5] ?? $"Unknown{i5}",
colorNames?[i4] ?? $"Unknown{i4}",
colorNames?[i3] ?? $"Unknown{i3}",
colorNames?[i2] ?? $"Unknown{i2}",
colorNames?[i1] ?? $"Unknown{i1}"
];
return colorNamesArray;
}
private static string[] IntToColors5x5x4(int value, Dictionary<int, string>? colorNames)
{
int i1 = (value >> 0) & 0b111111;
int i2 = (value >> 6) & 0b111111;
int i3 = (value >> 12) & 0b111111;
int i4 = (value >> 18) & 0b111111;
string[] colorNamesArray =
[
colorNames?[i4] ?? $"Unknown{i4}",
colorNames?[i3] ?? $"Unknown{i3}",
colorNames?[i2] ?? $"Unknown{i2}",
colorNames?[i1] ?? $"Unknown{i1}"
];
return colorNamesArray;
}
private static string[] IntToColors5x3(int value, Dictionary<int, string>? colorNames)
{
int i1 = (value >> 0) & 0b111111;
int i2 = (value >> 6) & 0b111111;
int i3 = (value >> 12) & 0b111111;
string[] colorNamesArray =
[
colorNames?[i3] ?? $"Unknown{i3}",
colorNames?[i2] ?? $"Unknown{i2}",
colorNames?[i1] ?? $"Unknown{i1}"
];
return colorNamesArray;
}
/// <summary>
/// Reduces the possible values based on the black matches with the selection
/// </summary>
/// <param name="values">The list of possible moves</param>
/// <param name="gameType">The type of game being played</param>
/// <param name="blackHits">The number of black hits with the selection</param>
/// <param name="selection">The key pegs of the selected move</param>
/// <returns>The remaining possible moves</returns>
/// <exception cref="ArgumentException"></exception>
public static List<int> HandleBlackMatches(this IList<int> values, GameType gameType, int blackHits, int selection)
{
int maxHits = GetFieldsCount(gameType) - 1;
if (blackHits is < 1 || blackHits > maxHits)
{
throw new ArgumentException($"invalid argument - hits need to be between 1 and {maxHits}");
}
bool IsMatch(int value, int blackhits, int selection)
{
int fieldsCount = GetFieldsCount(gameType);
int bitsPerField = GetBitsPerField(gameType);
int mask = (1 << bitsPerField) - 1;
int matches = 0;
for (int field = 0; field < fieldsCount; field++)
{
int shift = field * bitsPerField;
int selectionField = (selection >> shift) & mask;
int valueField = (value >> shift) & mask;
if (valueField == selectionField)
{
matches++;
}
}
return (matches == blackhits);
}
List<int> result = new(capacity: values.Count);
foreach (int value in values)
{
if (IsMatch(value, blackHits, selection))
{
result.Add(value);
}
}
return result;
}
/// <summary>
/// Reduces the possible values based on the white matches with the selection
/// </summary>
/// <param name="values">The possible values</param>
/// <param name="gameType">The type of game being played</param>
/// <param name="whiteHits">The number of white hits with the selection</param>
/// <param name="selection">The selected pegs</param>
/// <returns>The remaining possbile values</returns>
public static List<int> HandleWhiteMatches(this IList<int> values, GameType gameType, int whiteHits, int selection)
{
List<int> newValues = new(values.Count);
int fieldsCount = GetFieldsCount(gameType);
foreach (int value in values)
{
// need to have clean selections with every run
var selections = new KeyPegWithFlag[fieldsCount];
for (int i = 0; i < fieldsCount; i++)
{
selections[i] = new KeyPegWithFlag(selection.SelectPeg(gameType, i), false);
}
var matches = new KeyPegWithFlag[fieldsCount];
for (int i = 0; i < fieldsCount; i++)
{
matches[i] = new KeyPegWithFlag(value.SelectPeg(gameType, i), false);
}
int matchCount = 0;
for (int i = 0; i < fieldsCount; i++)
{
for (int j = 0; j < fieldsCount; j++)
{
if (!matches[i].Used && !selections[j].Used && matches[i].Value == selections[j].Value)
{
matchCount++;
selections[j] = selections[j] with { Used = true };
matches[i] = matches[i] with { Used = true };
}
}
}
if (matchCount == whiteHits)
{
newValues.Add(value);
}
}
return newValues;
}
/// <summary>
/// Reduces the possible values based on the blue matches (partial matches for Game5x5x4) with the selection
/// </summary>
/// <param name="values">The possible values</param>
/// <param name="gameType">The type of game being played</param>
/// <param name="blueHits">The number of blue hits with the selection</param>
/// <param name="selection">The selected pegs</param>
/// <returns>The remaining possbile values</returns>
public static List<int> HandleBlueMatches(this IList<int> values, GameType gameType, int blueHits, int selection)
{
// Blue matches only apply to Game5x5x4
if (gameType != GameType.Game5x5x4)
{
return values.ToList(); // No filtering needed for other game types
}
List<int> newValues = new(values.Count);
int fieldsCount = GetFieldsCount(gameType);
foreach (int value in values)
{
// For Game5x5x4, we need to count partial matches
// This is a simplified implementation that counts blue-like matches
// In a real implementation, this would need to understand shape+color combinations
// For now, we'll do a basic filtering that reduces possibilities
int partialMatches = 0;
for (int i = 0; i < fieldsCount; i++)
{
int selectionField = selection.SelectPeg(gameType, i);
int valueField = value.SelectPeg(gameType, i);
// This is a simplified blue match check
// In reality, blue matches are more complex for shape+color combinations
if (valueField != selectionField && (valueField & selectionField) != 0)
{
partialMatches++;
}
}
if (partialMatches == blueHits)
{
newValues.Add(value);
}
}
return newValues;
}
/// </summary>
/// <param name="values">The possible values</param>
/// <param name="gameType">The type of game being played</param>
/// <param name="selection">The selected pegs</param>
/// <returns>The remaining possbile values</returns>
public static List<int> HandleNoMatches(this IList<int> values, GameType gameType, int selection)
{
bool Contains(int[] selections, int value)
{
int fieldsCount = GetFieldsCount(gameType);
for (int i = 0; i < fieldsCount; i++)
{
if (selections.Contains(value.SelectPeg(gameType, i)))
{
return true;
}
}
return false;
}
List<int> newValues = new(values.Count);
int fieldsCount = GetFieldsCount(gameType);
int[] selections = Enumerable.Range(0, fieldsCount)
.Select(i => selection.SelectPeg(gameType, i))
.ToArray();
foreach (int value in values)
{
if (!Contains(selections, value))
{
newValues.Add(value);
}
}
return newValues;
}
/// <summary>
/// Get the int representation of one peg from the int representaiton of pegs
/// </summary>
/// <param name="code">The int value representing the pegs</param>
/// <param name="gameType">The type of game being played</param>
/// <param name="pegNumber">The peg number to retrieve from the int representation</param>
/// <returns>The int value of the selected peg</returns>
/// <exception cref="InvalidOperationException"></exception>
public static int SelectPeg(this int code, GameType gameType, int pegNumber)
{
int bitsPerField = GetBitsPerField(gameType);
int fieldsCount = GetFieldsCount(gameType);
int mask = (1 << bitsPerField) - 1;
if (pegNumber < 0 || pegNumber >= fieldsCount)
throw new InvalidOperationException($"invalid peg number {pegNumber}");
int shift = pegNumber * bitsPerField;
return (code >> shift) & mask;
}
private static int GetFieldsCount(GameType gameType) =>
gameType switch
{
GameType.Game6x4 => 4,
GameType.Game8x5 => 5,
GameType.Game5x5x4 => 4,
GameType.Game5x3 => 3,
_ => 4
};
private static int GetBitsPerField(GameType gameType) =>
gameType switch
{
GameType.Game6x4 => 6, // 6 bits for up to 64 values (using 6)
GameType.Game8x5 => 6, // 6 bits for up to 64 values (using 8)
GameType.Game5x5x4 => 6, // 6 bits for up to 64 values (using 25, but limited per position)
GameType.Game5x3 => 6, // 6 bits for up to 64 values (using 5)
_ => 6
};
}