I have written my first real C++ project, a Sudoku solver! It's not elegant, but it works well. It solves easy and medium but not hard!
#include <locale>
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <cstring>
const char test[11] = {'e', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'};
class Grid { // A Class With The Goal Of Solving A Sudoku
public:
char board[9][9] = { {0} }; // Declare (and apparently initialize) the sudoku grid array
void mapChars(std::string fileName) { // function to get a users input sudoku file and parse to the array
std::ifstream myfile (fileName.c_str());
if (myfile.is_open()) {
std::string line;
unsigned int x = 0;
unsigned int y = 0;
while (getline(myfile,line)) {
for(unsigned int i = 0; i < 20; i++) {
if(strchr(test, line[i]) != NULL){
if (line[i] == 'e') {
y++; x=0;
} else {
board[y][x] = line[i];
x++;
}
}
}
} myfile.close();
}
}
void dumpMap() {
for(unsigned int j = 0;j < 9;j++){
for(unsigned int i = 0;i < 9;i++){
std::cout << board[j][i] << ' ';
}
std::cout << std::endl;
}
}
bool testGrid(char sType, char ident, unsigned int num) { // sType takes type { c: col , r: row, s: square, a: all
unsigned int count = 0;
if ( sType == 'r' ) {
for(int it = 0; it < 9; it++) {
if(board[num][it] == ident) { count = 1; }
}
if(count == 1){
return true;
} else {
return false;
}
return false;
}
if ( sType == 'c' ) {
for(int it = 0; it < 9; it++) {
if(board[it][num] == ident) { count = 1; }
}
if(count == 1){
return true;
} else {
return false;
}
}
if ( sType == 's' ) {
unsigned int startY = 0;
unsigned int startX = 0;
unsigned int count = 0;
startY = (num / 3) * 3;
startX = (num % 3) * 3;
unsigned int rw = startY;
unsigned int cl = startX;
while(rw < (startY + 3)) {
while(cl < (startX + 3)) {
if(board[rw][cl] == ident){ count = 1; }
cl++;
}
rw++;
cl = startX;
}
if(count == 1){
return true;
} else {
return false;
}
}
return false;
}
unsigned int wildCardCount() {
unsigned int wildCards = 0;
for(unsigned int j = 0;j < 9;j++){
for(unsigned int i = 0;i < 9;i++){
if(board[j][i] == '*') { wildCards++; }
}
}
return wildCards;
}
// find coords of first blank cell in square 1
void doStuff(int iter) {
unsigned int startY = 0;
unsigned int startX = 0;
startY = (iter / 3) * 3;
startX = (iter % 3) * 3;
unsigned int rw = startY;
unsigned int cl = startX;
while(rw < (startY + 3)) {
while(cl < (startX + 3)) {
if(board[rw][cl] == '*'){
char poss[9] = {'1','2','3','4','5','6','7','8','9'};
unsigned int totalcount = 0;
unsigned int possibilities = 0;
for(unsigned int alpha = 0; alpha < 9; alpha++){
if (testGrid('r', poss[alpha], rw) == true) { totalcount++; }
if (testGrid('c', poss[alpha], cl) == true) { totalcount++; }
if (testGrid('s', poss[alpha], iter) == true) { totalcount++; }
if(totalcount == 0) { possibilities++; }
totalcount = 0;
}
//std::cout << possibilities << " possibilities" << std::endl;
if(possibilities == 1) {
unsigned int possibleRow = rw;
unsigned int possibleCol = cl;
unsigned int lastCheck = 0;
for(unsigned int alpha = 0; alpha < 9; alpha++){
if (testGrid('r', poss[alpha], possibleRow) == true) { lastCheck++; }
if (testGrid('c', poss[alpha], possibleCol) == true) { lastCheck++; }
if (testGrid('s', poss[alpha], iter) == true) { lastCheck++; }
if(lastCheck == 0) { board[rw][cl] = poss[alpha]; }
lastCheck = 0;
}
}
possibilities = 0;
}
cl++;
}
rw++;
cl = startX;
}
}
} sudoku;
int main() {
unsigned int initialBlanks = 0;
sudoku.mapChars("med1.map");
sudoku.dumpMap();
initialBlanks = sudoku.wildCardCount();
std::cout << std::endl << initialBlanks << " blanks remain." << std::endl << std::endl;;
while(sudoku.wildCardCount() != 0){
for(unsigned int beta = 0; beta < 10; beta++) {
sudoku.doStuff(beta);
sudoku.dumpMap();
std::cout << std::endl << sudoku.wildCardCount() << " blanks remain." << std::endl << std::endl;;
}
}
std::cout << std::endl << "Completed. " << initialBlanks << " blanks filled.";
return 0;
}
It takes a .MAP file, which is just the following:
s-*-*-*-*-*-1-4-5-9-e s-*-*-1-*-5-6-*-*-8-e s-*-*-*-3-*-*-*-*-6-e s-1-8-*-*-*-7-*-*-*-e s-*-*-*-*-3-*-*-*-*-e s-3-*-*-*-*-2-*-*-*-e s-8-*-*-4-7-*-3-*-*-e s-7-9-2-6-*-*-*-*-*-e
Am I please with myself? Yes. Am I finished? No.
I just now need to work out what I need to do to start solving those hard grids, tidy up the code and put in some validation.