-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.cpp
158 lines (109 loc) · 4.67 KB
/
algorithm.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
//
// Created by kyle on 2/9/19.
//
#include <array>
#include <cassert>
#include <functional>
#include <iostream>
#include <list>
#include <set>
#include <sstream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "types.h"
#include "puzzle.h"
#include "algorithm.h"
using std::string;
void steps::start_pass() { pass++; }
void steps::start_phase(string _phase) { phase = move(_phase); }
void steps::record_step(sudoku_puzzle puzzle, string description, square& current, square& source) {
size_t n = count + 1;
steps.push_back({n, pass, move(description), phase, puzzle, current.location(), source.location()});
count++;
}
void steps::record_step(sudoku_puzzle puzzle, string description, square& current) {
size_t n = count + 1;
steps.push_back({n, pass, move(description), phase, puzzle, current.location(), no_highlight});
count++;
}
steps::iterator steps::begin() {
return steps.begin();
}
steps::iterator steps::end() {
return steps.end();
}
void removeAlreadyExistingValuesByRow(sudoku_puzzle& puzzle, steps& steps, square& square);
void removeAlreadyExistingValuesByColumn(sudoku_puzzle& puzzle, steps& steps, square& square);
void removeAlreadyExistingValuesByBlock(sudoku_puzzle& puzzle, steps& steps, square& square);
steps solve(sudoku_puzzle& puzzle) {
steps steps;
while (!puzzle.solved()) {
steps.start_pass();
steps.start_phase("Remove Conflicts");
puzzle.forEachUnsolvedSquare([&](square& square) {
removeAlreadyExistingValuesByRow(puzzle, steps, square);
removeAlreadyExistingValuesByColumn(puzzle, steps, square);
removeAlreadyExistingValuesByBlock(puzzle, steps, square);
});
steps.start_phase("Accept Unique Candidates");
puzzle.forEachUnsolvedSquare([&](square& square) {
for (int8_t candidate : square.candidates()) {
if (puzzle.squaresInRowWithCandidate(square.row(), candidate) == 1) {
square.set(candidate);
std::ostringstream o;
o << "Only potential " << square.value() << " in row.";
steps.record_step(puzzle, o.str(), square);
return;
}
if (puzzle.squaresInColumnWithCandidate(square.col(), candidate) == 1) {
square.set(candidate);
std::ostringstream o;
o << "Only potential " << square.value() << " in column.";
steps.record_step(puzzle, o.str(), square);
return;
}
if (puzzle.squaresInBlockWithCandidate(square.block(), candidate) == 1) {
square.set(candidate);
std::ostringstream o;
o << "Only potential " << square.value() << " in block.";
steps.record_step(puzzle, o.str(), square);
return;
}
}
});
}
return steps;
}
void removeAlreadyExistingValuesByRow(sudoku_puzzle& puzzle, steps& steps, square& square) {
puzzle.forEachSquareInRow(square.row(), [&](class square& other_square) {
if (other_square == square) return;
if (other_square.is_solved() && square.could_be(other_square.value())) {
square.removeCandidate(other_square.value());
std::ostringstream o;
o << "Removing " << other_square.value() << ". Already exists in row.";
steps.record_step(puzzle, o.str(), square, other_square);
}
});
}
void removeAlreadyExistingValuesByColumn(sudoku_puzzle& puzzle, steps& steps, square& square) {
puzzle.forEachSquareInColumn(square.col(), [&](class square& other_square) {
if (other_square == square) return;
if (other_square.is_solved() && square.could_be(other_square.value())) {
square.removeCandidate(other_square.value());
std::ostringstream o;
o << "Removing " << other_square.value() << ". Already exists in column.";
steps.record_step(puzzle, o.str(), square, other_square);
}
});
}
void removeAlreadyExistingValuesByBlock(sudoku_puzzle& puzzle, steps& steps, square& square) {
puzzle.forEachSquareInBlock(square.block(), [&](class square& other_square) {
if (other_square == square) return;
if (other_square.is_solved() && square.could_be(other_square.value())) {
square.removeCandidate(other_square.value());
std::ostringstream o;
o << "Removing " << other_square.value() << ". Already exists in block.";
steps.record_step(puzzle, o.str(), square, other_square);
}
});
}