Click here to Skip to main content
15,891,839 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sudoku
{
	public class GridClass
	{

		private List<int> Squares = new List<int>(Constants.BoardSize);
		private List<List<int>> SquarePossibilities = InitPossibilities();

		public GridClass(List<int> squares)
		{
			if (Squares.Count == 0)
			{
				for (int i = 0; i < Constants.BoardSize; i++)
				{
					Squares.Add(i);
				}
			}
			Squares = squares;
		}

		public List<int> GetAllSquares()
		{
			return Squares;
		}

		public List<int> GetRow(int rowNumber)
		{
			List<int> row = new List<int>(9);
			for (int i = 0; i < 9; i++)
			{
				row.Add(Squares[i + (rowNumber * 9)]);
			}
			return row;
		}

		public List<int> GetColumn(int columnNumber)
		{
			List<int> column = new List<int>(9);
			for (int i = 0; i < 9; i++)
			{
				column.Add(Squares[(i * 9) + columnNumber]);
			}
			return column;
		}

		public List<int> GetRegion(int regionNumber)
		{
			List<int> region = new List<int>(9);
			int verticalOffSet = (regionNumber / 3) * 3;
			for (int i = 0 + verticalOffSet; i < 3 + verticalOffSet; i++)
			{
				List<int> row = GetRow(i);
				int horizontalOffSet = (regionNumber % 3) * 3;
				for (int j = 0 + horizontalOffSet; j < 3 + horizontalOffSet; j++)
				{
					region.Add(row[j]);
				}
			}
			return region;
		}

		private static List<List<int>> InitPossibilities()
		{
			List<List<int>> list = new List<List<int>>();
			for (int i = 0; i < 80; i++)
			{
				list.Add(new List<int>() {1,2,3,4,5,6,7,8,9});
			}
			return list;
		}

	}
}

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