Exemple #1
0
    public void SolveBoard()
    {
        solver.Initialise(sudoku);
        solver.settings.bruteForce = false;
        var result = solver.Solve();

        Debug.Log(result);
        for (int i = 0; i < sudoku.settings.numHorizontal; i++)
        {
            for (int j = 0; j < sudoku.settings.numVertical; j++)
            {
                if (solver.GetValue(i, j) != "")
                {
                    SetFull(i, j, solver.GetValue(i, j).ToString(), false);
                }
                else
                {
                    boxes[i, j].Clear(false);
                    foreach (string x in solver.final.possible_values[i, j])
                    {
                        boxes[i, j].ToggleCentre(x, false);
                    }
                }
            }
        }
    }
Exemple #2
0
    public SolveResult Solve()
    {
        for (int i = 0; i < final.sudoku.settings.numHorizontal; i++)
        {
            for (int j = 0; j < final.sudoku.settings.numVertical; j++)
            {
                // Trigger the propogate for all already set vlaues.
                if (GetValue(i, j) != "")
                {
                    SetValue(i, j, GetValue(i, j));
                }
            }
        }
        SolveResult s = ApplyRules();

        if (s == SolveResult.Impossible)
        {
            return(s);
        }
        if (s == SolveResult.Solved)
        {
            return(s);
        }
        // In any other case, we can continue solving.
        if (!settings.bruteForce)
        {
            return(s);
        }
        // Find the smallest possible switch box
        (int x, int y)bestPoint = (-1, -1);
        int best = final.sudoku.AllValues().Count + 1;

        for (int i = 0; i < final.sudoku.settings.numHorizontal; i++)
        {
            for (int j = 0; j < final.sudoku.settings.numVertical; j++)
            {
                if ((final.possible_values[i, j].Count < best) && (final.possible_values[i, j].Count != 1))
                {
                    best      = final.possible_values[i, j].Count;
                    bestPoint = (i, j);
                }
            }
        }
        // Split on (i, j)
        int        amountSolved     = 0;
        int        amountIncomplete = 0;
        SolveState newFinal         = new SolveState();

        foreach (var k in final.possible_values[bestPoint.x, bestPoint.y])
        {
            BoardSolver bs = new BoardSolver();
            bs.Initialise(final, settings);
            var r = bs.Solve();
            if (r == SolveResult.Solved)
            {
                amountSolved++;
                newFinal = bs.final;
            }
            if (r == SolveResult.Incomplete)
            {
                amountIncomplete++;
                if (amountSolved == 0)
                {
                    newFinal = bs.final;
                }
            }
            if (r == SolveResult.MaybeMultiple)
            {
                newFinal = bs.final;
                amountSolved++;
                amountIncomplete++;
            }
            if (r == SolveResult.Multiple)
            {
                return(r);
            }
            if (r == SolveResult.Impossible)
            {
                continue;
            }
        }
        if ((amountSolved == 0) && (amountIncomplete == 0))
        {
            return(SolveResult.Impossible);
        }
        final = newFinal;
        if (amountSolved > 1)
        {
            return(SolveResult.Multiple);
        }
        if (amountIncomplete == 0)
        {
            return(SolveResult.Solved);
        }
        if (amountSolved == 0)
        {
            return(SolveResult.Incomplete);
        }
        return(SolveResult.MaybeMultiple);
    }