bool InCollectionSolver::SolveSquare(Coords coords) { bool didSomething = false; Square * toSolve = _theBoard->GetSquare(coords); const vector<int> values = toSolve->Values(); if (CheckIn(toSolve, _theBoard->GetCol(coords), coords.YCoord())) { _stats[ColToken]++; didSomething = true; } if (CheckIn(toSolve, _theBoard->GetRow(coords), coords.XCoord())) { _stats[RowToken]++; didSomething = true; } if (CheckIn(toSolve, _theBoard->GetArea(coords), _theBoard->IndexInAreaMap(coords))) { _stats[AreaToken]++; didSomething = true; } // Return whether we have managed to eliminate any possibilities return didSomething; }
bool UniqueInCollectionSolver::SolveSquare(Coords coords) { bool madeProgress = false; Square * toSolve = _theBoard->GetSquare(coords); // For this solver, as we will solve the square as soon as we make progress // there is no point in checking any other collections as soon as one returns // true if (CheckForUniqueSolution(_theBoard->GetArea(coords), toSolve, _theBoard->IndexInAreaMap(coords))) { _stats[AreaToken]++; madeProgress = true; } if (!madeProgress && CheckForUniqueSolution (_theBoard->GetRow(coords), toSolve, coords.XCoord())) { _stats[RowToken]++; madeProgress = true; } if (!madeProgress && CheckForUniqueSolution(_theBoard->GetCol(coords), toSolve, coords.YCoord())) { _stats[ColToken]++; madeProgress = true; } return madeProgress; }
// Make a mapping between some coordinates on the board an an integer // representing the area it is part of. So for a 3x3 board this will // look like: // 000|111|222 // 000|111|222 // 000|111|222 // ----------- // 333|444|555 // 333|444|555 // 333|444|555 // ----------- // 666|777|888 // 666|777|888 // 666|777|888 // int Board::GetAreaIdForCoords(Coords coords) { int areaSize = GetAreaSideSize(); // Assumes we down fractions down when assigning to int int xComponent = coords.XCoord() / areaSize; int yComponent = coords.YCoord() / areaSize; return yComponent * areaSize + xComponent; }
int Board::IndexInAreaMap(Coords coords) { // If we are using the boards area map, derive the index in the area // returned which signifies the square at the x/y coords. This assumes // that the squares in an area are populated by x coord, and then by // y. int areaSideSize = GetAreaSideSize(); return coords.XCoord() % areaSideSize + (areaSideSize * (coords.YCoord() % areaSideSize)); }
bool Board::IsSquareValid(Coords coords, int solvedValue) { // Check that the solved value of this square doesn't appear in any of // the rows, columns or areas it belongs to as a solved value too if (ContainsSolvedValue(GetRow(coords), solvedValue, coords.XCoord()) || ContainsSolvedValue(GetCol(coords), solvedValue, coords.YCoord()) || ContainsSolvedValue(GetArea(coords), solvedValue, IndexInAreaMap(coords))) { cout << "Found invalid repeated value of " << solvedValue << endl; return false; } return true; }