codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> #include <string> #include <sstream> using namespace std; // class SudCell prototype -------------------------------------- class SudCell { private: bool completed; public: static const int MIN_VAL = 1; static const int MAX_VAL = 9; static const int NUM_VALS = 9; static const int SQRT_NUM_VALS = 3; static const int NOT_FINAL_YET = -1; SudCell(); SudCell(const SudCell &sud); string ToString(); string ToStringReveal(); int GetFinalValue(); bool IsCompleted(); bool ValuePresent(int val); int NumRemainingVals(); void SetToBlank(); bool ForcedSet(int val); bool Complete(); bool RuleOut(int val); bool SoftSet(int val); bool Validate(int val); private: bool digits[NUM_VALS]; }; // class Sudoku prototype -------------------------------------- class Sudoku { public: static const int MIN_VAL = SudCell::MIN_VAL; static const int MAX_VAL = SudCell::MAX_VAL; static const int NUM_VALS = SudCell::NUM_VALS; static const int SQRT_NUM_VALS = SudCell::SQRT_NUM_VALS; static const int NOT_FINAL_YET = SudCell::NOT_FINAL_YET; Sudoku(); void Display(bool reveal); int GetFinalValue(int row, int col); bool IsCompleted(int row, int col); bool ValuePresent(int row, int col, int val); int NumRemainingVals(int row, int col); int NumRemainingValsTotal(); bool AllSingletons(); bool EmptyCellFound(); void SetToBlank(); bool ForcedSet(int row, int col, int val); bool Complete(int row, int col); bool RuleOut(int row, int col, int val); bool SoftSet(int row, int col, int val); bool Validate(int val); bool IndexValidate(int n); // B1 methods-------------- void WireUpSectorArrays(); void SectorDisplay(int sec_row, int sec_col); bool SectorValidate(int n); bool GetSector(int &sec_row, int &sec_col, int row, int col); bool GetSectorCoords(int &row_in_sec, int &col_in_sec, int row, int col); bool GetFullCoords(int sec_row, int sec_col, int row_in_sec, int col_in_sec, int &row, int &col); SudCell* sector[SQRT_NUM_VALS][SQRT_NUM_VALS] [SQRT_NUM_VALS][SQRT_NUM_VALS]; private: SudCell suds[NUM_VALS][NUM_VALS]; //SudCell* sector[SQRT_NUM_VALS][SQRT_NUM_VALS] [SQRT_NUM_VALS][SQRT_NUM_VALS]; //void WireUpSectorArrays(); }; // Main--------------------------------------- int main() { Sudoku puzzle; puzzle.WireUpSectorArrays(); puzzle.SectorDisplay(1,1); /* // set some values in the matrix puzzle.ForcedSet(3, 4, 5); puzzle.ForcedSet(5, 7, 8); puzzle.ForcedSet(7, 2, 9); for (int k = -1; k < 12; k++) if ( !puzzle.ForcedSet(k, k, k) ) cout << "Could not set " << k << endl; cout << endl; // see the matrix puzzle.Display(false); cout << endl; puzzle.WireUpSectorArrays(); // see if we can display a couple legal and illegal sectors puzzle.SectorDisplay(1, 2); cout << endl; puzzle.SectorDisplay(3, 3); cout << endl; puzzle.SectorDisplay(2, 3); cout << endl; // should be invalid puzzle.SectorDisplay(0, 1); cout << endl; puzzle.SectorDisplay(2, 4); cout << endl; */ /* SudCell sud1, sud2, sud3; if ( !sud1.ForcedSet(10) ) cout << "Bad 10\n"; if ( !sud2.ForcedSet(-1) ) cout << "Bad -1\n"; if ( !sud3.RuleOut(50) ) cout << "Bad 50\n"; cout << "A: " << sud1.ToString() << endl; cout << "B: " << sud2.ToString() << endl; cout << "C: " << sud3.ToString() << endl; cout << endl; cout << "D: " << sud1.ToStringReveal() << endl; cout << "E: " << sud2.ToStringReveal() << endl; cout << "F: " << sud3.ToStringReveal() << endl; cout << endl; sud1.ForcedSet(3); sud2.ForcedSet(9); sud3.RuleOut(0); sud3.RuleOut(2); sud3.RuleOut(4); sud3.RuleOut(6); sud3.RuleOut(8); cout << sud1.ToString() << endl; cout << sud2.ToString() << endl; cout << sud3.ToString() << endl; cout << endl; cout << sud1.ToStringReveal() << endl; cout << sud2.ToStringReveal() << endl; cout << sud3.ToStringReveal() << endl; cout << endl; sud1.Complete(); sud2.Complete(); sud3.Complete(); cout << sud1.IsCompleted() << " " << sud2.IsCompleted() << " " << sud3.IsCompleted() << endl; cout << sud1.ValuePresent(1) << sud1.ValuePresent(2) << sud1.ValuePresent(3) << endl; cout << sud3.ValuePresent(1) << sud3.ValuePresent(2) << sud3.ValuePresent(3)<< endl; //Soduku check--------------------------------------- //int main() //{ Sudoku puzzle; // set some values in the matrix, attempting illegal puzzle.ForcedSet(3, 4, 5); puzzle.ForcedSet(7, 1, 3); puzzle.ForcedSet(9, 2, 6); for (int k = -1; k < 12; k++) if ( !puzzle.ForcedSet(k, k, k) ) cout << "Could not set " << k << endl; cout << "\n ------------- first basic ------------- \n"; puzzle.Display(false); cout << "\n ------------- then revealed ------------- \n"; puzzle.Display(true); cout << endl; for (int row = Sudoku::MIN_VAL; row <= Sudoku::MAX_VAL ; row++) for (int col = Sudoku::MIN_VAL; col <= Sudoku::MAX_VAL ; col++) { // rule out 2 through 8 in all places except middle row and col if ( row == 5 || col == 5 ) continue; for (int k = 2; k <= 8; k++) puzzle.RuleOut(row, col, k); } cout << "\n ---------- after massive rule-outs ---------- \n"; puzzle.Display(true); */ return 0; } // SUDCELL ------------------------------------------------- SudCell::SudCell() { completed = false; SetToBlank(); } SudCell::SudCell(const SudCell &sud) { this->completed = sud.completed; for (int x=0;x<9;x++) this->digits[x] = sud.digits[x]; } string SudCell::ToString() { string output; int boolcount = 0; int whichdigit = 0; for (int x=0;x<9;x++) { if (digits[x] == true) { whichdigit = x; boolcount++; } } if (boolcount > 1 || boolcount < 1) output = " - "; else { whichdigit++; ostringstream cnvrt; cnvrt << whichdigit; output = cnvrt.str(); output = " " + output + " "; } return output; } string SudCell::ToStringReveal() { string numstring, outstring; for (int x=0;x<9;x++) { if (digits[x] == true) { ostringstream cnvrt; cnvrt << x + 1; numstring += cnvrt.str(); } } numstring = " " + numstring + " "; return numstring; } int SudCell::GetFinalValue() { int boolcount = 0; int whichdigit = 0; for (int x=0;x<9;x++) { if (digits[x] == true) { whichdigit = digits[x]; boolcount++; } } if (boolcount > 1 || boolcount == 0) return NOT_FINAL_YET; else return whichdigit; } bool SudCell::IsCompleted() { int boolcount = 0; for (int x=0;x<9;x++) { if (digits[x] == true) boolcount++; } if (boolcount == 1) return true; else return false; } bool SudCell::ValuePresent(int val) { if (digits[val-1] == true) return true; else return false; } int SudCell::NumRemainingVals() { int boolcount = 0; for (int x=0;x<9;x++) { if (digits[x] == true) boolcount++; } return boolcount; } void SudCell::SetToBlank() { for (int x=0;x<9;x++) { digits[x] = true; } } bool SudCell::ForcedSet(int val) { if(val < MIN_VAL || val > MAX_VAL) return false; else { for (int x=0;x<9;x++) { digits[x] = false; } digits[val-1] = true; return true; } } bool SudCell::Complete() { if (IsCompleted()) { completed = true; return true; } else return false; } bool SudCell::RuleOut(int val) { if (val < MIN_VAL || val > MAX_VAL) return false; else { digits[val-1] = false; return true; } } bool SudCell::SoftSet(int val) { if (val < MIN_VAL || val > MAX_VAL) return false; else { digits[val] = true; return true; } } bool SudCell::Validate(int val) { if (val > MAX_VAL || val < MIN_VAL) return false; else return true; } // SUDOKU ------------------------------------------------ Sudoku::Sudoku() //copy constructor?? { } void Sudoku::Display(bool reveal) { string output; int levlinecount = 0, sepcount = 0, divider = 0, divideend = 0; if (reveal == false) { for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { output = suds[x][y].ToString(); cout << output; sepcount++; if (sepcount > 2) { sepcount = 0; if (levlinecount < 2) { cout << "|"; levlinecount++; } else { levlinecount = 0; if (divider < 2) { cout << endl; divider++; } else { if (divideend < 2) { cout << endl << "-----------------------------" << endl; divider = 0; divideend++; } } } } } } } else { for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { output = suds[x][y].ToStringReveal(); cout << output; sepcount++; if (sepcount > 2) { sepcount = 0; if (levlinecount < 2) { cout << "|"; levlinecount++; } else { levlinecount = 0; if (divider < 2) { cout << endl; divider++; } else { if (divideend < 2) { cout << endl << "-----------------------------" << endl; divider = 0; divideend++; } } } } } } } } int Sudoku::GetFinalValue(int row, int col) { return suds[row-1][col-1].SudCell::GetFinalValue(); } bool Sudoku::IsCompleted(int row, int col) { return suds[row-1][col-1].SudCell::IsCompleted(); } bool Sudoku::ValuePresent(int row, int col, int val) { return suds[row-1][col-1].SudCell::ValuePresent(val); } int Sudoku::NumRemainingVals(int row, int col) { return suds[row-1][col-1].SudCell::NumRemainingVals(); } int Sudoku::NumRemainingValsTotal() { int total = 0; for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { total += suds[x][y].GetFinalValue(); } } return total; } bool Sudoku::AllSingletons() { bool answer = true; for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { if (NOT_FINAL_YET == suds[x][y].GetFinalValue()) answer = false; } } return answer; } bool Sudoku::EmptyCellFound() { bool answer = false; for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { if (0 == suds[x][y].NumRemainingVals()) answer = true; } } return answer; } void Sudoku::SetToBlank() { for (int x=0;x<MAX_VAL;x++) { for (int y=0;y<MAX_VAL;y++) { suds[x][y].SetToBlank(); } } } bool Sudoku::ForcedSet(int row, int col, int val) { return suds[row-1][col-1].SudCell::ForcedSet(val); } bool Sudoku::Complete(int row, int col) { return suds[row-1][col-1].SudCell::Complete(); } bool Sudoku::RuleOut(int row, int col, int val) { return suds[row-1][col-1].SudCell::RuleOut(val); } bool Sudoku::SoftSet(int row, int col, int val) { return suds[row-1][col-1].SudCell::SoftSet(val); } bool Sudoku::Validate(int val) { if (val < MIN_VAL || val > MAX_VAL) return false; else return true; } bool Sudoku::IndexValidate(int n) { if (n < 0 || n > NUM_VALS-1) return false; else return true; } // B1 methods---------------------------------------- void Sudoku::WireUpSectorArrays() { int inc3 = 0, inc3acr = 0, inc3down = 0, inc3downjump = 0; for(int x=0;x<MAX_VAL;x++) for(int y=0;y<MAX_VAL;y++) { sector[inc3downjump][inc3acr][inc3down][inc3] = &suds[x][y]; //cout << sector[inc3downjump][inc3acr][inc3down][inc3]->SudCell::ToString(); inc3++; if(inc3 > 2) { inc3 = 0; inc3acr++; if (inc3acr > 2) { inc3acr = 0; inc3down ++; if(inc3down > 2) { inc3down = 0; inc3downjump++; if(inc3downjump > 2) inc3downjump = 0; } } } } } void Sudoku::SectorDisplay(int sec_row, int sec_col) { int out = 0, stop = 0; for (int x=0;x<3;x++) for (int y=0;y<3;y++) { sector[sec_row][sec_col][x][y]->ToString(); out++; if (out > 2) { cout << endl; out = 0; stop++; if (stop > 1) stop = 0; } } } bool Sudoku::SectorValidate(int n) { if (n > 0 || n < SQRT_NUM_VALS) return false; else return true; } bool Sudoku::GetSector(int &sec_row, int &sec_col, int row, int col) { if (row > 1 || row < 9) return false; else { if (row > 0 && row < 4) sec_row = 1; else if (row > 3 && row < 7) sec_row = 2; else if (row > 6 && row < 10) sec_row = 3; else return false; } if (row > 1 || row < 9) return false; else { if (col > 0 && col < 4) sec_col = 1; else if (col > 3 && col < 7) sec_col = 2; else if (col > 6 && col < 10) sec_col = 3; else return false; } return true; } bool Sudoku::GetSectorCoords(int &row_in_sec, int &col_in_sec, int row, int col) { if (row > 1 || row < 9 || col > 1 || col < 9) return false; else { row_in_sec = (row % 3) + 1; col_in_sec = (col % 3) + 1; return true; } } bool Sudoku::GetFullCoords(int sec_row, int sec_col, int row_in_sec, int col_in_sec, int &row, int &col) { if (sec_row < 1 || sec_row > 3 || sec_col < 1 || sec_row > 3) return false; else if (row_in_sec < 1 || row_in_sec > 3 || col_in_sec < 1 || col_in_sec > 3) return false; else { row = (sec_row * 3) + row_in_sec; col = (sec_col * 3) + col_in_sec; return true; } }
Private
[
?
]
Run code
Submit