//file Sudoku004.cpp
//authors aou
//date 2006.02.20
#include <iostream>
using namespace std;
/* Project 01, date assigned 2006.02.03, date due 2006.02.10
Implement the following member function:
int isValid(void);
test it on two puzzles, one good and one bad
*/
int bad[9][9]=
{
{1,2,3,4,5,6,7,8,0},
{1,2,3,4,5,6,7,8,9},
{9,8,7,6,5,4,3,2,1},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{9,8,7,6,5,4,3,2,1},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{0,8,7,6,5,4,3,2,1}
};
int good[9][9]=
{
{0,0,0,4,5,6,7,8,0},
{1,2,3,4,5,6,7,8,9},
{9,0,7,6,5,4,3,2,1},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{9,8,7,6,5,4,3,2,1},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{0,8,7,6,5,4,3,2,1}
};
class CSudoku
{
private:
int a[9][9];
bool checkDupes(int wa[]);
/*
Method1: Compare each value with every other value
Method2: Sort and compare adjacent values
method3: Search for missing values 1, 2, 3,... order
Method4: Use an array of foundFlags
Method5: If sum is not 45 then something wrong for sure,
if sum is 45 then there is no guarantee
*/
void moveRow2Array(int r, int wa[]);
/*
Move column c elements:
wa[i] = a[r][i] for i=0 to 8
*/
void moveCol2Array(int c, int wa[]);
/*
Move row r elements:
wa[i] = a[i][c] for i=0 to 8
*/
void moveRegion2Array(int r, int c, int wa[]);
/*
Move region r,c elements:
if r=0, c=0 => moving 00,01,02,
10,11,12
20,21,22
for r=0, c=0
wa[0]=a[r][c]
wa[1]=a[r][c+1]
wa[2]=a[r][c+2]
wa[3]=a[r+1][c]
wa[4]=a[r+1][c+1]
wa[5]=a[r+1][c+2]
wa[6]=a[r+2][c]
wa[7]=a[r+2][c+1]
wa[8]=a[r+2][c+2]
*/
public:
CSudoku(void); //default constructor
CSudoku(int d[9][9]); //custom constructor
void display(void);
int isValid(void); //Project 01 test it on two puzzles one good and one bad
void createNew(void);
bool checkSolution(void);
bool isvalidMove(int r, int c, int n);
bool solve(void);
};
void test_CSudoku_isValid(void);
void main(void)
{
test_CSudoku_isValid();
}
void test_CSudoku_isValid(void)
{
CSudoku puzzle1, puzzle2(good);
puzzle1.display();
cout << endl;
puzzle2.display();
cout << endl;
CSudoku puzzle3(bad);
puzzle3.display();
cout << endl;
int violations;
violations = puzzle3.isValid();
if (0 == violations)
cout << "This is a good pouzzle\n";
else
cout << "This is NOT a good pouzzle violations ="
<< violations << endl;
}
int CSudoku::isValid(void)
{
/*
problem:
returns the number of violations (27 max)
nine rows, 9 columns, 9 subregions
algorithm:
violations = 0
for each row
move row elements into array wa[]
call checkDupes(wa)
if necessary violations++
for each colums
move column elements into array wa[]
call checkDupes(wa)
if necessary violations++
for each subregion
move subregion elements into array wa[]
call checkDupes(wa)
if necessary violations++
return violations
*/
//supply the missing code
return true;
}
CSudoku::CSudoku(void)
{
for (int r=0; r<9; r++)
for (int c=0; c<9; c++)
a[r][c] = 0;
}
CSudoku::CSudoku(int d[9][9])
{
for (int r=0; r<9; r++)
for (int c=0; c<9; c++)
a[r][c] = d[r][c];
}
void CSudoku::display(void)
{
for (int r=0; r<9; r++)
{
for (int c=0; c<9; c++)
{
if (a[r][c]==0)
cout << ' ';
else
cout << a[r][c];
if (c==2 || c==5)
cout << '|';
else
cout << ' ';
}
cout << endl;
if (r==2 || r==5)
cout << "-----+-----+-----\n";
}
}
|