HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

9x9 Sudoku solver

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sudokusolver9x9

Problem

I wrote this in C++ in February and am planning on adding a generator to it. Before I do that, I'd like to get input on how to improve what I've done thus far.

The goal was to be able to solve every Sudoku I throw at it. This includes the ones that reach a point where you can no longer assign numbers based on what they might be. Then you have to start guessing from the list of potential values until you find the right one(s).

```
#include
#include
#include
#include

using namespace std;

class subSquare{
private:
//fNum is the final assigned number, pNums are numbers that a subsquare could potentially be.
int fNum;
vector pNums;
public:
void assignFNum(int const& num){
pNums.clear();
fNum=num;
}
void assignPNum(int const& num){
pNums.push_back(num);
}
int getFNum(){
return fNum;
}
vector getPNums(){
return pNums;
}
void removePNum(int const& num){
int pos = (distance(pNums.begin(), find(pNums.begin(), pNums.end(), num)));
pNums.erase(pNums.begin() + pos);
}
bool PNumsContain(int const& num){
return(find(pNums.begin(), pNums.end(), num) 1){
return true;
}
}
}
}
return false;
}
}

//Checks if the row contains an fNum or pNums.
bool rowContainsNum(subSquare input[9][9], int const& rowIndx, int const& num, string const& checkingFor){
int instances = 0;
if(checkingFor == "fNums"){
for(int col = 0; col 1){
return true;
}
}
}
return false;
}
}

//Checks if the column contains an fNum or pNums.
bool colContainsNum(subSquare input[9][9], int const& colIndx, int const& num, string const& checkingFor){
int instances = 0;
if(checkingFor == "fNums"){
for(int row = 0; row 0){
while(guessNumIndx > str;
cin.ignore();
strToUpper(str);
if(str=="SOLVE

Solution

using namespace std;

using namespace std;


Why is “using namespace std;” considered bad practice?

The particular reason why I don't like it is that it makes it hard to tell what is and is not coming from std. Another issue is that it can be hard to refactor code into separate files.

Naming

int fNum;
    vector pNums;


Consider

int value;
    std::vector possibilities;


Then you don't have to remember what you meant by fNum and pNums.

bool PNumsContain(int const& num){


Could be

bool isPossible(int const& num) {


Same argument.

void assignFNum(int const& num){


Consider

void setValue(int const& num) {


would be more idiomatic.

void assignPNum(int const& num){


could be

void addPossibility(int const& num) {


Which is clearer about what it is doing.

Indexes vs. iterators

for(int itr=0; itr<input.length(); itr++){
        if(!isdigit(input.at(itr))){


When I see this, the first thing that I think is "That's no iterator!"

for (auto itr = input.begin(); itr != input.end(); itr++) {
        if (!isdigit(*itr)) {


This is a C++ iterator. It's more common to call index variables (what you originally had) i.

Numbers don't have an uppercase

strToUpper(currentRow);
        if(isNumeric(currentRow)){


You don't need to convert numeric rows to uppercase. You only need to convert to upper case if it is not upper case.

if (isNumeric(currentRow)) {
// ...
        } else {
            strToUpper(currentRow);


printf vs. cout

You shouldn't mix C (printf, scanf) and C++ (cout, cin). Weird things can happen. You can use std::cout with objects. So you probably should usually stick with cout when writing C++ so as to only have one.

You can see more discussion at printf vs cout in C++.

Use enums where appropriate

if(mode == "TO_3D"){


This is screaming for an enum. Why do a string comparison where a simple integer comparison would do? An enum gives the same readability as a string with the performance of an integer, relying on the compiler to reconcile them.

Code Snippets

using namespace std;
int fNum;
    vector<int> pNums;
int value;
    std::vector<int> possibilities;
bool PNumsContain(int const& num){
bool isPossible(int const& num) {

Context

StackExchange Code Review Q#136422, answer score: 2

Revisions (0)

No revisions yet.