-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
272 lines (221 loc) · 8 KB
/
Game.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Sudoku
{
class Game
{
static Random _random = new Random();
static HashSet<string> lookup = new HashSet<string>();
static char[] digits = new char[]
{
'1','2','3','4','5','6','7','8','9'
};
public static void createSudokuGrid(char[,] mainGrid)
{
SolveGrid(mainGrid, shuffle:true);
lookup.Clear();
FillDots(mainGrid);
ShowSolvedGrid(mainGrid);
var res = FileHandler.PersistSudokuGrid(mainGrid);
EmptyGrid(mainGrid);
}
private static void EmptyGrid(char[,] mainGrid)
{
for (int row = 0; row < mainGrid.GetLength(0); row++)
{
for (int col = 0; col < mainGrid.GetLength(1); col++)
{
mainGrid[row, col] = '.';
}
}
}
private static void FillDots(char[,] mainGrid)
{
var mainGridSize = mainGrid.GetLength(0);
for (int row = 0; row < mainGridSize; row++)
{
int[] digitsRand = Enumerable
.Repeat(0, 30)
.Select(i => _random.Next(0, 9)).Distinct().Take(_random.Next(3, 6))
.ToArray();
foreach(var col in digitsRand)
{
mainGrid[row, col] = '.';
}
}
}
public static void puzzleSolver(char[,] mainGrid)
{
var isGridValid = CheckGridValidity(mainGrid);
if (!isGridValid)
{
UI.ShowMsg("Given SUDOKU grid is not valid or It is already Solved!");
return;
}
UI.ShowMsg("Given SUDOKU grid seems to be a valid one, solving...");
Classify();
SolveGrid(mainGrid);
lookup.Clear();
ShowSolvedGrid(mainGrid);
var res = FileHandler.PersistSudokuGrid(mainGrid);
}
private static bool SolveGrid(char[,] mainGrid, int row = 0, int col = 0, bool shuffle = false)
{
var mainGridSize = mainGrid.GetLength(0);
var subGridSize = 3;
var subGridNumber = (col / subGridSize) + (subGridSize * (row / subGridSize));
var isRowEnd = row > mainGridSize - 1;
if (isRowEnd)
{
return true;
}
var isColumnEnd = col >= mainGridSize - 1;
var isCurrentCellFilled = mainGrid[row, col] != '.';
if (isCurrentCellFilled)
{
return SolveGrid(mainGrid, isColumnEnd ? row + 1 : row, isColumnEnd ? 0 : col + 1);
}
var validDigits = FetchValidNumbers(row, col);
if (shuffle)
{
Shuffle(validDigits);
}
foreach (var digit in validDigits)
{
var subGridValue = $"subgrid{subGridNumber}->{digit}";
var rowValue = $"row{row}->{digit}";
var colValue = $"col{col}->{digit}";
if (lookup.Add(subGridValue) && lookup.Add(rowValue) && lookup.Add(colValue))
{
mainGrid[row, col] = digit;
var pathResult = SolveGrid(mainGrid, isColumnEnd ? row + 1 : row, isColumnEnd ? 0 : col + 1);
if (pathResult)
{
return true;
}
lookup.Remove(subGridValue);
lookup.Remove(rowValue);
lookup.Remove(colValue);
mainGrid[row, col] = '.';
}
}
return false;
}
private static char[] FetchValidNumbers(int row, int col)
{
var setOfValidDigits = new HashSet<char>(digits);
var subGridSize = 3;
var subGridNumber = (col / subGridSize) + (subGridSize * (row / subGridSize));
foreach (var digit in digits)
{
var subGridValue = $"subgrid{subGridNumber}->{digit}";
var rowValue = $"row{row}->{digit}";
var colValue = $"col{col}->{digit}";
if(lookup.Contains(subGridValue) || lookup.Contains(rowValue) || lookup.Contains(colValue)){
setOfValidDigits.Remove(digit);
}
}
return setOfValidDigits.ToArray();
}
private static bool CheckGridValidity(char[,] mainGrid, int row = 0, int col = 0)
{
var mainGridSize = mainGrid.GetLength(0);
var subGridSize = 3;
var subGridNumber = (col / subGridSize) + (subGridSize * (row / subGridSize));
if (mainGrid[row, col] != '.')
{
if (!lookup.Add($"subgrid{subGridNumber}->{mainGrid[row, col]}"))
{
return false;
}
if (!lookup.Add($"row{row}->{mainGrid[row, col]}"))
{
return false;
}
if (!lookup.Add($"col{col}->{mainGrid[row, col]}"))
{
return false;
}
}
col += 1;
var isColumnEnd = col > mainGridSize - 1;
if (isColumnEnd)
{
col = 0;
row += 1;
}
var isRowEnd = row > mainGridSize - 1;
if (isRowEnd)
{
return true;
}
return CheckGridValidity(mainGrid, row, col);
}
private static void Classify()
{
var totalGridStrength = 1;
for(int subGridDigit = 0; subGridDigit < 9; subGridDigit++)
{
var eachSubGridDigitsCount = lookup.Select(item => item).Where(item => item.StartsWith($"subgrid{subGridDigit}")).Count();
totalGridStrength *= eachSubGridDigitsCount;
}
if(totalGridStrength < 5000)
{
UI.ShowMsg($"Given SUDOKU grid is classified as SAMURAI");
return;
}
if (totalGridStrength < 10000)
{
UI.ShowMsg($"Given SUDOKU grid is classified as HARD");
return;
}
if (totalGridStrength < 30000)
{
UI.ShowMsg($"Given SUDOKU grid is classified as MEDIUM");
return;
}
UI.ShowMsg($"Given SUDOKU grid is classified as EASY");
}
private static bool ShowSolvedGrid(char[,] mainGrid, int row = 0, int col = 0)
{
if (row==0 && col == 0)
{
UI.ShowHorizontalDivider();
UI.NewLineWithTab();
}
var mainGridSize = mainGrid.GetLength(0);
Console.Write($"{mainGrid[row, col]}\t ");
col += 1;
var isColumnEnd = col > mainGridSize - 1;
if (isColumnEnd)
{
UI.NewLineWithTab();
col = 0;
row += 1;
}
var isRowEnd = row > mainGridSize - 1;
if (isRowEnd)
{
UI.NewLine();
UI.ShowHorizontalDivider();
return true;
}
return ShowSolvedGrid(mainGrid, row, col);
}
private static char[] Shuffle(char[] charArray)
{
char[] shuffledArray = new char[charArray.Length];
int rndNo;
Random rnd = new Random();
for (int i = charArray.Length; i >= 1; i--)
{
rndNo = rnd.Next(1, i + 1) - 1;
shuffledArray[i - 1] = charArray[rndNo];
charArray[rndNo] = charArray[i - 1];
}
return shuffledArray;
}
}
}