|
-
May 7th, 2013, 04:20 AM
#1
SudokuSolver does not solve
Hi guys,
I'm coding a Sudoku Solver but it isn't finished yet. However, I've tried to run it just to see, if it works for an empty Sudoku.
Code:
#include <iostream>
typedef unsigned short ushort;
using std::cout;
using std::cin; // will be needed later for input
using std::endl;
const ushort maxCol=9, maxRow=9;
class Sudoku
{
private:
ushort gameField[maxRow][maxCol];
public:
Sudoku(); //ctor
void Print();
void SetNumber(ushort row, ushort col, ushort number);
void Solve();
bool CheckRow(ushort row, ushort number);
bool CheckCol(ushort col, ushort number);
bool CheckSquare(ushort row, ushort col, ushort number);
};
Sudoku::Sudoku()
{
for (ushort actRow=0; actRow < maxRow; actRow++)
{
for (ushort actCol=0; actCol < maxCol; actCol++)
{
SetNumber(actRow, actCol, 0);
}
}
}
void Sudoku::Print()
{
for (ushort actRow=0; actRow < maxRow; actRow++)
{
for (ushort actCol=0; actCol < maxCol; actCol++)
{
cout << gameField[actRow][actCol] << " ";
if (actCol==2 || actCol==5)
{
cout << "| ";
}
}
cout << endl;
if (actRow == 2 || actRow == 5)
{
cout << "------ ------- ------ " << endl;
}
}
cout << endl;
}
bool Sudoku::CheckCol(ushort col, ushort number)
{
bool check = false;
for (ushort actRow=0; actRow < maxRow; actRow++)
{
if (gameField[actRow][col] == number) //Checks if you find your number only one time in the column
{
check = !check; //finds it one time then check is true
}
}
return check;
}
bool Sudoku::CheckRow(ushort row, ushort number)
{
bool check = false;
for (ushort actCol=0; actCol < maxCol; actCol++)
{
if (gameField[row][actCol] == number) //Checks if you find your number only one time in the row
{
check = !check; //finds it one time then check is true
}
}
return check;
}
bool Sudoku::CheckSquare(ushort row, ushort col, ushort number)
{
bool check = false;
ushort sqMinCol = col - ( col%3 ); // The smallest column in the actual square
ushort sqMinRow = row - ( row%3 );
ushort sqMaxCol = sqMinCol + 2; //The highest column in the actual square
ushort sqMaxRow = sqMinRow + 2;
for (ushort actCol=sqMinCol; actCol <= sqMaxCol; actCol++)
{
for (ushort actRow=sqMinRow; actRow <= sqMaxRow; actRow++)
{
if (gameField[actRow][actCol] == number)
{
check = !check;
}
}
}
return check;
}
void Sudoku::SetNumber(ushort row, ushort col, ushort number)
{
gameField[row][col] = number;
}
void Sudoku::Solve() //Tries different numbers in different rows and columns
{
for (ushort actRow=0; actRow < maxRow; actRow++)
{
for (ushort actCol=0; actCol < maxCol; actCol++)
{
for (ushort number=1; number <=9; number++)
{
if ( (CheckRow(actRow, number)==true) && (CheckCol(actCol, number)==true) && (CheckSquare(actRow, actCol, number)==true))
{
SetNumber(actRow, actCol, number);
break;
}
}
}
}
}
int main()
{
Sudoku game;
game.Print();
game.Solve();
game.Print();
return 0;
}
I don't know, why it doesn't work. CheckRow, CheckCol and CheckSquare work great.
I would appreciate any hints.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|