Click here to Skip to main content
15,884,099 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 84.4K   2.2K   112  
An example of how to use Microsoft Solver Foundation on a constraint satisfaction problem.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Sudoku
{
    //Rename boards?
    public partial class SudokuGrid : UserControl
    {
        private List<SudokuSquare> sudokuSquares = new List<SudokuSquare>();
        private List<int> solution = new List<int>();
        private List<int> attempt = new List<int>();
        private List<int> problem = new List<int>();
        private List<Point> PositionList = new List<Point>();

        public SudokuGrid()
        {
            InitializeComponent();
            solution = GameController.GetSudokuSolution();
            problem = GameController.GetSudokuProblem();
            DrawSquares();
        }

        private void GetPossibleSolution()
        {
            attempt = new List<int>();
            int input = 0;
            foreach (SudokuSquare square in sudokuSquares)
            {
                Int32.TryParse((square.SudokuTextBox.Text), out input);
                attempt.Add(input);
            }
        }

        private void DrawSquares()
        {
            int squareNumber = 0;
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    SudokuSquare sudokuSquare = new SudokuSquare(problem[squareNumber], solution[squareNumber], row, column);
                    sudokuSquare.SudokuTextBox.TextChanged += new TextChangedEventHandler(TextChanged);
                    sudokuSquares.Add(sudokuSquare);
                    Canvas.Children.Insert(1, sudokuSquare);
                    squareNumber++;
                }
            }
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            GetPossibleSolution();
            GameController.IsSudokuSolved(attempt);
        }
    }
}

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