Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / MFC

A Sudoku Teacher Using Boost Libraries

Rate me:
Please Sign up or sign in to vote.
4.73/5 (27 votes)
7 Jul 20067 min read 68.2K   1.9K   60  
A Sudoku teacher using multi_index_container, lambda, and other Boost libraries.
// SBlock.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 SUDOKU_BLOCK_H_
#define SUDOKU_BLOCK_H_

#include "SudokuElement.h"
#include <boost/array.hpp>

// a block of 3x3 cells
class CSudokuBlock
{
public:
    CSudokuBlock() 
    {
    	prowsAllowingDigit = new boost::dynamic_bitset<>(3);
    	pcolsAllowingDigit = new boost::dynamic_bitset<>(3);
    };

    ~CSudokuBlock()
    {
    };

	boost::array<SudokuCell*, 9>& GetCollection()
	{
		return cells;
	};

    boost::dynamic_bitset<>* GetRowsAllowingDigit(int digit)
    {
        SetRowsAllowingDigit(digit);
        return prowsAllowingDigit;
    };

    boost::dynamic_bitset<>* GetColsAllowingDigit(int digit)
    {
        SetColsAllowingDigit(digit);
        return pcolsAllowingDigit;
    };

	bool EliminateDigitForRow(int row, int digit, std::string sReason)
    {
		return 
        cells[3*row]->EliminateDigit(digit, sReason) |
        cells[(3*row)+1]->EliminateDigit(digit, sReason) |
        cells[(3*row)+2]->EliminateDigit(digit, sReason);
    }

    bool EliminateDigitForCol(int col, int digit, std::string sReason)
    {
		return 
        cells[col]->EliminateDigit(digit, sReason) |
        cells[col+3]->EliminateDigit(digit, sReason) |
        cells[col+6]->EliminateDigit(digit, sReason);
    }

    boost::array<SudokuCell*, 9>& GetVectorOfCells() { return cells; }

private:
    boost::array<SudokuCell*, 9> cells;

	boost::dynamic_bitset<> *prowsAllowingDigit;
	boost::dynamic_bitset<> *pcolsAllowingDigit;

    void SetRowsAllowingDigit(int digit)
    {
                    // ROWS
        for (int rowsInCol = 0; rowsInCol < 3; ++rowsInCol)
        {
            int startSq = rowsInCol * 3;

            bool bPossible = cells[startSq]->DigitIsPossible(digit) ||
                            cells[startSq+1]->DigitIsPossible(digit) ||
                            cells[startSq+2]->DigitIsPossible(digit);
            (*prowsAllowingDigit)[rowsInCol] = (bPossible) ? 1 : 0;
        }
    }

    void SetColsAllowingDigit(int digit)
    {
                    // COLS
        for (int colsInRow = 0; colsInRow < 3; ++colsInRow)
        {
            int startSq = colsInRow;

            bool bPossible = cells[startSq]->DigitIsPossible(digit) ||
                            cells[startSq+3]->DigitIsPossible(digit) ||
                            cells[startSq+6]->DigitIsPossible(digit);
            (*pcolsAllowingDigit)[colsInRow] = (bPossible) ? 1 : 0;
        }
    }




};


#endif      // SUDOKU_BLOCK_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States 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.

Comments and Discussions