-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuState.cpp
206 lines (178 loc) · 4.54 KB
/
SudokuState.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
#include <iostream>
#include "SudokuState.h"
#include <string>
/**
* SudokuState is a constructor that will create a
* sudoku state object and fill the state with a given
* 2D array.
* Afterwards, it will check if the state is solved.
*/
SudokuState::SudokuState(char currentState[9][9])
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
state[i][j] = currentState[i][j];
isSolved = isFinalSolution();
}
SudokuState::SudokuState()
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
state[i][j] = '-';
}
SudokuState::~SudokuState()
{
// delete state;
}
int SudokuState::isFinalSolution()
{
//Check each box
//Top left box
if (!checkBox(1, 1))
return 1;
//Top Middle box
if (!checkBox(1, 2))
return 2;
//Top Right box
if (!checkBox(1, 3))
return 3;
//Middle Left box
if (!checkBox(2, 1))
return 4;
//Middle Box
if (!checkBox(2, 2))
return 5;
//Middle Right
if (!checkBox(2, 3))
return 6;
//Bottom Left
if (!checkBox(3, 1))
return 7;
//Bottom Middle
if (!checkBox(3, 2))
return 8;
//Bottom Right
if (!checkBox(3, 3))
return 9;
//Check each Vertical line
if (!verticalLinesCheck())
return 10;
//Check each Horizontal line
if (!horizontalLinesCheck())
return 11;
if (checkDash())
return 99;
return -1; //means no errors found thus solved
}
bool SudokuState::checkDash()
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (state[i][j] == '-')
return true;
return false;
}
bool SudokuState::checkBox(int locationX, int locationY)
{
char currentSection[9] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
int spot = 0;
int startX = (locationX - 1) * 3;
int startY = (locationY - 1) * 3;
for (int i = startX; i < startX + 3; i++)
for (int j = startY; j < startY + 3; j++)
if (state[i][j] != '-')
currentSection[spot++] = state[i][j];
for (int i = 0; i < spot; i++)
for (int j = i + 1; j < spot; j++)
if (currentSection[i] == currentSection[j])
return false;
return true;
}
bool SudokuState::verticalLinesCheck()
{
for (int i = 0; i < 9; i++)
{
char currentSection[9] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
int spot = 0;
for (int j = 0; j < 9; j++)
if (state[j][i] != '-')
currentSection[spot++] = state[j][i];
for (int i = 0; i < spot; i++)
for (int j = i + 1; j < spot; j++)
if (currentSection[i] == currentSection[j])
return false;
}
return true;
}
bool SudokuState::horizontalLinesCheck()
{
for (int i = 0; i < 9; i++)
{
char currentSection[9] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
int spot = 0;
for (int j = 0; j < 9; j++)
if (state[i][j] != '-')
currentSection[spot++] = state[i][j];
for (int i = 0; i < spot; i++)
for (int j = i + 1; j < spot; j++)
if (currentSection[i] == currentSection[j])
return false;
}
return true;
}
int SudokuState::checkSolved()
{
isSolved = isFinalSolution();
return isSolved;
}
std::string SudokuState::print()
{
std::string returnString = "";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
returnString += state[i][j];
returnString += " ";
if (j % 3 == 2)
{
returnString += " ";
}
}
returnString += "\n";
if (i % 3 == 2)
{
returnString += "\n";
}
}
return returnString;
}
std::string SudokuState::printColor(SudokuState initialState)
{
std::string returnString = "";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (state[i][j] == initialState.state[i][j])
returnString += state[i][j];
else
{
returnString += "\033[1;32m";
returnString += state[i][j];
returnString += "\033[0m";
}
returnString += " ";
if (j % 3 == 2)
{
returnString += " ";
}
}
returnString += "\n";
if (i % 3 == 2)
{
returnString += "\n";
}
}
return returnString;
}