-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
382 lines (311 loc) · 7.39 KB
/
main.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int screenWidth = 630, screenHeight = 630;
#define MapSize 9
#define StaticNum 20 // number of static digits during generation
GLFWwindow *window;
int global_x; // x coord cursor
int global_y; // y coord cursor
struct Map
{
bool staticCell; // cell that contains a static number
int cell; // cell that contains a simple number
};
Map map[MapSize][MapSize]; // map in matrix view
bool checkWin; // if true, the player wins
int searchSection(int coord) // знаходить границю секції певного числа
{
coord++;
float temp = coord / (float) 3;
if(temp <= 1) return 2;
else if(temp > 1 && temp <= 2) return 5;
else if(temp > 2) return 8;
return 0;
}
bool Detect(const int &num, const int &i, const int &j) // checking the intersection of duplicate numbers in a row and column
{
if(num != 0)
{
for(int k = 0; k < MapSize; k++)
{
if(num == map[k][j].cell && k != i) return true; // horizontal check
if(num == map[i][k].cell && k != j) return true; // vertical check
}
}
return false;
}
bool checkCells() // checking whether all the numbers are arranged correctly
{
for(int i = 0; i < MapSize; i++)
{
for(int j = 0; j < MapSize; j++)
{
int num = map[i][j].cell;
return Detect(num, i, j);
}
}
return false;
}
bool checkAmount() // check if the entire field is filled with numbers
{
int count = 0;
for(int i = 0; i < MapSize; i++)
{
for(int j = 0; j < MapSize; j++)
{
if(map[i][j].cell != 0)
{
count++ ;
}
}
}
return 81 - count == 0;
}
bool checkSection(const int &num, const int &lim_x, const int &lim_y) // searches for the boundaries of the section in which the number is located
{
for(int i = lim_x - 2; i <= lim_x; i++)
{
for(int j = lim_y - 2; j <= lim_y; j++)
{
if(num == map[i][j].cell)
{
return true;
}
}
}
return false;
}
void NewGame()
{
checkWin = false;
for(int i = 0; i < MapSize; i++) // clear field
{
for(int j = 0; j < MapSize; j++)
{
map[i][j].cell = 0;
map[i][j].staticCell = false;
}
}
for(int i = 0; i < StaticNum; i++) // generate of static numbers on the filed
{
int rand_x = rand()% MapSize;
int rand_y = rand()% MapSize;
if(map[rand_x][rand_y].cell == 0)
{
// look for the boundaries of the section in which the number is located
int lim_x = searchSection(rand_x);
int lim_y = searchSection(rand_y);
int rand_num;
do
{
rand_num = rand()% 9;
}
while(checkSection(rand_num, lim_x, lim_y)
|| Detect(rand_num, rand_x, rand_y)); // checking if the random satisfies the conditions
map[rand_x][rand_y].cell = rand_num;
map[rand_x][rand_y].staticCell = true; // indicate that the number is constant
}
else // otherwise, do an additional iteration
{
i--;
}
}
}
void drawLine(float x1, float y1, float x2, float y2) // draw line)
{
glVertex2f(x1,y1);
glVertex2f(x2,y2);
}
void showCount(int num) // draw digits
{
switch(num)
{
case 1: // yellow
glColor3f(1,1,0);
break;
case 2: // green
glColor3f(0,1,0);
break;
case 3: // red
glColor3f(1,0,0);
break;
case 4: // azure
glColor3f(0,0,1);
break;
case 5: // blue
glColor3f(0,1,1);
break;
case 6: // pink
glColor3f(1,0,1);
break;
case 7: // black
glColor3f(0,0,0);
break;
case 8: // light-azure
glColor3f(0,0.5,1);
break;
case 9: // orange
glColor3f(1,0.65,0);
break;
}
glPushMatrix();
glLineWidth(3);
glBegin(GL_LINES);
if((num != 1) && (num != 4)) drawLine(0.3,0.85,0.7,0.85);
if((num != 0) && (num != 1) && (num != 7)) drawLine(0.3,0.5,0.7,0.5);
if((num != 1) && (num != 4) && (num != 7)) drawLine(0.3,0.15,0.7,0.15);
if((num != 5) && (num != 6)) drawLine(0.7,0.5,0.7,0.85);
if(num != 2) drawLine(0.7,0.5,0.7,0.15);
if((num != 1) && (num != 2) && (num != 3) && (num != 7)) drawLine(0.3,0.5,0.3,0.85);
if((num == 0) || (num == 2) || (num == 6) || (num == 8)) drawLine(0.3,0.5,0.3,0.15);
glEnd();
glPopMatrix();
}
void setField(int x, int y) // selection cursor
{
glPushMatrix();
glTranslatef(x, y, 0);
glLineWidth(4);
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
drawLine(0.1, 0.1, 0.1, 0.9);
drawLine(0.9, 0.9, 0.9, 0.1);
glEnd();
glPopMatrix();
}
void emptyField() // draw field
{
glPushMatrix();
glTranslatef(0.1,0.1,0);
glScalef(0.8,0.8,0);
glBegin(GL_POLYGON);
drawLine(0, 0, 0, 1);
drawLine(1, 1, 1, 0);
glEnd();
glPopMatrix();
}
void Cells() // draw cell
{
glPushMatrix();
glLineWidth(3);
glColor3f(0,0,0);
for(int i = 0; i < 4; i++)
{
glTranslatef(3,0,0);
glBegin(GL_LINES);
drawLine(0,0,0,9);
glEnd();
if(i == 1)
{
glRotatef(90,0,0,1);
glTranslatef(0,-3,0);
}
}
glPopMatrix();
}
void setCell(const int &num) // sets a digit to a specific cell
{
if(map[global_x][global_y].staticCell == false) map[global_x][global_y].cell = num;
}
void Keyboard(GLFWwindow *window, int key, int scode, int action, int mode) // keystroke processing
{
if(key == GLFW_KEY_1 && action == GLFW_PRESS) setCell(1);
else if(key == GLFW_KEY_2 && action == GLFW_PRESS) setCell(2);
else if(key == GLFW_KEY_3 && action == GLFW_PRESS) setCell(3);
else if(key == GLFW_KEY_4 && action == GLFW_PRESS) setCell(4);
else if(key == GLFW_KEY_5 && action == GLFW_PRESS) setCell(5);
else if(key == GLFW_KEY_6 && action == GLFW_PRESS) setCell(6);
else if(key == GLFW_KEY_7 && action == GLFW_PRESS) setCell(7);
else if(key == GLFW_KEY_8 && action == GLFW_PRESS) setCell(8);
else if(key == GLFW_KEY_9 && action == GLFW_PRESS) setCell(9);
if(key == GLFW_KEY_SPACE && action == GLFW_PRESS) NewGame();
if(key == GLFW_KEY_F && action == GLFW_PRESS)
{
for(int j = 0; j < 9; j++)
{
for(int i = 0; i < 9; i++)
{
map[i][j].cell = 0;
map[i][j].staticCell = false;
}
}
}
}
void Draw() // main draw
{
for(int i = 0; i < MapSize; i++)
{
for(int j = 0; j < MapSize; j++)
{
if(checkWin)
{
glColor3f(0.5,1,0.5);
}
else
{
if(map[i][j].staticCell) glColor3f(0.39,0.39,0.39);
else glColor3f(0.5,0.5,0.5);
}
glPushMatrix();
glTranslatef(i,j,0);
emptyField();
if(map[i][j].cell > 0)
{
showCount(map[i][j].cell);
}
if(checkAmount() && !checkCells())
{
cout << "You Win!" << endl;
checkWin = true;
}
glPopMatrix();
}
}
setField(global_x, global_y);
Cells();
}
void getPos(GLFWwindow *window, double Xpos, double Ypos) // get cursor coords
{
global_x = int(Xpos/(screenWidth/MapSize));
global_y = int((MapSize ) - Ypos/(screenHeight/MapSize));
//cout << "x = " << global_x << " y = " << global_y << endl;
}
int main()
{
srand(time(NULL));
if(!glfwInit())
{
cout << "Failed to init library";
return -1;
}
window = glfwCreateWindow(screenWidth, screenHeight, "Sudoku", nullptr, nullptr);
if(!window)
{
cout << "Error to create window";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glClearColor(0.9,0.9,0.9,1);
glOrtho(0, MapSize, 0, MapSize, 0, 1);
glfwSetKeyCallback(window, Keyboard);
glfwSetCursorPosCallback(window, getPos);
glfwSetWindowSizeCallback(window, [](GLFWwindow* window, int width, int height)
{
screenWidth = width;
screenHeight = height;
glViewport(0, 0, width, height);
});
NewGame();
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
while(!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
Draw();
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}