-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSudoku.cs
323 lines (285 loc) · 8.54 KB
/
Sudoku.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
class Sudoku {
private static readonly Field[][] Board = new Field[9][];
private const int MinGeneratedFiels = 25;
private const int MaxGeneratedFiels = 80;
private static int step;
private static readonly Stopwatch _watch = new Stopwatch();
private class Field {
public int value;
public bool canChange = true;
}
private struct Cursor {
public static int x;
public static int y;
}
static void Main(string[] args) {
Console.ForegroundColor = ConsoleColor.Green;
Console.Title = "Sudoku";
Console.WriteLine("<---------------Sudoku---------------->");
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
Console.WriteLine("Press any key to start.\n");
Console.ReadKey();
Start();
}
private static void Start() {
step = 0;
Console.WriteLine("\bGenerating sudoku...");
GenerateBoard();
while (true) {
PrintBoard(true);
HandleInput();
}
}
private static void HandleInput() {
var key = Console.ReadKey();
if (char.IsNumber(key.KeyChar) && Board[Cursor.x][Cursor.y].canChange) {
Board[Cursor.x][Cursor.y].value = (int) char.GetNumericValue(key.KeyChar);
if (CheckForWin()) Win();
}
switch (key.Key) {
case ConsoleKey.Q:
Environment.Exit(0);
break;
case ConsoleKey.Delete:
if (Board[Cursor.x][Cursor.y].canChange) Board[Cursor.x][Cursor.y].value = 0;
break;
case ConsoleKey.S:
AutoSolve();
break;
case ConsoleKey.UpArrow:
Cursor.x--;
if (Cursor.x < 0) Cursor.x = 0;
break;
case ConsoleKey.DownArrow:
Cursor.x++;
if (Cursor.x > 8) Cursor.x = 8;
break;
case ConsoleKey.LeftArrow:
Cursor.y--;
if (Cursor.y < 0) Cursor.y = 0;
break;
case ConsoleKey.RightArrow:
Cursor.y++;
if (Cursor.y > 8) Cursor.y = 8;
break;
}
}
private static void AutoSolve() {
Cursor.x = 0;
Cursor.y = 0;
_watch.Reset();
_watch.Start();
Console.WriteLine("\bSolving...");
if (!Solve()) {
Console.WriteLine("Solution not found.");
QuitOrRestart();
}
_watch.Stop();
PrintBoard(false);
Console.WriteLine("\nSolved. Steps: " + step);
Console.WriteLine("Time (ms): " + _watch.ElapsedMilliseconds);
QuitOrRestart();
}
private static bool Solve() {
if (_watch.ElapsedMilliseconds > 1000) {
return false;
}
if (CheckForWin()) return true;
var emptySpotsCords = GetEmptySpotsCord(Board);
Cursor.x = emptySpotsCords.Item1;
Cursor.y = emptySpotsCords.Item2;
if (Equals(new Tuple<int, int>(9, 9), emptySpotsCords)) {
return true;
}
for (int num = 1; num <= 9; num++) {
if (IsNumberCorrect(emptySpotsCords.Item1, emptySpotsCords.Item2, num)) {
Board[emptySpotsCords.Item1][emptySpotsCords.Item2].value = num;
step++;
if (Solve()) {
return true;
}
Board[emptySpotsCords.Item1][emptySpotsCords.Item2].value = 0;
}
}
return false;
}
private static Tuple<int, int> GetEmptySpotsCord(Field[][] board) {
for (var i = 0; i < board.Length; i++) {
for (var j = 0; j < board.Length; j++) {
if (board[i][j].canChange && board[i][j].value == 0) {
return new Tuple<int, int>(i, j);
}
}
}
return new Tuple<int, int>(9, 9);
}
private static bool IsNumberCorrect(int x, int y, int num) {
// Check column
foreach (var field in Board[x]) {
if (field.value == num) {
return false;
}
}
foreach (var row in Board) {
if (row[y].value == num) {
return false;
}
}
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (Board[i + (3 * (x / 3))][j + (3 * (y / 3))].value == num) {
return false;
}
}
}
return true;
}
private static bool CheckForWin() {
return Board.SelectMany(line => line).All(field => field.value != 0) && IsBoardCorrect();
}
private static void Win() {
PrintBoard(false);
Console.WriteLine(@"
__ __ __ __ _ _ _
\ \ / / \ \ / / | | | |
\ \_/ /__ _ _ \ \ /\ / /__ _ __ | | | |
\ / _ \| | | | \ \/ \/ / _ \| '_ \ | | | |
| | (_) | |_| | \ /\ / (_) | | | | |_|_|_|
|_|\___/ \__,_| \/ \/ \___/|_| |_| (_|_|_)
");
QuitOrRestart();
}
private static void QuitOrRestart() {
Console.WriteLine("Press Enter to play again or Q to quit.");
while (true) {
var key = Console.ReadKey().Key;
Console.Write("\b");
switch (key) {
case ConsoleKey.Q:
Environment.Exit(0);
break;
case ConsoleKey.Enter:
Start();
break;
}
}
}
private static void PrintBoard(bool showCursor) {
Console.Clear();
PrintTitle();
for (var i = 0; i < Board.Length; i++) {
if (i % 3 == 0) {
Console.WriteLine("+---+---+---+");
}
for (var j = 0; j < Board[0].Length; j++) {
if (j % 3 == 0) {
Console.Write('|');
}
if (i == Cursor.x && j == Cursor.y && showCursor) {
Console.ForegroundColor = ConsoleColor.White;
Console.Write('█');
} else if (Board[i][j].value > 0) {
Console.ForegroundColor = !Board[i][j].canChange ? ConsoleColor.Red : ConsoleColor.White;
Console.Write(Board[i][j].value);
} else {
Console.Write(' ');
}
Console.ForegroundColor = ConsoleColor.Green;
}
Console.Write("|\n");
}
Console.WriteLine("+---+---+---+");
Console.WriteLine(
"\nHELP Arrows - controls, 1-9 - Place number, 0 or Del - to remove number, S - autosolve, Q - quit");
}
private static void PrintTitle() {
Console.WriteLine(" ╔══════╗");
Console.WriteLine(" ║SUDOKU║");
Console.WriteLine(" ╚══════╝");
}
private static void GenerateBoard() {
step = 0;
for (var i = 0; i < Board.Length; i++) {
Board[i] = new Field[9];
for (var j = 0; j < Board.Length; j++) {
Board[i][j] = new Field();
}
}
var random = new Random();
var randomRange = random.Next(MinGeneratedFiels, MaxGeneratedFiels);
for (var i = 0; i < randomRange; i++) {
var x = random.Next(0, 9);
var y = random.Next(0, 9);
Board[x][y].value = random.Next(1, 9);
Board[x][y].canChange = false;
if (!IsBoardCorrect()) {
Board[x][y].value = 0;
i--;
Board[x][y].canChange = true;
}
}
_watch.Restart();
if (Solve()) {
foreach (var row in Board) {
foreach (var field in row) {
if (field.canChange) {
field.value = 0;
}
}
}
_watch.Stop();
} else {
_watch.Stop();
GenerateBoard();
}
}
private static bool IsBoardCorrect() {
List<int> numberList;
// Check all rows
foreach (var row in Board) {
numberList = GenerateNumbeList();
foreach (var field in row) {
if (numberList.Contains(field.value)) numberList.Remove(field.value);
else if (field.value > 0) {
return false;
}
}
}
//Check all columns
for (var i = 0; i < Board.Length; i++) {
numberList = GenerateNumbeList();
for (var j = 0; j < Board.Length; j++) {
if (numberList.Contains(Board[j][i].value)) numberList.Remove(Board[j][i].value);
else if (Board[j][i].value > 0) {
return false;
}
}
}
//Check all squares
for (var squareX = 0; squareX < 3; squareX++) {
for (var squareY = 0; squareY < 3; squareY++) {
numberList = GenerateNumbeList();
for (var rowInSquare = 0; rowInSquare < 3; rowInSquare++) {
for (var columnInSquare = 0; columnInSquare < 3; columnInSquare++) {
if (numberList.Contains(Board[squareX * 3 + rowInSquare][squareY * 3 + columnInSquare].value))
numberList.Remove(Board[squareX * 3 + rowInSquare][squareY * 3 + columnInSquare].value);
else if (Board[squareX * 3 + rowInSquare][squareY * 3 + columnInSquare].value > 0) return false;
}
}
}
}
return true;
}
private static List<int> GenerateNumbeList() {
var numberList = new List<int>();
for (var i = 1; i < 10; i++) {
numberList.Add(i);
}
return numberList;
}
}