12,501,692 members (65,018 online)

52.8K views
59 bookmarked
Posted

# A Sudoku Teacher Using Boost Libraries

, 7 Jul 2006
A Sudoku teacher using multi_index_container, lambda, and other Boost libraries.
 ```// CellCollection.h // // Copyright 2006 Scott A. Ross // thrownbybabe@yahoo.com // // You are hereby granted permission to use this software for non-commercial use. // // This software is provided "as is" with no expressed // or implied warranty. I accept no responsibility or liability for any // damage or loss of business that this software may cause. // /////////////////////////////////////////////////////////////////////////////// #ifndef CELL_COLLECTION_H_ #define CELL_COLLECTION_H_ #include #include "SudokuElement.h" #include "SBlock.h" #include "PileAndChain.h" #include "PointingPairs.h" #include #include #include #include #include #include // for 'operator+=()' #include class CellCollection { public: CellCollection(); ~CellCollection(); enum SudokuCollectionType { sct_Row = 0, sct_Col = 1, sct_Square = 2 }; // Methods indicating state bool Complete() const; // all cells have been filled in int NumAnswers() const; // number of cells which have been determined // Methods related to setting/getting the answer for a cell (if one has been determined) int GetAnswer(int sqNum) const; bool SetAnswer(int sqNum, int answer, std::string sReason); void ClearAnswer(int sqNum); void GetReasons(int sqNum, std::vector& reasons) const; // methods for maintaining the dynamic bitset indicating which digits are still possible for // a given cell // returns 0 and a list of possibilities or returns a digit if the square is already solved bool IsDigitPossible(int sqNum, int digit) const; int GetPossibilities(int squareNum, std::vector& Possibilities) const; int RemoveTakenDigits(SudokuCell* pElm, boost::dynamic_bitset<>* pUsedDigits, std::string* sReason); int GetPossibilitiesString(int squareNum, std::string& strPos) const; public: // We use a multi_index_container so we can access the cells as rows, columns or blocks typedef boost::multi_index::multi_index_container< SudokuCell*, boost::multi_index::indexed_by< // sort by cell number - 0 boost::multi_index::ordered_unique >, // sort by row - 1 boost::multi_index::ordered_non_unique >, // sort by column - 2 boost::multi_index::ordered_non_unique >, // sort by block - 3 boost::multi_index::ordered_non_unique > > > squares_set; squares_set& Grid() { return Squares; }; // Get a row, column or block void GetRow(int rowNum, boost::array &rowVec) const; void GetCol(int colNum, boost::array &colVec) const; void GetBlock(int blockNum, boost::array &blockVec) const; void TakeSnapshot(); bool CellChanged() const; void GetChangedCells(std::vector &vChangedCells) const; int GetChangedCellCount() const; void SaveState(); void RestoreState(); bool GotContradictions() const; void SetTautologies(std::string &sText) const; void SetAsideAnswer(); private: // indexes squares_set::nth_index<0>::type& sort_by_number; squares_set::nth_index<1>::type& sort_by_row; squares_set::nth_index<2>::type& sort_by_col; squares_set::nth_index<3>::type& sort_by_block; squares_set Squares; squares_set::iterator sqIter; }; #endif // CELL_COLLECTION_H_```

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

A list of licenses authors might use can be found here

## Share

 Web Developer United States
I live in Minnesota with my wife and son, having moved here from Montana. I enjoy creating software which benefits people, and I am fortunate to have a job where I am able to do that.
I enjoy reading, hunting and fishing, any sport with a racquet or paddle, and rooting for the New England Patriots.
In addition to my full-time job, I founded Rohawk, LLC which creates software for Christian churches.

## You may also be interested in...

 Pro Pro