예제 #1
0
bool hiddenSingles(Puzzle &puzzle)
{
    for (int i = 0; i < 9; ++i)
        for (int j = 0; j < 9; ++j)
            if (puzzle.getCellNumber(i, j) == 0)
            {
                for (int k = 0; k < puzzle.getPossibleValues(i, j).size(); ++k)
                {
                    if (!puzzle.posInRegion(i, j, puzzle.getPossibleValues(i, j)[k]) || !puzzle.posInColumn(i, j, puzzle.getPossibleValues(i, j)[k]) || !puzzle.posInRow(i, j, puzzle.getPossibleValues(i, j)[k]))
                    {
                        puzzle.setCellNumber(i, j, puzzle.getPossibleValues(i, j)[k]);
                        puzzle.getPossibleValues(i, j).clear();
                        puzzle.updateRowColumnRegion(i, j);
                        return true;
                    }
                }
            }
    return false;
}
예제 #2
0
//return true if guess was correct
bool guess(Puzzle &puzzle, Puzzle &alternative)
{
    for (int i = 0; i < 9; ++i)
    {
        for (int j = 0; j < 9; ++j)
        {
            if ((puzzle.getCellNumber(i, j) == 0) && !(puzzle.getPossibleValues(i, j).empty()))
            {
                for (int k = 0; k < puzzle.getPossibleValues(i, j).size(); ++k)
                {
                    //make an alternative puzzle that assumes the puzzle is false
                    //MUST MAKE A DEEP COPY
                    alternative.erasePossibleValue(i, j, puzzle.getPossibleValues(i, j)[k]);
                    
                    //set number as guess
                    puzzle.setCellNumber(i, j, puzzle.getPossibleValues(i, j)[k]);
                    puzzle.getPossibleValues(i, j).clear(); //clear possible values after setting cell number
                    puzzle.updateRowColumnRegion(i, j);
                    return true;
                }
            }
            else if (puzzle.getCellNumber(i, j) == 0 && puzzle.getPossibleValues(i, j).empty())
                return false;
            //contradiction occured...
        }
    }
    return 0;
} //must return false for contradictions