// Sudoku.cpp : Basic class for holding a Sudoku board, reading a board from files, a writing a board to the screen // #include #include #include #include #include using namespace std; class Board { int dim; int ** cells; long totalChecks; public: Board (int); ~Board(); string toString(); void set_square_value(int,int,int); int Board::get_square_value(int,int); static Board * fromFile(string); bool checkForVictory(); int get_dim() {return dim;} }; Board::Board(int d) { if(d > 62) throw ("Dimensions must be at most 62"); dim = d; cells = new int*[dim]; for(int i=0; iset_square_value(row, col, val); } } inFile.close(); return out; } bool Board::checkForVictory() { unsigned long victory = 0; //optimization: check if it's filled: for(int i=1; iget_square_value(i,j)==0) return false; for(int i=1; iget_square_value(i, j); columnTotal += 1 << this->get_square_value(j, i); } if(rowTotal!=victory||columnTotal!=victory) return false; } int dimsqrt = (int)(sqrt((double)dim)); //check little squares: cout << "checking little squares" << endl; for(int i=0;iget_square_value(i*dimsqrt+k, j*dimsqrt+m); cout << this->get_square_value(i*dimsqrt+k, j*dimsqrt+m); } cout << endl; } if(squareTotal != victory) return false; } } return true; } void testBasics() { Board * b = new Board(4); b->set_square_value(1, 1, 1); b->set_square_value(1, 2, 2); b->set_square_value(1, 3, 3); b->set_square_value(1, 4, 4); b->set_square_value(2, 1, 3); b->set_square_value(2, 2, 4); b->set_square_value(2, 3, 1); b->set_square_value(2, 4, 2); b->set_square_value(3, 1, 4); b->set_square_value(3, 2, 3); b->set_square_value(3, 3, 2); b->set_square_value(3, 4, 1); b->set_square_value(4, 1, 2); b->set_square_value(4, 2, 1); b->set_square_value(4, 3, 4); b->set_square_value(4, 4, 3); cout << b->toString(); cout << b->checkForVictory(); char a; cin >> a; } int main(int argc, char* argv[]) { testBasics(); return 0; }