Click here to Skip to main content
15,885,546 members
Articles / Multimedia / GDI+
Tip/Trick

Sudoku Project in VS 2010

Rate me:
Please Sign up or sign in to vote.
4.85/5 (8 votes)
16 Dec 2013CPOL2 min read 49K   2.5K   26   11
Sudoku game solver with a "SudokuControl"

Introduction

This application tries to solve any Sudoku game. It applies all basic Sudoku logic to discover missing numbers:

  1. Test of possible numbers in a cell by scanning all possibilities in that cell's row, column and square.
  2. For each row, column and square, scan all numbers (from 1 to 9) to fit a number in its cell (for example: In the third row, number 5 can only be in the second cell because none of the other cells support it.)
  3. Finally, if no searches of the previous types are found, trial and error starting in those cells that have least number of possibilities.

I made a YouTube playlist of the entire project. I was recording and writing the code at the same time.

Background

Almost twelve (about 2000) years ago, I encountered the Sudoku game and it was love at first sight. I was studying Math in Faculdade de Ciências da Universidade do Porto (Portugal), and immediately I made an application to solve it (I programmed in Visual Basic 6 since I was 17). That application solved it with no problem. Now in 2013, for the first time, I'm unemployed since my degree (Portuguese economy sucks) and I decided to make a "remake" of that application because I've seen some bad Sudoku applications (by design). Because I've been teaching C#, SQL, and Math these last years, I think I can make a good app...

Using the Code

Image 1

The app consists of:

  • A set of classes representing the Sudoku Game and structure
  • A control named SudokuControl to present the game.

The classes that I've created are SudokuGame, SudokuCell, SudokuLine, SudokuColumn, SudokuSquare and an abstract class SudokuCellGroup that represents any set of nine SudokuCells. For that reason, SudokuLine, SudokuColumn, and SudokuSquare inherit from this class. Each of these classes have methods to validate the Sudoku game, to search for unknown numbers, etc.

Here is the code for SudokuCellGroup:

C#
using System;
using System.Collections.Generic;
using System.Text;
namespace Sudoku
{
    public abstract class SudokuCellGroup
    {
        public abstract SudokuCell this[int index]
        {
            get;
        }
        public SudokuGame Game { get; set; }
        public SudokuCellGroup(SudokuGame game)
        {
            this.Game = game;
        }
        public bool Validate()
        {
            List<int> ints = new List<int>();
            for (int i = 0; i < 9; i++)
            {
                if (this[i].HasValue)
                {
                    if (ints.Contains(this[i].Value))
                        return false;
                    else
                        ints.Add(this[i].Value);
                }
            }
            return true;
        }
        public List<SudokuCell> GetPossibleCells(int value)
        {
            List<SudokuCell> cls = new List<SudokuCell>();
            for (int i = 0; i < 9; i++)
                if (this[i].GetPossibleValues().Contains(value))
                    cls.Add(this[i]);
            return cls;
        }
   }
}

Here is the code for SudokuCell that returns the possible numbers to place in that cell in a given moment:

C#
public List<int> GetPossibleValues()
{
    List<int> lst = new List<int>();
    if (!this.HasValue)
    {
        lst.Add(1); lst.Add(2); lst.Add(3); lst.Add(4); lst.Add(5);
        lst.Add(6); lst.Add(7); lst.Add(8); lst.Add(9);

        for (int i = 0; i < 9; i++)
        {
            SudokuCell item = this.Line[i];
                
            if (item != this)
                if (item.HasValue)
                    if (lst.Contains(item.Value))
                        lst.Remove(item.Value);
        }

        for (int i = 0; i < 9; i++)
        {
            SudokuCell item = this.Column[i];
            if (item != this)
                if (item.HasValue)
                    if (lst.Contains(item.Value))
                        lst.Remove(item.Value);
        }
        for (int i = 0; i < 9; i++)
        {
            SudokuCell item = this.Square[i];
                
            if (item != this)
                if (item.HasValue)
                    if (lst.Contains(item.Value))
                        lst.Remove(item.Value);
        }
    }

    return lst;
}

History

Current version. No changes made. The project can be downloaded fully here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer
Portugal Portugal
Hello, my name is Rui Carvalho and I'm a Math teacher in Portugal. I Studied in 'Faculdade de Ciências da Universidade do Porto' and I've been programming since I was 16 years old. I'm an algorithms expert. I started in my CASIO calculator and then moved up to Visual Basic 6 (in my lovely Pentium 233 Mhz). Then I learned JAVA and c# as soon as possible. I've been programming in c# since then. For my databases I use SQL server and MySQL. I teach JAVA, C#, T-SQL to software developers (between my Math classes).
I worked in a software company for 3 years and I loved it. I spent those 3 years changing the base routines from VB6 to C# in many applications. I learned a lot about Taxes, Billing, Payments, Production,Company Management, Server Management,Synchronization,FTP, etc...
Today, unfortunately, I'm unemployed.
(Portugal is living a very very bad economic crisis)

Comments and Discussions

 
General加油 Pin
Member 1094267021-Jul-15 16:01
Member 1094267021-Jul-15 16:01 
SuggestionAlgorithm Update Pin
arpit007-from-New-Delhi9-Jun-14 19:02
professionalarpit007-from-New-Delhi9-Jun-14 19:02 
GeneralRe: Algorithm Update Pin
Rui Jorge Carvalho12-Aug-15 5:16
professionalRui Jorge Carvalho12-Aug-15 5:16 
QuestionBugs fixed Pin
Rui Jorge Carvalho21-Apr-14 13:33
professionalRui Jorge Carvalho21-Apr-14 13:33 
Questiongreat Pin
Mahdi Nejadsahebi20-Apr-14 21:43
Mahdi Nejadsahebi20-Apr-14 21:43 
QuestionError in Program Pin
arpit007-from-New-Delhi1-Feb-14 6:11
professionalarpit007-from-New-Delhi1-Feb-14 6:11 
Hi,
I was going through the program and found a bug.
you can see that the input game is faulty and has no solution, but the program assumes it to be a valid game and starts an infinite loop.
100000000000100000000000002000000100000000000000000000000000010000000000000000000
Kindly go through similar kinds of error prone test cases.
Thanks!
AnswerRe: Error in Program Pin
Rui Jorge Carvalho19-Apr-14 12:12
professionalRui Jorge Carvalho19-Apr-14 12:12 
AnswerErrors Pin
Pinta4313-Nov-13 1:28
Pinta4313-Nov-13 1:28 
GeneralValidation missing... Pin
Rui Jorge Carvalho15-Dec-13 23:43
professionalRui Jorge Carvalho15-Dec-13 23:43 
GeneralErrors in solving Pin
Pinta4323-Oct-13 2:49
Pinta4323-Oct-13 2:49 
GeneralRe: Errors in solving Pin
Rui Jorge Carvalho12-Nov-13 12:13
professionalRui Jorge Carvalho12-Nov-13 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.