Skip to content

Commit 2210522

Browse files
committed
solving puzzle automatically
1 parent 4b56868 commit 2210522

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

include/PlayState.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <SFML/Graphics/Sprite.hpp>
77
#include <SFML/Graphics/Texture.hpp>
8+
#include <set>
89

910
class PlayState : public State {
1011
public:
@@ -21,6 +22,8 @@ class PlayState : public State {
2122

2223
void setColorOfMistakes(int row, int col, sf::Color color);
2324

25+
void solve();
26+
2427
private:
2528
SudokuSolver solver;
2629

@@ -34,4 +37,8 @@ class PlayState : public State {
3437
sf::Sprite m_resetSprite;
3538

3639
bool m_loadedFromFile;
40+
41+
std::set<std::pair<int, int>> m_mistakes;
42+
43+
bool m_done;
3744
};

include/SudokuSolver.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
32
#include <utility>
43

54
enum
@@ -14,11 +13,13 @@ class SudokuSolver {
1413
SudokuSolver();
1514

1615
bool Solve();
17-
16+
1817
void set(int row, int column, int num);
1918

2019
int get(int row, int column) const;
2120

21+
int* getSolvedCell();
22+
2223
void print();
2324

2425
private:

src/PlayState.cpp

+40-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <unordered_map>
1111

1212
PlayState::PlayState(StateMachine& machine, sf::RenderWindow& window, bool replace)
13-
: State(machine, window, replace), m_loadedFromFile{ false }
13+
: State(machine, window, replace), m_loadedFromFile{ false }, m_done{false}
1414
{
1515
if (!m_mainbgText.loadFromFile("assets/mainbg.png"))
1616
{
@@ -79,7 +79,10 @@ PlayState::PlayState(StateMachine& machine, sf::RenderWindow& window, bool repla
7979
cell.text.setFillColor(sf::Color::Black);
8080

8181
if (m_loadedFromFile)
82+
{
83+
solver.set(x, y, cell.num);
8284
setColorOfMistakes(x, y, sf::Color::Red);
85+
}
8386
}
8487
}
8588
}
@@ -106,6 +109,9 @@ void PlayState::update()
106109
case sf::Keyboard::Escape:
107110
m_next = StateMachine::build<MenuState>(m_machine, m_window, false);
108111
break;
112+
case sf::Keyboard::S:
113+
solve();
114+
break;
109115
default:
110116
break;
111117
}
@@ -120,7 +126,8 @@ void PlayState::update()
120126
m_next = StateMachine::build<MenuState>(m_machine, m_window, false);
121127
else if (m_resetSprite.getGlobalBounds().contains(worldPos))
122128
reset();
123-
129+
if (m_done)
130+
return;
124131
for (int x = 0; x < ROW; x++)
125132
{
126133
for (int y = 0; y < COL; y++)
@@ -146,6 +153,8 @@ void PlayState::update()
146153
std::cout << "\ntrue";
147154
for (int i = 0; i < ROW * COL; i++)
148155
m_machine.m_cells.get()[i].text.setFillColor(sf::Color::Black);
156+
157+
m_mistakes.clear();
149158
}
150159
else
151160
{
@@ -189,6 +198,7 @@ void PlayState::reset()
189198

190199
solver = SudokuSolver();
191200
solver.print();
201+
m_done = false;
192202
}
193203

194204

@@ -219,8 +229,35 @@ void PlayState::setColorOfMistakes(int row, int column, sf::Color color)
219229
for (auto i = nums.begin(); i != nums.end(); i++)
220230
{
221231
if (i->first != 0 && nums.count(i->first) > 1)
232+
{
222233
m_machine.m_cells.get()[i->second.second * ROW + i->second.first].text.setFillColor(color);
234+
m_mistakes.insert(std::make_pair(i->second.first, i->second.second));
235+
}
223236
else
237+
{
238+
m_mistakes.erase(std::make_pair(i->second.first, i->second.second));
224239
m_machine.m_cells.get()[i->second.second * ROW + i->second.first].text.setFillColor(sf::Color::Black);
240+
}
241+
}
242+
}
243+
244+
void PlayState::solve()
245+
{
246+
for(auto& i : m_mistakes)
247+
{
248+
m_machine.m_cells.get()[i.second * ROW + i.first].num = 0;
249+
solver.set(i.first, i.second, 0);
225250
}
226-
}
251+
solver.Solve();
252+
253+
int* cell = solver.getSolvedCell();
254+
255+
for (int i = 0; i < ROW * COL; i++)
256+
{
257+
m_machine.m_cells.get()[i].num = cell[i];
258+
m_machine.m_cells.get()[i].text.setString(std::to_string(cell[i]));
259+
m_machine.m_cells.get()[i].text.setFillColor(sf::Color::Black);
260+
}
261+
262+
m_done = true;
263+
}

src/SudokuSolver.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
SudokuSolver::SudokuSolver()
77
{
88
for (int i = 0; i < ROW * COL; i++)
9+
{
910
grid[i] = UNASSIGNED;
11+
m_temp[i] = UNASSIGNED;
12+
}
1013

1114
std::srand(std::time(nullptr));
1215
}
@@ -30,6 +33,11 @@ int SudokuSolver::get(int row, int column) const
3033
return grid[column * ROW + row];
3134
}
3235

36+
int* SudokuSolver::getSolvedCell()
37+
{
38+
return m_temp;
39+
}
40+
3341
void SudokuSolver::print()
3442
{
3543
system("cls");
@@ -62,11 +70,13 @@ bool SudokuSolver::solve()
6270
if (cell.first == -1)
6371
return true;
6472

65-
for (int i = 1; i <= 9; i++)
73+
for (int i = 0; i < 9; i++)
6674
{
67-
if (isSafe(cell.first, cell.second, i))
75+
int num = std::rand() % 9 + 1;
76+
77+
if (isSafe(cell.first, cell.second, num))
6878
{
69-
m_temp[cell.second * ROW + cell.first] = i;
79+
m_temp[cell.second * ROW + cell.first] = num;
7080

7181
if (solve())
7282
return true;
@@ -77,6 +87,7 @@ bool SudokuSolver::solve()
7787
return false;
7888
}
7989

90+
8091
bool SudokuSolver::isSafe(int row, int column, int num)
8192
{
8293
//Check ROW
@@ -104,7 +115,7 @@ bool SudokuSolver::isSafe(int row, int column, int num)
104115
return true;
105116
}
106117

107-
std::pair<int,int> SudokuSolver::FindUnassignedCell()
118+
std::pair<int, int> SudokuSolver::FindUnassignedCell()
108119
{
109120
for (int x = 0; x < ROW; x++)
110121
{

0 commit comments

Comments
 (0)