-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuClass.cpp
More file actions
158 lines (145 loc) · 4.64 KB
/
Copy pathSudokuClass.cpp
File metadata and controls
158 lines (145 loc) · 4.64 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
#include "SudokuClass.h"
#include <QColor>
#include <time.h>
#include "Types.h"
SudokuClass::SudokuClass(SudokuStructure * sudoku, SudokuSettings *settings)
{
this->sudoku = sudoku;
this->settings = settings;
for(int i = 0; i < 10; i++){
this->numberState.push_back(0);
}
}
Cell * SudokuClass::getCell(int posX, int posY)
{
return this->sudoku->getSudoku()[posX-1][posY-1];
}
void SudokuClass::defaultColors(){
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
Cell * cell = this->sudoku->getSudoku()[i][j];
cell->setDefColor();
cell->setDefFontColor();
}
}
std::vector<int> SudokuClass::updateNumberState()
{
for(int i = 0; i < 10;i++){
this->numberState[i] = 0;
}
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
Cell * cell = this->sudoku->getSudoku()[i][j];
this->numberState[cell->getVal()]++;
}
return this->numberState;
}
void SudokuClass::clickCell(int posX, int posY)
{
//kliknięcie na komórkę. Podświetlenie kolumn oraz wierszy. Jeśli została kliknięta cyfra - zostają podświetlone te same cyfry w sudoku
Cell * cell = this->getCell(posX, posY);
this->defaultColors();
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
Cell * editedCell = this->sudoku->getSudoku()[i][j];
if(editedCell != cell)
editedCell->setClicked(false);
if(((i + 1 == cell->getRow()) || (j + 1 == cell->getCol())) &&
(((i + 1 == cell->getRow()) && (j + 1 == cell->getCol())) || (this->settings->ColsAndRows))){
editedCell->setCellColor(QColor(0, 154, 255, 255));
}
if((cell->getVal() != 0) && (editedCell->getVal() == cell->getVal()) && (this->settings->Numbers)){
editedCell->setFontColor(QColor(Qt::red));
}
}
}
}
void SudokuClass::numberClicked(int number)
{
//kliknięty numer na panelu dolnym. Podświetlenie tych samych numerów na planszy
if((number < 1) || (number > 9)) return;
this->defaultColors();
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
Cell * cell = this->sudoku->getSudoku()[i][j];
if(cell->getVal() == number){
cell->setFontColor(QColor(Qt::red));
}
}
}
}
void SudokuClass::clearMemory()
{
delete this->sudoku;
}
void SudokuClass::updateCellVal(int posX, int posY)
{
if(!this->settings->Errors) {
this->clickCell(posX, posY);
return;
}
//zamknięcie okienka
bool rowConflict = false;
bool colConflict = false;
Cell * cell = this->getCell(posX, posY);
//this->defaultColors();
for(int i = 0; i < 9; i++)
for(int j = 0; j <9; j++){
Cell * tCell = this->sudoku->getSudoku()[i][j];
if((cell->getVal() != 0) && (cell->getVal() == tCell->getVal()) && (cell != tCell)){
if(cell->getCol() == tCell->getCol()) colConflict = true;
if(cell->getRow() == tCell->getRow()) rowConflict = true;
}
}
if(rowConflict || colConflict){
for(int i = 0; i < 9; i++)
for(int j = 0; j <9; j++){
Cell * tCell = this->sudoku->getSudoku()[i][j];
if(rowConflict && (cell->getRow() - 1 == i))
tCell->setCellColor(QColor(Qt::red));
if(colConflict && (cell->getCol() - 1 == j))
tCell->setCellColor(QColor(Qt::red));
}
}else{
this->clickCell(posX, posY);
}
}
bool SudokuClass::checkSudoku()
{
for(int i = 0; i < 9; i++)
for(int j = 0; j <9; j++){
Cell * actualCell = this->sudoku->getSudoku()[i][j];
Cell * solvedCell = this->sudoku->getSolvedSudoku()[i][j];
if(actualCell->getVal() != solvedCell->getVal()){
return false;
}
}
return true;
}
SudokuStructure *SudokuClass::getSudokuStructure()
{
return this->sudoku;
}
std::vector<int> SudokuClass::getNumberState()
{
return this->numberState;
}
Cell *SudokuClass::getHint()
{
this->defaultColors();
bool searching = true;
while(searching){
srand((unsigned)time(NULL));
int i = rand() % 9;
int j = rand() % 9;
Cell * cell = this->sudoku->getSudoku()[i][j];
if(cell->getVal() == 0){
Cell * solvedCell = this->sudoku->getSolvedSudoku()[i][j];
cell->setVal(solvedCell->getVal());
cell->setCellColor(QColor(Qt::green));
searching = false;
return cell;
}
}
return NULL;
}