Click here to Skip to main content
15,892,298 members
Articles / Artificial Intelligence

Sudoku using Microsoft Solver Foundation

Rate me:
Please Sign up or sign in to vote.
4.85/5 (36 votes)
26 May 2014CPOL6 min read 85.1K   2.2K   112  
An example of how to use Microsoft Solver Foundation on a constraint satisfaction problem.
using System.Collections.Generic;
using System.Windows;

namespace Sudoku
{
    public static class GameController
    {
        public static void GenerateNewGame()
        {
            SudokuProblem.GenerateProblem();
        }

        public static List<int> GetSudokuSolution()
        {
            if (SudokuProblem.GetSolution().Count == 0)
            {
                SudokuProblem.GenerateProblem();
            }
            return SudokuProblem.GetSolution();
        }

        public static List<int> GetSudokuProblem()
        {
            if (SudokuProblem.GetProblem().Count == 0)
            {
                SudokuProblem.GenerateProblem();
            }
						if (!SudokuProblem.HasUniqueSolution())
						{
							MessageBox.Show("Multiple solutions");
						}
            return SudokuProblem.GetProblem();
        }

        public static void IsSudokuSolved(List<int> solution)
        {
            if (solution == null)
            {
                return;
            }

            if (!solution.Contains(Constants.PlaceHolder))
            {
                if (SudokuProblem.IsSolved(solution))
                {
                    MessageBox.Show("Congratulations!");
                }
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Code Rain
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions