-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku_Game.cpp
More file actions
309 lines (255 loc) · 6.56 KB
/
Sudoku_Game.cpp
File metadata and controls
309 lines (255 loc) · 6.56 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
#include"raylib.h"
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
using namespace std;
static const int APP_SCALE = 3; //Increase if too small on your display
static const int SCREEN_SIZE = APP_SCALE * 180;
static const int UI_BUFFER = SCREEN_SIZE/6;
static const int N = 9;
int gui(int qdoku[N][N], int doku[N][N])
{
InitWindow(SCREEN_SIZE, SCREEN_SIZE + UI_BUFFER, "Sudoku Game");
SetTargetFPS(60);
Color empty_color = WHITE;
Color filled_color = RAYWHITE;
Color cell_outline = LIGHTGRAY;
Color big_outline = DARKGRAY;
Color selected_box_color = RED;
Color selected_input_color = RED;
int rect_size = SCREEN_SIZE / N;
int draw_loc_x = NULL;
int draw_loc_y = NULL;
int text_shift_x = NULL;
int text_shift_y = NULL;
int ch_size = NULL;
int font_size = SCREEN_SIZE/18;
int mouse_x = NULL;
int mouse_y = NULL;
int selected_x = NULL;
int selected_y = NULL;
int immediate_input = NULL;
int key_input = NULL;
int no_mistakes = NULL;
int time = NULL;
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(empty_color);
time = (int)GetTime();
//Drawing grid and centering numbers
for (int i = 0; i < N; i++)
{
draw_loc_y = i * rect_size;
for (int j = 0; j < N; j++)
{
draw_loc_x = j * rect_size;
if(qdoku[i][j] == 0)
{
DrawRectangle(draw_loc_x, draw_loc_y, rect_size, rect_size, empty_color);
DrawRectangleLines(draw_loc_x, draw_loc_y, rect_size, rect_size, cell_outline);
}
else
{
DrawRectangle(draw_loc_x, draw_loc_y, rect_size, rect_size, filled_color);
DrawRectangleLines(draw_loc_x, draw_loc_y, rect_size, rect_size, cell_outline);
//Formatting vars
ch_size = MeasureText(TextFormat("%i", qdoku[i][j]), font_size);
text_shift_x = (rect_size / 2) - (ch_size / 2);
text_shift_y = (rect_size / 2) - (font_size / 2);
DrawText(TextFormat("%i", qdoku[i][j]), draw_loc_x + text_shift_x, draw_loc_y + text_shift_y, font_size, big_outline);
}
}
}
//Drawing 3x3 Dark outline
for (int i = 0; i < N/3 ; i++)
{
draw_loc_y = i * SCREEN_SIZE/3;
for (int j = 0; j < N / 3; j++)
{
draw_loc_x = j * SCREEN_SIZE/3;
DrawRectangleLines(draw_loc_x, draw_loc_y, SCREEN_SIZE/3, SCREEN_SIZE/3, big_outline);
}
}
//Getting mouse input
int cell = 0; //Fuck knows what this var does but we need it.
if (IsMouseButtonPressed(cell))
{
mouse_x = GetMouseX();
mouse_y = GetMouseY();
key_input = NULL;
}
// constraintes for the selection box
selected_x = (mouse_x / rect_size) * rect_size;
selected_y = (mouse_y / rect_size) * rect_size;
//If click is in bounds
if (mouse_y < SCREEN_SIZE && qdoku[mouse_y / rect_size][mouse_x / rect_size] == 0)
{
//Drawing a outline around the selected cell
DrawRectangleLines(selected_x, selected_y, rect_size, rect_size, selected_box_color);
//get input from keyboard
immediate_input = GetKeyPressed();
if (immediate_input != 0)
key_input = immediate_input - 48;
//Checking if the input is valid and making changes if it is
if (doku[mouse_y / rect_size][mouse_x / rect_size] == key_input)
qdoku[mouse_y / rect_size][mouse_x / rect_size] = key_input;
/*
if (key_input == 64 || key_input == 32)
time = time-(int)GetTime();
*/
else if (immediate_input != 0 && doku[mouse_y / rect_size][mouse_x / rect_size] != key_input)
no_mistakes++;
if (key_input > 0 && key_input <= 9)
{
ch_size = MeasureText(TextFormat("%i", key_input), font_size);
text_shift_x = (rect_size / 2) - (ch_size / 2);
text_shift_y = (rect_size / 2) - (font_size / 2);
DrawText(TextFormat("%i", key_input), selected_x + text_shift_x, selected_y + text_shift_y, font_size, selected_input_color);
}
}
//Timer
DrawText(TextFormat("Time Elapsed: %d", time), rect_size/2, SCREEN_SIZE + UI_BUFFER / 2 - font_size / 2, font_size, big_outline);
//Mistakes
DrawText(TextFormat("%d",no_mistakes), SCREEN_SIZE-rect_size/2- MeasureText(TextFormat("%i", no_mistakes), font_size) , SCREEN_SIZE + UI_BUFFER / 2 - font_size / 2, font_size, selected_input_color);
EndDrawing();
}
CloseWindow();
return 0;
}
int print(int doku[N][N]) //Prints to console
{
cout << endl << "=====================" << endl << endl;
for (int i = 0; i <= 8; i++)
{
for (int j = 0; j <= 8; j++)
{
cout << doku[i][j] << " ";
if (j == 2 || j == 5)
cout << " ";
}
cout << endl;
if (i == 2 || i == 5)
{
cout << endl;
}
}
cout << endl << "=====================" << endl << endl;
return 0;
}
bool empty_finder(int& r, int& c, int doku[9][9])//Looks for empty cells in the grid
{
for (r = 0; r < 9; r++)
{
for (c = 0; c < 9; c++)
{
if (doku[r][c] == 0)
{
return true;
}
}
}
return false;
}
bool check(int y, int x, int n, int doku[N][N]) // 1 -> The number can be inserted; else 0
{
for (int i = 0; i < N; i++)
{
if (n == doku[y][i]) // Column Checker
return false;
if (n == doku[i][x]) // Row Checker
return false;
}
//Square checker
int xrs = 0, xre = 0; //xrs -> x co-ordinate range start || yre -> co - ordinate range end
int yrs = 0, yre = 0; //yrs -> y co-ordinate range start || yre -> y co - ordinate range end
if (y >= 0 && y <= 2)
{
yrs = 0;
yre = 2;
}
else if (y > 2 && y <= 5)
{
yrs = 3;
yre = 5;
}
else if (y > 5 && y <= 8)
{
yrs = 6;
yre = 8;
}
if (x >= 0 && x <= 2)
{
xrs = 0;
xre = 2;
}
else if (x > 2 && x <= 5)
{
xrs = 3;
xre = 5;
}
else if (x > 5 && x <= 8)
{
xrs = 6;
xre = 8;
}
for (int j = yrs; j <= yre; j++)//Moving Vertically
{
for (int k = xrs; k <= xre; k++)//Move Horizontally to check
{
if (doku[j][k] == n) //Checking if the number exists
{
return false;
}
}
}
return true; //If it gets this far it can be inserted
}
bool solver(int doku[N][N])
{
int r, c;
if (empty_finder(r, c, doku) == false)
{
return true; //The sudoku is solved!
}
for (int num = 1; num <= 9; num++)
{
if (check(r, c, num, doku))
{
doku[r][c] = num;
if (solver(doku))
{
return true;
}
doku[r][c] = 0; //Wrong choice so fallback
}
}
return false;
}
int main()
{
int no_num = NULL;
char ch;
ifstream qbase;
qbase.open("big_grids.txt");
while (qbase >> ch) ++no_num;
qbase.clear();
srand(time(0));
int board_at = 81 * (rand() % (no_num / 81));
qbase.seekg(board_at);
int flag = 0;
int qdoku[N][N], doku[N][N];
while (flag!=81)
{
qbase >> ch;
qdoku[(flag/9)][(flag % 9)]=(int)ch-48;
doku[(flag / 9)][(flag % 9)] = (int)ch - 48;
++flag;
}
solver(qdoku);
print(qdoku);
gui(doku,qdoku);
qbase.close();
return 0;
}