-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuMove.cpp
98 lines (78 loc) · 2.26 KB
/
SudokuMove.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
//
// Created by Armin on 11/04/2023.
//
#include <iostream>
#include "SudokuMove.h"
int const allMovesMap = 0b111111111;
int valueToMap(int value) {
return 1 << (value - 1);
}
SudokuMove::SudokuMove() {
for (int i = 0; i < 9; ++i) {
allowedColumns[i] = allMovesMap;
allowedRows[i] = allMovesMap;
}
for (auto & allowedGroup : allowedGroups) {
for (int & y : allowedGroup) {
y = allMovesMap;
}
}
}
SudokuMove::SudokuMove(SudokuMove const & sudoku) {
std::copy(&sudoku.allowedRows[0], &sudoku.allowedRows[0] + 9, &allowedRows[0]);
std::copy(&sudoku.allowedColumns[0], &sudoku.allowedColumns[0] + 9, &allowedColumns[0]);
std::copy(&sudoku.allowedGroups[0][0], &sudoku.allowedGroups[0][0] + 3*3, &allowedGroups[0][0]);
std::copy(&sudoku.taken[0], &sudoku.taken[0] + 9, &taken[0]);
}
SudokuMove::SudokuMove(const std::shared_ptr<SudokuMove> &parent, int x, int y, int value) : SudokuMove(*parent) {
moveX = x;
moveY = y;
moveValue = value;
this->parent = parent;
int map = valueToMap(value);
allowedRows[y] &= ~map;
allowedColumns[x] &= ~map;
allowedGroups[x / 3][y / 3] &= ~map;
taken[x] |= (1 << y);
}
void SudokuMove::print(std::ostream & os) const {
int data[9][9] { };
data[moveY][moveX] = moveValue;
auto move = parent;
while (move != nullptr) {
data[move->moveY][move->moveX] = move->moveValue;
move = move->parent;
}
for (auto & y : data) {
for (auto x : y) {
os << (char)(x + '0') << " ";
}
os << std::endl;
}
os << std::endl;
}
bool SudokuMove::isSolved() const {
for (int i : taken) {
if (i != allMovesMap) {
return false;
}
}
return true;
}
bool SudokuMove::isMoveAllowed(int x, int y) const {
return (taken[x] & (1 << y)) == 0;
}
bool SudokuMove::isMoveAllowed(int x, int y, int value) const {
int map = valueToMap(value);
return (allowedRows[y] & allowedColumns[x] & allowedGroups[x / 3][y / 3] & map) != 0;
}
void SudokuMove::findNextAllowedMove(int & x, int & y) const {
for (; x < 9; x++) {
for (; y < 9; y++) {
if (isMoveAllowed(x, y)) {
return;
}
}
y = 0;
}
}