forked from AlexIzydorczyk/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.hpp
131 lines (106 loc) · 3.23 KB
/
solver.hpp
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
/*
Matt Olson
Alex Izydorczyk
Implement Board class to hold state of sudoku game
*/
#pragma once
#include <cassert>
#include <cstdlib>
#include <iostream>
// board class to hold state of sudoku game (parts of board implementation
// borrowed from lecture code)
class Board{
int N;
int **grid;
bool **immutable;
bool **infeasible;
public:
// To be implemented as matrix with single array
Board(int _N) : N(_N){
//matrix to keep track of values
grid = new int*[N];
for(int i = 0; i < N; i++){
grid[i] = new int[N];
}
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
grid[i][j] = 0;
//matrix to track immutables
immutable = new bool*[N];
for(int i = 0; i < N; i++){
immutable[i] = new bool[N];
}
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
immutable[i][j] = true;
// matrix to track which values are causing infeasibilitsy
// these are problem cells that will be
// highlighted in red during gameplay
infeasible = new bool*[N];
for(int i = 0; i < N; i++){
infeasible[i] = new bool[N];
}
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
infeasible[i][j] = false;
}
//destructor for board
~Board(){
for(int i = 0; i < N; i++){
delete [] grid[i];
}
delete [] grid;
for(int i = 0; i < N; i++){
delete [] immutable[i];
}
delete [] immutable;
for(int i = 0; i < N; i++){
delete [] infeasible[i];
}
delete [] infeasible;
}
void printPuzzle(); // print the puzzle to the screen
bool checkPuzzle(); // Check if puzzle is complete
void clearPuzzle(); //Clear mutables from puzzle
bool inBounds(int val); //Check if value can exist in puzzle
bool feasibleUser(int row, int col, int val);
//Operator overload to assign value to cell
int& operator() (int x, int y){
assert(x < N && y < N);
return grid[x][y];
}
//Operator overload to assign value to cell
void assignValue(int x, int y, int val){
(*this)(x,y) = val;
}
// toggle cell mutability
void assignImmutable(int x, int y, bool val){
immutable[x][y] = val;
}
//Checks if cell is ummutable (not changeable by solve/user)
bool checkImmutable(int x, int y){
return immutable[x][y];
}
//Keep track of "problem cells"
//i.e. cells that cause infeasibility
bool isProblem(int x, int y){
return infeasible[x][y];
}
//Get size of game
//ie. 9 for 9x9 game
int getSize(){
return N;
}
// function for debugging (kind of dumb, but need to change its
// signature each time you change the puzzle size...but I suppose
// in practice we will never need this)
void setFromArray(int a[4][4]){
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
grid[i][j] = a[i][j];
}
};
bool feasible(Board &b, int row, int col, int val);
bool solve(Board &b, int row, int col);
int* genPerm(int N);
Board generatePuzzle(int n, int nobs);