You are on page 1of 50

2011

 
 

   

Sudoku  Game  
 
 
Third Year Project Report

University of Manchester
School of Computer Science
 
 
 

 
 
 
 
 
Student  Name:     Ahmed  Abdulkarim  Almuhrij  

Degree  Programme:   BSc (Hons)  Software  Engineering    

Course  Supervisor:   Dr.  David  Lester  

Date  Submission:     4th  May  2011  

University  of  Manchester  


 
SUDOKU  GAME   May  4,  2011  
 

Abstract  
 

Sudoku game is well famous and popular game among many players all over the world. This report
details the development of a Sudoku game application that is written in Java. The application is even
developed to work in Android systems.

In addition, the report details the implementation of the complexity of the algorithms used to solve
any kind of Sudoku puzzle. Also, how to generate a puzzle with different level of difficulties and
make sure there will be only one solution.

The aim of the report is also to discuss the backtracking, brute force algorithms and other logics in
order to create and solve Sudoku puzzles. Furthermore, the user-friendly environment is considered in
the report as the rules of Sudoku are connected to the interface.

Moreover, the report specifics how well these methods of solving solves the puzzle and achieved the
goal of this implementation. The report concludes by evaluating the end application to analyse how
good it met its objectives and the performance of solving algorithms. Finally, the report summarises
the overall achievements of the application development and indicates other possible extensions.

Project  Title:     Sudoku  Game  

Author:     Ahmed  A.  Almurhij  

Date:       4th  May  2011  

Supervisor:     Dr.  David  Lester  

University  of  Manchester  |  Abstract   2  


 
SUDOKU  GAME   May  4,  2011  
 

Acknowledgments  
 

I would like to thank my supervisor, Dr. Dave, for his support and feedbacks. His advises was
valuable and helpful for me all the time.

I would also thank my family and friends for their praying and encouraging me to finish the
work. Also, I would like to thank Ibrahim, Abdullah and Abdulaziz for their valuable
feedbacks. The main picture window designed by my brother Abdulrahman, so I would like
to thank him for his effort.

Finally, Special thanks for my mum who always contacted me to make sure I do the right
things and keep tracking of my development in the whole project. Without her, I don’t think I
could do what I have done so far.  

   

   

University  of  Manchester  |  Acknowledgments   3  


 
SUDOKU  GAME   May  4,  2011  
 

Contents  
1   Introduction .................................................................................................................. 7  
1.1   Sudoku  and  Its  Rules  ................................................................................................................  7  
1.2   Project  Proposal  .......................................................................................................................  8  
1.3   Existing  Implementations  .........................................................................................................  9  
1.4   Aims  and  Objectives  ...............................................................................................................  10  
1.5   Tackling  the  Problem  ..............................................................................................................  11  
1.6   Report  Overview  ....................................................................................................................  13  
2   Background and Literature survey ........................................................................14  
2.1   Rules  Applied  to  My  Sudoku  Program  ....................................................................................  14  
2.2   Existing  Solver  and  generator  Implementations  ....................................................................  14  
2.2.1   Guy  Hoghton’s  Sudoku  Solver  Implementation  ..............................................................  14  
2.2.2   Nick  Smallbone’s  Sudoku  Solver  Implementation  ...........................................................  15  
2.2.3   Pramberg’s  Sudoku  Generator  Implementation  .............................................................  15  
2.3   Algorithms  for  Solving  Sudoku  ...............................................................................................  16  
2.3.1   Sudoku  and  Exact  Cover  Problem  ...................................................................................  16  
2.3.2   Backtracking  Algorithm  to  Solve  Sudoku  .........................................................................  17  
2.3.3   Brute-­‐force  Algorithm  to  Solve  Sudoku  ...........................................................................  17  
2.3.4   Stochastic  Search  to  Solve  Sudoku  ..................................................................................  18  
2.4   Logics  to  Solve  Sudoku  Puzzles  ...............................................................................................  18  
2.4.1   Naked  Single  ....................................................................................................................  19  
2.4.2   Hidden  Single  ...................................................................................................................  19  
2.4.3   Locked  Candidates  (Pointing  Pair)  ...................................................................................  20  
2.4.4   Naked  Pair  .......................................................................................................................  21  
2.4.5   Hidden  Triple  ...................................................................................................................  22  
2.4.6   X-­‐Wing  .............................................................................................................................  22  
2.5   NP-­‐Complete  Problem  ............................................................................................................  23  
2.6   Platforms  and  software  ..........................................................................................................  24  
3   Design ............................................................................................................................25  
3.1   Code  structure  ........................................................................................................................  25  
3.2   Features  ..................................................................................................................................  27  
3.3   GUI  ..........................................................................................................................................  28  
 

University  of  Manchester  |  Contents   4  


 
SUDOKU  GAME   May  4,  2011  
 
 

4   Implementation ...........................................................................................................29  
4.1   Implementing  Sudoku  Solver  .................................................................................................  29  
4.2   Implementing  Sudoku  Generator  ...........................................................................................  32  
4.3   Implementing  Sudoku  Game  ..................................................................................................  33  
4.3.1   Conflict  Check  ..................................................................................................................  33  
4.3.2   Hints  ................................................................................................................................  34  
4.3.3   Statistics  ..........................................................................................................................  34  
4.4   Implementing  Create  Solver  ...................................................................................................  34  
4.5   Implementing  Save/Load  .......................................................................................................  35  
5   Results ...........................................................................................................................36  
5.1   Sudoku  Game  .........................................................................................................................  36  
5.1.1   File  toolbar  ......................................................................................................................  36  
5.1.2   Save/Load  Puzzles  ...........................................................................................................  38  
5.1.3   Game  Level  and  Pause  Game  ..........................................................................................  39  
5.1.4   Conflict  Check  and  Hints  ..................................................................................................  39  
5.1.5   Restart  and  Give  Up  ........................................................................................................  40  
5.1.6   Submit  .............................................................................................................................  41  
5.2   Game  Solver  ...........................................................................................................................  42  
6   Testing ...........................................................................................................................45  
6.1   Test-­‐Driven  Development  (TDD)  ............................................................................................  45  
6.2   Feedbacks  ...............................................................................................................................  46  
7   Evaluation ....................................................................................................................47  
7.1   Unit  Testing  ............................................................................................................................  47  
7.1.1   Solver  and  Generator  ......................................................................................................  47  
7.2   Features  and  GUI  ....................................................................................................................  48  
8   Conclusions ..................................................................................................................49  
8.1   Achievements  Obtained  .........................................................................................................  49  
8.2   Future  Extensions  ...................................................................................................................  49  
9   References ....................................................................................................................50  
 

University  of  Manchester  |  Contents   5  


 
SUDOKU  GAME   May  4,  2011  
 

List  of  Figures  


Figure 1: Sudoku puzzle  ........................................................................................................................  7  
Figure 2: An Example for Naked Single  ..............................................................................................  19  
Figure 3: An Example for Hidden Single  ............................................................................................  20  
Figure 4: An Example for Locked Candidates 1  ..................................................................................  20  
Figure 5: An Example for Locked Candidates 2  ..................................................................................  21  
Figure 6: X-Wing Example  ..................................................................................................................  23  
Figure 7: Project Structure  ...................................................................................................................  25  
Figure 8: Package interactions  .............................................................................................................  26  
Figure 9: checkConflict function  .........................................................................................................  29  
Figure 10: count the number of empty cells  .........................................................................................  30  
Figure 11: collect the possibilities for each cell  ...................................................................................  31  
Figure 12: Example for collecting possibilities  ...................................................................................  31  
Figure 13: Sudoku game in medium level  ...........................................................................................  36  
Figure 14: File contents  .......................................................................................................................  37  
Figure 15: Statistics - screenshot  .........................................................................................................  37  
Figure 16: Options - screenshot  ...........................................................................................................  38  
Figure 17: Save Game - screenshot  ......................................................................................................  38  
Figure 18: continue Saved Game - screenshot  .....................................................................................  38  
Figure 19: Game Levels coloured - screenshot  ....................................................................................  39  
Figure 20: conflict Check example - screenshot  ..................................................................................  39  
Figure 21: hints example - screenshot  ..................................................................................................  40  
Figure 22: Give up - screenshot  ...........................................................................................................  40  
Figure 23: Empty Cells Exist - screenshot  ...........................................................................................  41  
Figure 24: congratulations - screenshot  ...............................................................................................  41  
Figure 25: Failed solution - screenshot  ................................................................................................  41  
Figure 26: Create Sudoku Game - screenshot  ......................................................................................  42  
Figure 27: File toolbar for game solver - screenshot  ...........................................................................  42  
Figure 28: conflict inputs - screenshot  .................................................................................................  43  
Figure 29: More 1 Solution Alert - screenshot  .....................................................................................  43  
Figure 30: Save Created Game - screenshot  ........................................................................................  43  
Figure 31: Load Create Game - screenshot  ..........................................................................................  44  
Figure 32: Not Solvable Alert - screenshot  ..........................................................................................  44  
Figure 33: TDD Steps  ..........................................................................................................................  45  
Figure 34: Algorithms Performance for 400 puzzles  ...........................................................................  48  

University  of  Manchester  |  List  of  Figures   6  


 
SUDOKU  GAME   May  4,  2011  
 

1 Introduction  
1.1 Sudoku  and  Its  Rules  

Originally, Sudoku is a Japanese puzzle game. It means “Single Number” or “Number


Place”. The game consists of a 9x9 grid, which is divided into nine 3x3 grids (called
boxes or sub-grids). So, there are 9 rows and 9 columns. The intersection of a row and
a column is called a cell. The result of the total cells is 81. Figure 1(a) is an example of
a Sudoku puzzle with empty grid. When start the Sudoku puzzle, a player will be
provided with random numbers in random cells which called givens; figure 1(b) shows
an example of real Sudoku. The objective is to fill the empty cells with only one
number from 1 to 9 in the shortest time. There are 3 rules in order to complete and
accomplish all of the empty cells:

1) The number must only occur in a column once.


2) The number must only occur in a row once.
3) The number must occur in a sub-grid only once.

Moreover, this means that the solution of any puzzle should have one solution that is
unique. The difficulty of the puzzle depends not only how many numbers are given, but
also on the placement of the given cells.

(a)  Empty  grid   (b)  Givens  placed  

  Cell     Row     Column   Sub-­‐grid     Given  cell  


 

Figure 1: Sudoku puzzle

However, Sudoku is a logic-based, so there is no specific arithmetic required to solve


the puzzle. In addition, other puzzles could be created which may replace the numbers
with different objects such as letters or colours [10].

University  of  Manchester  |  Introduction   7  


 
SUDOKU  GAME   May  4,  2011  
 
1.2 Project  Proposal  

Before starting the final year, I have selected my project to be workout cell puzzles.
During the summer and first week on the final year, I have decided to work on a
Sudoku puzzle. I have chosen to work on this game because it was interesting for me.
Moreover, the logic and algorithms for solving the puzzle was motivating and
challenging. At that time, I planned to improve the algorithms of this game to be more
efficient and to make a user-friendly environment.

First of all, I wrote down some question that would help me to start working on the
project:

1) What kind of the software development methodology should I use?


2) Does the project aim to something realistic or not?
3) What algorithms should be used to solve the puzzle in efficient time?

Having these questions, which are written in my logbook, I decided first to look for
answers in order to start with confidence. The discussion made with my supervisor, Dr
Dave, and with some research, I found the answer for the first question. There were 3
software methodologies I considered to work on:

1. Waterfall development,
2. Rapid application development (RAD), and
3. Agile software development.

Each one of these methodologies has some advantageous and disadvantageous for my
project. I decided to work on agile software methodology. The reasons why I selected it
were because I was taking an agile software course at that time. So, I thought using this
methodology would help me to gain more knowledge of the course, which will help me
to study. Moreover, the agile methodology is based on iterative and incremental
development. As the agile software states four important manifesto items:

1. Individuals and interactions.


2. Working software.
3. Customer collaboration.
4. Responding to change.

However, in my project, I needed to work with iterations. Each iteration requires five
stages; the requirements, design, implementation, testing and evaluation and
maintenance. I knew that if I used this methodology, it will make me more assurance
about how my progress is going on. During the first weeks of my project, I did not have
all my requirements. In addition, I used to change some requirements in order to
improve my program.

University  of  Manchester  |  Introduction   8  


 
SUDOKU  GAME   May  4,  2011  
 
As a result, using this methodology will not make me suffer as dealing with waterfall
development. In waterfall development, the framework is linear. This means, I had to
have all the requirements and the whole plan in order to go to the next stage. So, I could
not rely on waterfall design.

While in rapid application development, it was an interesting methodology to work on.


In this methodology, the workflow will be faster and involves iterations. The reason
why I did not use it after the consideration is the iteration has a fixed and restricted
timeboxing, which was not appropriate for my project.

The second question is to know if the proposal is realistic and reachable for me. I have
some experience about the rules of Sudoku puzzle and some programming languages
such as Java and Prolog. Moreover, I did some research to look for the algorithms of
solving the puzzles. In the end, I knew the project was realistic and implementable. The
hard and main thing for me was coding the algorithms. However, during the past 3
years in the University of Manchester, I learned how to program in different languages.
So, I was confidence that I could do the project in an efficient way.

Finally, the proposal of my project was to design and implement a Sudoku game
application. I needed to know and look for algorithms that are used to generate and
solve the puzzle in efficient time. These algorithms are implemented in the application
and it’s explained later in sections 2 and 4.

1.3 Existing  Implementations  

In this section, it shows some implementations that are already available in the Internet.
I used to play Sudoku game in the past either by using the Internet or a phone
application. First, I wrote some notes about 2 Sudoku applications I have found in the
Internet:

1) Web Sudoku: this is available at http://www.websudoku.com


• The program looks simple. However, the user needs to click in each cell in
order to fill the cell with a number. Also, the conflict checking is not
working all the time. The user needs to click on “How am I doing?” in
order to highlight the conflict numbers. Moreover, the highlight is not
removed until the user click again on the same button.

2) Sudoku Live: this is available at http://www.sudokulive.net/sudoku.php


• It’s similar to the previous one. However, the user can write anything
inside the cells. Conflict check is not clear for new starters. On the other
hand, before start solving the puzzle, it provides the solution methods
needed to solve it.

In both of them, the interface of the program is boring. There are no features to make
the game more enjoyable.

University  of  Manchester  |  Introduction   9  


 
SUDOKU  GAME   May  4,  2011  
 
Moreover, there are open sources for different algorithms, which are available in the
Internet. The area I needed to look at is about solving and generating a Sudoku puzzle.

First, introducing two implementations available online for solving a Sudoku puzzle;

1) Guy Hoghton’s Sudoku solver:


The code is written by him and is available online. Anyone can see the
source code by seeing the HTML mark-up for the page. The implementation
used in his code is a depth-first search technique. This algorithm is able to
search through the puzzle in order to find the right solution [1].

2) Nick Smallbone’s Sudoku Solver:


The code is available online. The algorithm is originally written in python
and it’s written in C++ as well. Looking at the code, it looks like the code
implements a depth-first search and best-first search to look for a solution [8].

Second, there are some implementations available online for generating a Sudoku
puzzle. For example, Jörgen Pramberg has written the code for generating the Sudoku
puzzle. This is an open source which is available online. He states that the generated
Sudoku puzzle should only have one solution. Otherwise, the puzzle is considered as
not a real Sudoku [6].

1.4 Aims  and  Objectives  

The first objective of the project is to create a Sudoku solver algorithm. There are some
known algorithms used in order to solve a Sudoku puzzle. I have been interesting in
three of these algorithms;

1) Backtracking algorithm [9].


2) Brute-force algorithm [7].
3) Stochastic search (optimization methods) [5].

In addition, there are logical methods that could be used in order to lead to the solution.
It was an aim for me to code these methods and connect them to the algorithms
mentioned above. So, this will make the solving faster.

The second objective is to construct a random Sudoku generator. However, in order to


generate a real Sudoku puzzle, the solver will check if the generated puzzle only has
one solution. It needs to automatically generate puzzles in advanced for each level in
order to have less time loading.

As mentioned before, the Graphical User Interface needs to be entertaining and user-
friendly environment. As I looked to several Sudoku programs, I tried to note the
disadvantages in order to improve them in my project. So, my other aim is to create an
interface with different features to make it more interesting and attractive to users.

University  of  Manchester  |  Introduction   10  


 
SUDOKU  GAME   May  4,  2011  
 
1.5 Tackling  the  Problem    

The aims and objectives mentioned in the previous section needed to be done in a
scheduled time. I have defined some milestones in order to lead and direct me to the
right path of doing the project on time. Also, this helped me to keep tracking the
problems and when to start each of the sub-goals I had in my project. The milestones
are shown in the table below.

Milestones Work being done

A Coding and implementing “Backtracking Algorithm”


B Test 1 to insure algorithm works fine
1 Solver implementation
C Coding and implementing “Brute-force Algorithm”
D Test 2 to insure algorithm works fine
A Coding and implementing random puzzle creator
Adapt program to make sure only one solution for each
B
created puzzle
2 Generator implantation
Implement the generator for 3 different levels (easy-
C
medium-hard)
D Test 3 to insure generated puzzle is a real Sudoku puzzle
A Create the interface for game
Implement setting for cells to accept only 1 number from 1
B
to 9
3 Construct GUI C Connect solver and generator code to the interface
D Implementing the conflict check highlight
Test 4 to insure the output in the interface are correct and
E
the conflict check works fine
Implement more options to user
Adapt program to allow users to change cells and
I
background colours
Adapt program to allow users to save and load puzzles
A II
for different levels
4 Features Adapt program to colour the valid and invalid
III
numbers when giving up is clicked
IV Implement the timer and save it for each level
Create hidden file for user puzzles, colour applied and
B
statistics to be saved in and loaded from.
C Test 5 to make sure all features work as implemented

University  of  Manchester  |  Introduction   11  


 
SUDOKU  GAME   May  4,  2011  
 
However, I have done these milestones on the time. So, I have the time to add more
work and sub-goals into the project and make it more unique. To do more testing and
searching for feedbacks, I have created the program to work in windows machines.
Therefore, I was enabled to send the program to people in order to have feedbacks,
which helped me to tackle the problems for later implementations. The two sub-goals
added to the project are explained in the table below.

Milestones Work being done


A Adapt program to accept a valid user puzzle to solve
B Show more than one solution if possible
5 Create puzzle
C Create GUI with same features provided before
D Test 6 to ensure the solution for a given puzzle is valid
A Coding and implementing logical solving algorithms
More solver B Connect it to previous solving to improve efficiency
6
implementation C Adapt program to switch and select faster algorithm
D Test 7 to make sure the solving is correct and efficient

Furthermore, to make the application available on any android software, I added


iteration for implementing this application on different platform. The table below
explain the main points of this goal.

Milestones Work being done


A Coding the generating Sudoku
B Coding the solving Sudoku
7 Android Version C Implementing the GUI
D Implementing the features
E Test 8 to ensure the application works fine

University  of  Manchester  |  Introduction   12  


 
SUDOKU  GAME   May  4,  2011  
 
1.6 Report  Overview  

The report structured is divided into sections. Each of the section is dealing with a
particular area.
Section 2: Background and Literature Survey

In this section, a description of the project and more details about the existing
algorithms introduced before. More details about the algorithms mentioned before
and logics for solving the game are explained.
Section 3: Design
It covers the application design for the main aspects; code structure, features and
Graphical User Interface. Also, it presents some diagrams that explain the overall
design and packages.
Section 4: Implementations
The section details the code implementations for the application. Also, it has
some screenshots for the code implemented and pseudo code for other
implementations.
Section 5: Result
In this section, it explains how the system works with screenshots of the
application.
Section 6: Testing
It details how the test is being implemented at the end of iterations. Moreover, it
explains how the feedbacks have been collected.
Section 7: Evaluation
Evaluating the final application with some testing. It details if the application has
achieved its objectives.
Section 8: Conclusion
This section concludes the report with what have been achieved. In addition, it
discusses future activities that could be added to the application.

University  of  Manchester  |  Introduction   13  


 
SUDOKU  GAME   May  4,  2011  
 

2 Background  and  Literature  survey  


2.1 Rules  Applied  to  My  Sudoku  Program  

When I start thinking about the project, I had to come up with rules. These rules keep
my program efficient and more sensible to use. However, the Sudoku puzzle has its
own rules that I have included to my program. The rules are mentioned before are:

1) The number must only occur in a column once.


2) The number must only occur in a row once.
3) The number must occur in a sub-grid only once.

In order to make my program even better, I set some additional rules:

1) The puzzle must be real Sudoku puzzle.


– Only has one solution.
2) The puzzle must be solvable by the logic.
– This should make it easier for a user to solve the puzzle.
3) The puzzle must be symmetrical.
− The pattern of a given numbers in the cells has reflectional and rotational
symmetry or one of them.
4) The puzzle must have 9x9 grids.
– This means 9 3x3 sub-grids, 9 rows, 9 columns and 81 cells.
5) Each cell in the puzzle must accept one number from 1 to 9.

I have implemented all these rules after doing some research for different Sudoku
programs. The idea was to introduce a program with all the features from different
program and to improve some of them to make it more unique.

2.2 Existing  Solver  and  generator  Implementations  

Before explaining the pure algorithms for solving the Sudoku puzzles, there are three
sub-sections explaining more about existing implementations. The report has
mentioned them in the introduction.

2.2.1 Guy  Hoghton’s  Sudoku  Solver  Implementation  

Guy Hoghton has written the code for solving different puzzles. It’s implemented in
JavaScript. Reading through the code, it appears to implement a depth-first search
technique in order to solve the puzzle. It uses two 2-dimension arrays and one 1-
dimention array. The first two is called matrix and predef. The matrix is for
holding the values of each cell. However, the predef is a Boolean array to check
whether the cell has a given number or not. There are also check functions to
determine whether the number is valid in the cell or not. In order to solve the puzzle,
the function solve() is called.

University  of  Manchester  |  Background  and  Literature  survey   14  


 
SUDOKU  GAME   May  4,  2011  
 
The procedure is explained as follow:
1. If the cell has predef set to false (which means no given number) and it is
less than 9, then add one to its value.
2. Then call the check functions to decide if the value placed is valid or not.
I. If the value is valid, then go to the next empty cell.
II. If the value is not valid, stay in the same cell. (This action illustrate
that this algorithm is using depth first search technique).
3. If it stays in the same cells and increment its value until 9 and does not find
any right solution for it, it goes to the previous cell and tries to increment its
value.
4. The puzzle might have no solution. That it determine with the rest of if-else
statements [1].

2.2.2 Nick  Smallbone’s  Sudoku  Solver  Implementation  

The algorithm is to solve almost any Sudoku puzzle. I went across the code in order
to understand the idea of his implementation.
First thing to do is to go through all empty cells and check for all possibility
numbers from 1 to 9 that can be placed on it. The algorithm uses an array to have all
the possibilities for each cell. Looking at the code, it looks like the code implements
a depth first search and best first to look for a solution. In order to solve the puzzle,
the procedure below describes it;
1. The function findMin() is called to find the cell with smallest number of
possibility.
2. If a cell has only one valid possibility, then fill this number in the cell.
3. If the cell has more than one possibility, then it calls solve() method
which continue recursively until all cells are occupied.
4. Then the solution is found [8].

This algorithm provides a new way to have a list of all possible valid numbers for
each cell. As a result, this improves the performance comparing to Guy’s solver that
uses an array.

2.2.3 Pramberg’s  Sudoku  Generator  Implementation  

Jörgen Pramberg has written the code for generating the Sudoku puzzle. This is an
open source which is available online [6]. He states that the generated Sudoku
puzzle should only have one solution (real Sudoku). First, start with empty grid.
Then, the generate function randomly selects cells and places it with random
numbers from 1 to 9. Then, it checks if the number placed is valid and there are no
conflicts. Finally, it makes sure that the generated puzzle is uniqueness, which
means there is only one solution for the generated puzzle [6].  

University  of  Manchester  |  Background  and  Literature  survey   15  


 
SUDOKU  GAME   May  4,  2011  
 
2.3 Algorithms  for  Solving  Sudoku  

There are three algorithms I have mentioned in the introduction. I have used the first
two algorithms in my program (Backtracking and Brute-force algorithm). Instead of
doing the stochastic search algorithm, I have implemented my solving logics. I thought
there is no need to include all of the three algorithms in my project. However, since I
had researched on all of them in advanced, the report explains all of them in more
detailed. However, before start explaining the algorithms, the report details the exact
cover problem in order to start solving puzzles.

2.3.1  Sudoku  and  Exact  Cover  Problem  

Solving Sudoku is considered to be an exact cover problem. It has been explained


that in order to solve a Sudoku puzzle, there are rules need to be followed. In a 9x9
Sudoku puzzle, there are 9 rows, 9 columns and 9 3x3 sub-grids. As a result, there
are four types of constrains;

1) Each Cell can contain exactly one number from 1 to 9.


2) For each Row, there is exact one instance for each digit.
3) For each Colum, there is exact one instance for each digit.
4) For each Sub-Grid, there is exact one instance for each digit.

What makes the Sudoku as an exact cover problem is the fact that each one of the
above constrains involves exactly one of the possibilities. For each possible
assignment of a specific number to a specific cell is a possibility.

Moreover, there are 9x9 cells and for each cell, there are 9 possibilities. So, there are
9 x 9 x 9 = 729 possibilities. However, it is needed to set up an exact cover matrix
for our Sudoku problem. The row of this matrix corresponds to a possible way of
assigning a cell with a number. So, there are 9 possible numbers, 9 rows and 9
columns that is equal to 9 x 9 x 9 = 729 rows which represent the possibility of any
number placed in any cell. On the other hand, the column of this matrix correspond
to the four constrains sets. These constrains sets are correspond to the four types of
constrains which mentioned above. These constrains set are:

1) There are 81 cells (81 constrains sets).


2) There are 9 rows and for each cell in each row, there are 9 possibilities
(9 x 9 = 81 constrains sets).
3) There are 9 columns and for each cell in each column, there are 9
possibilities (9 x 9 = 81 constrains sets).
4) There are 9 sub-grids and for each cell in each sub-grid, there are 9
possibilities (9 x 9 = 81 constrains sets).
As a result, there are 81 + 81 + 81 + 81 = 324 constrains sets. So, the Sudoku matrix
is created. The problem now can be represented as 729x324 matrix.

University  of  Manchester  |  Background  and  Literature  survey   16  


 
SUDOKU  GAME   May  4,  2011  
 
Finally, I used the exact cover problem in order to reduce the complexity of the
algorithms used to reformulate the Sudoku problem. Furthermore, building the exact
cover problem helps to analyse how to solve a Sudoku problem [12].

2.3.2 Backtracking  Algorithm  to  Solve  Sudoku  

This algorithm was the first implementation built in the project. However, the
algorithm uses recursion along with the backtracking. The idea of the recursive call
is the need to call the same function again in order to keep looking for the right
answer. If the function called itself and could not place any number in the cell, the
function goes back to the cell that has the wrong number in it and replaces it with
another valid number and tries again. The idea of this algorithm is quite simple.

Basically, the algorithm calls the function recursively. First, it goes to the first empty
cell, and then tries to fill it with the lowest valid number. After that, it writes the
number into the cell. Then, the next recursion is called in order to move to the next
cell, and tries to fill it with the lowest valid number. It keeps doing this until all cells
are filled.

However, if the current cell does not have any valid number, the cell is cleared and
the algorithm starts backtracking. So, it goes back to find the cell that needs to be
cleared and changed to another valid number that will not lead to any confliction.
Then, it continues recursively to move to the next cell and fill it with a valid number.
If all the empty cells are filled, then all recursive calls are terminated and the puzzle
is solved. If the backtracking goes to the first empty cell and could not find any other
valid number, then the puzzle is not solvable [9].

Wei-Meng Lee has states that, the combination of the recursion and backtracking to
solve a Sudoku puzzle is guaranteed to find a solution, if, however, the puzzle is
solvable [3].

2.3.3 Brute-­‐force  Algorithm  to  Solve  Sudoku  

This algorithm is simpler than backtracking algorithm. It tries all the possible
numbers in order to find a solution. Furthermore, finding a solution for Sudoku
problem is only a matter of time, if the puzzle is solvable. The idea of this algorithm
is to start with the first cell and place number 1. Then, it checks if it valid or not. If
the number placed is valid, it goes to the next cell and place number 1 again. Now,
the second cell has the number 1 same as the previous cell, so that is not valid. Now,
the algorithm increments this number to 2. Then, it checks if it valid or not. In case
all the numbers from 1 to 9 are not valid, it clears the cell and goes back to the
previous cell and increment the value in it. It continues repeating the algorithm until
all empty cells are filled with valid numbers. At that time, the puzzle is solved [7].

University  of  Manchester  |  Background  and  Literature  survey   17  


 
SUDOKU  GAME   May  4,  2011  
 
However, the backtracking and brute-force algorithms are very similar to each other.
They try all the possibilities in order to find a solution. In brute-force algorithm, it
seems longer than backtracking algorithm. That is because in brute-force, it does not
has any clue about what should be in the next cell. As a result, there will be a large
number of iterations.

2.3.4 Stochastic  Search  to  Solve  Sudoku  

It also called optimization methods. This algorithm does not use any logic in order to
solve the problem. It simply throws number randomly in the empty cells and sees
whether it has the valid solution or not.

Given a real Sudoku puzzle, the algorithm first fills all empty cells with random
numbers. Then, it estimates and calculates the number of errors. After that, it
shuffles and mixes up the numbers entered in the empty cells. Then, it recalculates
the numbers of errors. It is doing that until the valid solution is reached without any
number of errors.

However, the stochastic search algorithm is considered slower than previous


algorithms (backtracking and brute-force). The reason is implementing the stochastic
search algorithm is not using any logic to solve the problem. On the other hand,
implementing some logics to fill as possible cells before using the stochastic search
would improve this algorithm [5].

2.4 Logics  to  Solve  Sudoku  Puzzles  

In this section, it describes a human logics rather than computer algorithms. The three
algorithms mentioned above are only to a computer to perform. It is difficult for a
human to apply these algorithms to solve any puzzle, as it takes very long time.

However, coding the human logics in order to solve a Sudoku problem is much faster
and understandable. Instead of making the computer to try all the possibilities, we could
implement some logics that lead to the solution faster. The report presents six of the
logics that I have implemented in my application.

University  of  Manchester  |  Background  and  Literature  survey   18  


 
SUDOKU  GAME   May  4,  2011  
 
2.4.1 Naked  Single  

Given a Sudoku puzzle, there will be given numbers in random cells. Taking the
possibilities for each empty cell is a good approach in order to reduce the number of
trying to find the solution. However, sometimes there are cells that have only one
possibility. In this case, we only need to place that possibility in that cell [12].

2
5

3
8 1 6
9

Figure 2: An Example for Naked Single

The reason why it called naked single is because a cell only has one (single)
possibility. Look at figure 2 to see an example. In the figure 2, the red cell only has
one possibility; it can only be a number 6. All other possibilities are taking place in
the same row, column and sub-grid.

2.4.2 Hidden  Single  

This case happens if there is a cell that has a particular possibility that is not valid in
any cell in the same row, column and sub-grid. So, this possibility should be placed
in the cell. For example, in the figure 3 below, the red cell has the possibilities {3, 4,
and 5}. While the possibility 3 is not valid in any other cells in the same row,
column and sub-gird. As a result, number 3 should be placed in the red cell [12].

However, the first two logics are used in order to reduce the number of possibilities
for other cells. Sometimes in an easy level, it could solve the whole puzzle, but for
higher levels, it may help by leading to find the solution faster.

University  of  Manchester  |  Background  and  Literature  survey   19  


 
SUDOKU  GAME   May  4,  2011  
 

2 4
9 6 7 8
5 1
3
4

Figure 3: An Example for Hidden Single

2.4.3 Locked  Candidates  (Pointing  Pair)  

Sometimes, when you look at a sub-grid, you can determine there is a number has to
be in a particular row or column, even if you do not know which cell should contain
that number. This helps to remove that number from the possibility list in the other
cells in the same row or column, but not inside that sub-grid [12].

For example, looking at figure 4, the red cells must have the number 7 in it. There
are no other cells in the same sub-grid could have this possibility. So, we can
eliminate the number 7 from all possibility lists in the same column but outside the
sub-grid, which is coloured in blue.

2 9
7
4 6

Figure 4: An Example for Locked Candidates 1

Another example in the figure 5 shows that the number 4 must be in either of the red
cells. As a result, we can remove the number 4 from all possibility lists in the same
row but outside the sub-grid, which are coloured in blue [12].

University  of  Manchester  |  Background  and  Literature  survey   20  


 
SUDOKU  GAME   May  4,  2011  
 

6 2

9 3

Figure 5: An Example for Locked Candidates 2

2.4.4 Naked  Pair  

This is the case when there are exactly two possibilities occurring in two different
cells in the same row, column or sub-grid. So, one of the cells must contain one of
the possibilities, and the other cell must contain the other possibility. More precisely,
those two possibilities cannot be in any other cells [12].

For example, the table below shows the first row. Each cell shows the possibility
list.

4,5,6,8 5,6,8 4,5 1,3,6,7,8 4,5 1,4,5,8 1,3,7,8,9 2 3,5,8,9

Looking at the table, there are 2 cells that have the same possibilities {4 and5}. That
means no other cells in this row can contain the number 4 and 5. So, we eliminate
them from the rest.

6,8 6,8 4,5 1,3,6,7,8 4,5 1,8 1,3,7,8,9 2 3,8,9

Now, we can see that there are two cells having the possibility {6 and 8}. We do the
same thing as before by removing them from the other cells.

6,8 6,8 4,5 1,3,7 4,5 1 1,3,7,9 2 3,9

Finally, this example has shown the benefits of the naked pair logic. Reducing the
number of possibilities may lead to the need of guessing later on.

University  of  Manchester  |  Background  and  Literature  survey   21  


 
SUDOKU  GAME   May  4,  2011  
 
2.4.5 Hidden  Triple    

This technique is similar to naked pair, but instead of removing the possibilities from
other cells in the same row, column or sub-grid, possibilities are eliminated from the
cells that hold these possibilities. To make it clearer, if there are three cells, with
three possibilities that occur between them and do not appear in any cells of the
same row, column or sub-grid, then the other possibilities in these three cells can be
eliminated [12].

For instance, the table below shows a sub-grid with all the possibility lists:

2,5 3,4,7,9 1,5,8


1,7,8 2,7 6
2,3,4,7,9 1,2,5,8 3,4,7

We can notice that any of the possibilities {3, 4 and 9} are occurring in only three
cells. Using this logic, we can eliminate the other possibilities occurring in these
three cells. So the sub-grid will be:

2,5 3,4,9 1,5,8


1,7,8 2,7 6
3,4,9 1,2,5,8 3,4

2.4.6 X-­‐Wing  

As shown the figure 7 below, the cells in yellow contains the possibility 9 in row 1
and row 8. Also, there are no other cells contain the possibility 9 in row 1 and row 8
except them. It is clear that the number 9 must occur in row 1 and 8.

However, the possibility 9 cannot be placed in the same column. Also, 9 cannot be
placed in the same row. It is either;

1) In the yellow cells in the top-left and down-right or


2) In the yellow cells in the top-right and down-left.

In addition, we can’t decide which one of the two will be a valid solution.
Furthermore, the yellow cells will only have this possibility in both columns. This
means, we can remove the 9 in the green cells that are in the same column of the
yellow cells [12].

University  of  Manchester  |  Background  and  Literature  survey   22  


 
SUDOKU  GAME   May  4,  2011  
 
Row  1  

Row  2  

Row  3  

Row  4  
Row  5  
Row  6  

Row  7  

Row  8  

Row  9  

Figure 6: X-Wing Example

To end this sub-section, there are lots of logics that lead to solve any Sudoku problem.
All these logics help to make the searching for a solution much faster and easier.
However, sometimes the logics are not enough to find the final solution. Sometimes we
need to perform some kind of guessing after using these logics in order to find a
solution.

2.5 NP-­‐Complete  Problem  

It is helpful to know the complexity of the Sudoku problem before starting the
implementations. Sudoku is considered as an NP-complete problem. Well, there is not a
known polynomial time algorithm, which solves all the instance problems. Moreover,
this suggests that there are some problems that need to perform some kind of search
algorithm. Furthermore, dealing with this kind of computational complexity has a time
and cost problems. The time problem is the amount of time needed for a computer to
find the solution. However, this time depends on the difficulty of the problem. The size
of the problem depends on the given puzzle. In addition, the space or amount of the
memory available during the problem solving is considered as a cost problem [2, 4 and
11].

University  of  Manchester  |  Background  and  Literature  survey   23  


 
SUDOKU  GAME   May  4,  2011  
 
2.6 Platforms  and  software  

When I decided to program a Sudoku game, I planned to write the code in Java using
netbeans program to code the whole project. The program is coded to work in Widows
and Linux machines. For example, there are hidden files to record the data, and these
files are hidden in both software. For implementing the algorithms, I was willing to
code them in Prolog. So, I have learnt some basics in Prolog and tried to implement
simple algorithms. However, I could not find any way to link the Prolog code with
Java code. What I have found is to write some C language in between Prolog and Java,
in order to send and receive the input/output to and from both sides.
In addition, using netbeans to write the code in java has provided me with Jar files. So,
at each time I finish a version of my program I could use some programs such as NSA
with visual C#, JSmooth and Jar2Exe to convert the jar files into one exe program
which can be run in widows.
At the time I have finished my program, I decided to implement my program in order to
work under Android software. So, I have used eclipse to start coding in java. This was
new and gave me a big experience to move from platform I used to work on to an
android platform. The operating system was not easy to start with, but having the
android’s tutorials in their website, helped me a lot.

University  of  Manchester  |  Background  and  Literature  survey   24  


 
SUDOKU  GAME   May  4,  2011  
 

3 Design  
3.1 Code  structure  

The code structure for the project was incrementing and improving with iterations, as I
am using the agile software methodology. However, figure 7 shows overall structure
for the code and GUI.

Game   Generator   Info  Game  

Solver  

    1  
Features   Colour  
Options  
  Cell  limit  

  Conflict  Check  

  Instruction Save/Load  
s  
  Timer   High  Score  &  Avg  

    Hints    

  Statistics  

   

  Create  Solver   Solver  

    2  
Features   Colour  
  Options  
Cell  limit  
  Interface   Conflict  Check  

  Code  
Save/Load  

  Interface  -­‐  Interface  


Code  -­‐  Code  
 
Interface  -­‐  Code  
     

Figure 7: Project Structure

Moreover, the code is built to have a low coupling and high cohesion as much as
possible. The code is divided into packages with clear names. These packages are
connected to represent the whole program. Figure 8 shows the main interactions
between the packages.

University  of  Manchester  |  Design   25  


 
SUDOKU  GAME   May  4,  2011  
 
 

 
Sudoku  
 

  Features   Solver  &  


Generator  
 

  GUI  

 
     

Figure 8: Package interactions

For each package, there are classes inside them. They are described as followed:

1) Sudoku package; it deals with:


a. Cell – having the cell limit of only one number from 1 to 9.
b. Grid – having 81 cells organised as 9x9 grids.
c. Game – the main puzzle game. Grid which filled with random
numbers in random Cells.
d. Create Solver – empty Grid. Users can fill some of the Cells, and the
program shows solution(s).
e. Tests – to make sure it works fine.
2) Features package; explained in the next sub-section.
3) Solver and Generator package; it has;
a. Solver – 2 algorithms (backtracking and brute-force) and different
logics to solve puzzles.
b. Generator – randomly generate a real Sudoku puzzle. This check for
reality and validity for a puzzle by sending it to the Solver. Three
levels provided (easy – medium – hard).
c. Tests – to make sure it works fine.
4) GUI package; it contains;
a. Main window interface.
b. Game interfaces.
c. Create Solver interfaces.

University  of  Manchester  |  Design   26  


 
SUDOKU  GAME   May  4,  2011  
 
3.2 Features  

The Features was needed to make the program more efficient and attractive. In other
words, features would make my program unique. So, the requirements to be collected
in each stage of iteration should be classified in either basic or non-basic features. This
classification gave me a reason to know what is more important to start with.

These are the overall basic features:

a. Cell Limit – cells must only accept one number from 1 to 9.


b. Timer – provide a Game with an incrementing timer when start the
game.
c. Restart – erase the Cell numbers entered by a user.
d. Give up – provide the solution to users.
e. Levels of difficulty – provide three levels of difficulty (easy, medium
and hard).
f. Conflict checks – provide a way to highlight the wrong inputs entered
by users.
g. Score – provide the best and average scores for each level.

The non-basic features are:

a. Hints – provide a Game with 6 hints, which are numbers, placed


randomly in the Cells in order to help a user to find a solution.
b. Save/load – provide a user a way to continue the puzzle for later.
c. Colour options – provide different Cells and background colours.
d. Colour give up – colouring the valid user inputs by green and invalid
inputs by red when give up is performed.
e. Algorithm used – enabling the program to decide which solver to use
to provide the solution faster.
f. Generate in advanced – creating 2 Game puzzles for each level in
advanced and save them for later use.

Of course, both basic and non-basic features were important to include in my


application. However, there is one more feature used in the Create Solver interface:

g. Real or not real Sudoku – if user inputs do have more than one
solution, it warns the user that the puzzle entered is not real Sudoku.

Last feature was designed for the create solver interface in order to let the user know if
he/she wants to create a real puzzle.

University  of  Manchester  |  Design   27  


 
SUDOKU  GAME   May  4,  2011  
 
3.3 GUI  

One of my aims is to produce a user-friendly interface. Therefore, users should find the
interface entertaining and attractive to use. Also, the interface should be easy and
simple to deal with. So, users do not have to be confused to look for any features in the
application.

Clearly, the interface is an essential part of the whole program. It is the way that users
interact with the program and also the way to control the program (i.e. inputs and
outputs). There are three important interfaces that should be considered to have more
time to design:

1) Main welcome interface.


2) Game interface.
3) Create solver interface.

In the welcome window, it designed to make the user interesting in playing the game.
The interface is clear, which has buttons and toolbar contents ordered, as a user would
expect. In addition, there are keyboard shortcuts, which are shown in the toolbar. So,
the user can use only the keyboard in order to use the whole program.

However, in the game interface, the design was not easy. Having the user spending
most or all the time in this window makes the designing a bit difficult to satisfy all or
most of the users. So, I designed a feature that provides users to control the colouring of
the cells and background by using the options. Also, colouring the user inputs when
giving up is executed. Before designing this interface, I sketched four drawings of the
GUI to decide which one of them to use. Of course, in each of the iterations, there were
more features to add. So, I had to improve the design of the interface. Feedbacks from
my family and friends were really helpful to decide the most attractive design among
the four GUIs I have created. Furthermore, the generating and solving puzzles may take
time for the program to perform. So, the program is designed to make the interface
usable all the time by the user. The code behind the GUI is almost all the time working,
in order to generate puzzles in advanced and save them for later. If for each level of the
game, there are two puzzles in advanced are generated, then the code behind is waiting
for the user to use any of the puzzles saved to directly generate new one.

Finally, the create solver window was similar to the game window. However, it does
not need all the features that applied to the game window. So, I designed this interface
to have all cells empty and accept just valid inputs from a user. The conflict
highlighting designed to work all the time.

University  of  Manchester  |  Design   28  


 
SUDOKU  GAME   May  4,  2011  
 

4 Implementation  
Implementing the code for Sudoku was challenging. First thing came to my mind was how
could I implement the algorithms for solving puzzles in efficient way. It is the reason why I
took this project. After designing the code structure, I start the first iteration by implementing
the algorithms (backtracking and brute-force). Then, I thought generating puzzles instead of
using saved ones would improve my application. The sub-sections below are divided to give
more details about these implementations and to give the implementation of the Game GUI
and Create Solver GUI.

4.1 Implementing  Sudoku  Solver    

The first iteration was to implement the two algorithms; backtracking and brute-force
algorithms. These algorithms have been explained in section 2. First thing needed to be
implemented is checking if the number placed in a cell is not conflict with either of
row, column or sub-grid. The code written for the conflict check can be seen in figure
9.

Figure 9: checkConflict function


The function parameters are:

1) The number of cell’s row,


2) The number of cell’s column,
3) The whole puzzle and
4) The number that needs to be tested.

If the number has no violate in the cell, then it returns true. Otherwise, it returns false
and the solver algorithm needs to try another number.

University  of  Manchester  |  Implementation   29  


 
SUDOKU  GAME   May  4,  2011  
 
However, the implementation of the backtracking algorithm is to go through all cells
recursively. Here is the pseudo code for this implementation:

Boolean BackTrackAlgo (int row, int column, int [][] puzzle)

(1) Check if the row == 9


a. It true, then make row = 0
b. If column ++ == 9
i. Return true and solution is found
(2) Check if the cell is not empty
a. Call BackTrackAlgo for the next row.
(3) For loop incrementing number from 1 – 9
a. If checkConflict returns true,
i. then place this number in the cell
ii. and if BackTrackAlgo for the next row (recursive call)
1. return true;
(4) Otherwise, make the cell zero and return false, the solution could not be
found.
However, making the algorithm go through all the cells and see whether it is empty or
not seems not efficient enough. So, I have created a countFreeCells function, which
has a 2-dimensional array to save all the empty cells in the puzzle and to count the
number of them. This code implementing is shown in figure 10.

Figure 10: count the number of empty cells


Now, I can use a while loop that works until all empty cells are full. In addition, having
the possible numbers for each cell in advance would be even better. The code has a 3-
dimensional array, which goes to each cell before implementing the brute-force
algorithm and collects all the possibility for each cell. Figure 11 shows the code
implemented for collecting the possibilities and counts them for each empty cell.

University  of  Manchester  |  Implementation   30  


 
SUDOKU  GAME   May  4,  2011  
 

Figure 11: collect the possibilities for each cell

Now, the solver can directly go to the empty cells. Moreover, the solver only needs to
select the number from the possibility array instead of trying the numbers from 1 to 9.
So, the index in the figure 11 has the total number of possibilities for each cell that is
saved in numberOfPossible array.

Looking at the figure 12, it shows an example of the implementation explained in figure
10. In this example, the first row and column has only one possibility:
• PossibleNumber[1][1][1] = 5
• numberOfPossible[1] = 1

Col• 1 Col 2 Col 3

Row 1 5 3,4,7,9 1,5,8


Row 2 1,7,8 2,7 6
Row 3 3,4 1,2,5,8 3,4,7

Figure 12: Example for collecting possibilities

University  of  Manchester  |  Implementation   31  


 
SUDOKU  GAME   May  4,  2011  
 
However, in row 2 and column 1 has three possibilities:

• PossibleNumber[2][1][1] = 1
• PossibleNumber[2][1][2] = 7
• PossibleNumber[2][1][3] = 8
• numberOfPossible[2] = 3

At the end, the program knows how many empty cells and the possible numbers should
be placed in them.

However, implementing the logics that explained in section 2.4 was a later decision, in
order to insure the main areas are fulfilled in time. Section 2.4 gives an explanation that
I used to implement them in my program. Furthermore, in naked single logic, the
program can first look for any cell that has only one possible number and place it in the
cell. That is implemented by looking at numberOfPossible array.

On the other hand, for the hidden single, the code is implemented to:
(1) Select an empty cell.
(2) Select the first possibility form PossibleNumber array.
(3) Check if it occurs again in the same row, column, and sub-grid.
a. If not then place it in the cell
(4) If it occurs, then select the next possibility from PossibleNumber array and
go to (2) until all the number of possibilities has been checked in this cell.
(5) Next, move to the next empty cell and go to (2) again until it searches in all
the possibilities in each cell.

The logics implementation was performed before backtracking algorithm. Sometimes,


there is no need to use backtracking. However, if the logics could not lead to the
solution, then guessing the number using backtracking is needed.

4.2 Implementing  Sudoku  Generator  

There are two ways to generate Sudoku puzzle. First one is to randomly fill cells with
random numbers from 1 to 9. Then, check if it has only one solution. The other way is
to fill all the cells with a valid Sudoku, and then randomly remove numbers from the
puzzle. After that, it checks if the puzzle still has only one solution.

University  of  Manchester  |  Implementation   32  


 
SUDOKU  GAME   May  4,  2011  
 
However, in my program, I have implemented the first method:

(1) Decide the level of difficulty


a. Easy – loop for 80 times
b. Medium – loop for 50 times
c. Hard – loop for 20 times
(2) Select a random cell (1-81)
(3) Select a random number (1-9)
a. Check if it does not violate with the puzzle
(4) If not, place the number in the cell
(5) Go to number (2) until the number of loop selected in (1) is satisfied.
(6) Send the generated puzzle to the solver.
(7) Solver checks if it only has one solution (real Sudoku).
a. If not, go to number (2) again (generate another one)
(8) Print it to the user.

This generating might take more time sometimes. However, I have implemented the code
to produce 2 more puzzles for each level in advanced. So, the user does not have to wait
for new puzzles.

4.3 Implementing  Sudoku  Game  

This is where the puzzles are presented to users. The sub-sections explain the main
feature implementations.

4.3.1 Conflict  Check  

The user can make the game checks for his input at all the time and highlights the
row, column or sub-grid that will have a repeated number. This implementation is
using the same checkConflict function explained before in section 4.1 and figure 9
shows the code implementation. Moreover, it connected to the interface taking the
user input at the time he is playing. The code is implemented if the “conflict check”
button has been clicked:

(1) Remove all red highlighting in the grid.


(2) If there are repeated number(s) in the row,
a. Highlight the whole row.
(3) If there are repeated number(s) in the column,
a. Highlight the whole column.
(4) If there are repeated number(s) in the sub-grid,
a. Highlight the whole sub-grid.
(5) If no repeated at all go to (1).

University  of  Manchester  |  Implementation   33  


 
SUDOKU  GAME   May  4,  2011  
 
4.3.2 Hints  

In each game, the user is provided with 6 hints to use. The implementation is
explained in the pseudo code below:

(1) Start the game with only 6 hints.


(2) Each time the hint is clicked, reduce the number of hints.
a. If clicked 6 times, then disable the hints.
(3) Select a random cell which is either;
a. An empty or
b. Has a non-valid user input.
(4) Place the number for that cell which is taken from the solution.
(5) Underline the cell to make it noticeable to the user.

4.3.3 Statistics  

This implemented in order to make the program more challengeable for users. For
each game, there is a timer start when the game starts. If the game has ended
successfully, the time counted for solving the puzzle is send to the statistics in order
to:

(1) Increment the number of played game for this level.


(2) See if it is the best time reached for this level or not.
a. If it is, then place it as best time.
(3) Calculate the average time for the game played in this level.

4.4 Implementing  Create  Solver  

In the middle of creating the program, there was an iteration to implement a create
solver. This implementation will take the user inputs and solve it with the solving
methods explained before in section 4.1. The pseudo code implemented is:

(1) Start the window with a 9x9 empty grid.


(2) Accept users inputs in the cells.
a. Conflict check is working to alert the user if repeated input has been
entered. Also, solve is disabled if violation inputs occur.
b. If the inputs do not have a solution, alert the user with a reason.
(3) If solve button is clicked, the program selects which algorithm to implement.
(4) Confirm the user if there are more than one solution.
a. The user can see one of the solutions, or
b. The user can go back and make the puzzle as a real Sudoku (only has one
solution).

The decision of selecting which algorithm to implement is by calculating how many


inputs are given to the program to solve (if it was reasonable to use logics first or not).

University  of  Manchester  |  Implementation   34  


 
SUDOKU  GAME   May  4,  2011  
 
However, after using the logics, there might be the need to do some guessing. The
implementation of finding if a puzzle has more than one solution is to try to fill the rest
of the cells as explained in the steps here:

(1) Start the backtracking algorithm to fill the rest of empty cells with the smallest
valid number.
a. If all cells are filled, then a solution 1 has been found.
b. If could not fill all cells, then there is no solution for the puzzle.
i. Alert the user and exit from the solver.
(2) Restart the backtracking algorithm to fill the rest of the empty cells with the
largest valid number.
a. If all cells are filled, then a solution has been found.
i. If this solution is different form solution 1, then the program
knows there are at least two solutions for this puzzle. Then, alert
the user about it.
ii. If the solution found is the same as solution 1, then there is only
one solution.
b. If could not fill all the cells, then there is only one solution.

4.5 Implementing  Save/Load  

There is an implementation for saving the puzzles. This feature was necessary in order
to make the user satisfier. Each time the application is closed, the puzzles and important
information are saved in hidden files. Moreover, each time the application is started, the
information from the hidden files are moved to the application. The application has two
main windows. One for playing the game (Sudoku Game) and the other one is to create
a game (Create Solver). First, the important information in Sudoku Game, which need
to be saved are:

1) The generated puzzle and its level.


2) The user inputs for this puzzle.
3) The spend time for this puzzle.
4) The number of hints used.

However, the important thing need to be saved in Create Solver is the puzzle created by
the user. While the application also need to save:

1) The background and cells colour,


2) The statistics for each level.

All these information are saved in infoGame file, which is shown before in figure 7. So,
the application needs to interact with infoGame in order to have all the information it
needs.

University  of  Manchester  |  Implementation   35  


 
SUDOKU  GAME   May  4,  2011  
 

5 Results  
This section is divided into two main sub-sections. The first one is an illustration for playing
Sudoku game, while the second sub-section is to explain how to use the game solver.

5.1 Sudoku  Game  

The program has an instruction window to explain how the game is played and shows
the features of the application. Figure 13 shows a screen shot of the game window.
Each of the buttons shown is explained in the sub-sections below.

Figure 13: Sudoku game in medium level

5.1.1 File  toolbar  

The contents of the file are shown in figure 13. Explaining briefly each one of the
content:

• “New” – to start a new game of the same level.


• “Statistics” – enable to access the statistics window while the game is on.
See figure 15.
• “Saved” - users can save the game at any time.

University  of  Manchester  |  Results   36  


 
SUDOKU  GAME   May  4,  2011  
 
• “Play Saved one” – can load the game saved at any time for the current
level.
• “Options” – to access the window which the user can change the cell’s
and background’s colour. See figure 16.
• “Close” – to close the game.

Figure 14: File contents


In addition, the window has shortcuts for each of the content, so users don’t have
to use the mouse in order to perform what inside it. Figure 14 shows the
shortcuts.

Figure 15: Statistics - screenshot

In the statistics window, the user can remove all the statistics to be initialised to
zero again, and the shortcut is Ctrl+R. The user can use only the keyboard to play
and use the whole application without the need of the mouse.

University  of  Manchester  |  Results   37  


 
SUDOKU  GAME   May  4,  2011  
 

Figure 16: Options - screenshot

5.1.2 Save/Load  Puzzles  

In new Game, if it was the first time using the program, there will be no saved
puzzles. However, if a user saved a puzzle in any level, he/she can play it at any
time. Always before leaving the game, a window appears asking if the user wants to
save the puzzle for later, see Figure 17. Moreover, at each time entering a level with
a saved game, the program shows a window asking if you want to continue with the
saved one or not, see figure 18. Furthermore, the time, number of hints used and user
inputs are saved along with the puzzle.

Figure 17: Save Game - screenshot

 
 

Figure 18: continue Saved Game - screenshot

University  of  Manchester  |  Results   38  


 
SUDOKU  GAME   May  4,  2011  
 
5.1.3 Game  Level  and  Pause  Game  

It provides an easy access to the other levels. The current level is highlighting the
button, so a user will not be confused which level is on. In figure 19, a screenshot for
each level to show the highlighting behind the button.

The game can be pause by users. At the time the pause button is clicked, the cells
will be disabled and the timer will stop increasing. This hold until the button is
clicked again.

Figure 19: Game Levels coloured - screenshot

5.1.4 Conflict  Check  and  Hints  

When the conflict check button is clicked, the violation inputs will be highlighted
until the button is clicked again or when the user correct his/her input mistakes. Two
examples are shown in figure 20.

Figure 20: conflict Check example – screenshot


However, as we know that there are 6 hints for each game; an example would be
shown in figure 21, which shows 5 hints.  

University  of  Manchester  |  Results   39  


 
SUDOKU  GAME   May  4,  2011  
 
 

 
 

Figure 21: hints example - screenshot

5.1.5 Restart  and  Give  Up  

The restart button placed in the GUI in order to make it easier to users to restart the
game. All cells that have been filled with user inputs are removed and the time
counter is restarted to zero.

In addition, when a user fills some cells and decided to give up, the program shows
an alert to make sure that the user wants to see the solution. After that, if yes is
clicked, the program compares the solution it has with what the user has filled in
order to colour the correct user inputs by green and the wrong inputs with red.
Finally, the user can see the solution. An example is shown in figure 22.

Figure 22: Give up screenshot

University  of  Manchester  |  Results   40  


 
SUDOKU  GAME   May  4,  2011  
 
5.1.6 Submit  

To submit the solution, the program first will check that there are no empty cells. If
there an empty cell(s), then an alert will appear to the user asking to complete all
cells before submitting. Figure 23 shows the alert. However, if all cells are filled
then it checks if there are no conflict has been entered. If it has the correct solution,
then a congratulation screen will appear as shown in figure 24. Otherwise, the
solution is wrong and an alert screen is shown as in figure 25.

Figure 23: Empty Cells Exist - screenshot

Figure 24: congratulations - screenshot

Figure 25: Failed solution - screenshot

University  of  Manchester  |  Results   41  


 
SUDOKU  GAME   May  4,  2011  
 
5.2 Game  Solver  

This provides a user to create a puzzle and asking the program to solve it. A screen shot
has been taken in figure 26.

Figure 26: Create Sudoku Game – screenshot

The file toolbar shown in figure 27 has the following:

• “New” – to make the grid empty.


• “Save” – to save the puzzle created by a user at any time.
• “Show Saved Created” – to load the saved puzzle.
• “Options” – same as the one in Sudoku Game (colour options).

Figure 27: File toolbar for game solver - screenshot

The window also has shortcuts for each of the contents mentioned above which also
shown in the figure 27. The “conflict check” highlighting is working always when the
user places invalid input(s) in the cell. An example is shown in figure 28.

University  of  Manchester  |  Results   42  


 
SUDOKU  GAME   May  4,  2011  
 

Figure 28: conflict inputs – screenshot


In figure 29, it shows the alert if the input puzzle created by a user has more than one
solution. Otherwise, the user inputs are valid and the program does not complain about
it. The colours of the cells that have been entered by a user will be grey. Then, the
solver solves the puzzle and shows it to the user. All cells will be disabled.  

     

Figure 29: More 1 Solution Alert –screenshot


When a user wants to close the create window, an alert appears to the user asking if the
work done should be saved. Figure 30 shows the screenshot. Moreover, figure 31
indications a screenshot of loading the saved work alert. This alert is shown when the
user starts the Game Solver window.

Figure 30: Save Created Game – screenshot

University  of  Manchester  |  Results   43  


 
SUDOKU  GAME   May  4,  2011  
 
 

Figure 31: Load Create Game - screenshot


 

Figure 32 displays a screenshot of an alert. This alert happens when the solver can’t
find any solution for the user puzzle. So, the program will ask the user to try different
inputs. If the solver finds out the reason why the puzzle is not solvable, then the user
can see that reason and correct it.

Figure 32: Not Solvable Alert - screenshot


 

University  of  Manchester  |     44  


 
SUDOKU  GAME   May  4,  2011  
 

6 Testing    
As mentioned in section 1.5 (tackling the problem), there were tests at the end of each
milestone. These tests done in order to make sure the current process is valid with no errors.
Some of the implementations, I used to make tests before start coding (Test-Driven
Development methodology).

6.1 Test-­‐Driven  Development  (TDD)  

Test-Driven Development is one of techniques of using the agile software


methodology. I was studying the agile software course at the same time I was starting
implementing my code. Figure 33 represents the steps of performing the TDD. TDD
helps and guides me to do the right requirements with more confidence. Furthermore,
the idea of the TDD is to start writing a failed test before implementing the code. This
test is failed because the code implemented is not written yet. The tests have written as
the requirement specifies. As the code has been written in the program, then we can use
the test, which is written before. If the test passes, then the code implemented meets the
test requirements. After that, the code should be refactored to remove any duplication
between the tests and the code implemented.

 
     

Figure 33: TDD Steps


 

University  of  Manchester  |  Testing   45  


 
SUDOKU  GAME   May  4,  2011  
 
6.2 Feedbacks  

When the GUI has been created, non-programmer people can use the program easily.
At the end of the milestone 3 “construct GUI” which mentioned in section 1.5, I used to
show the program to my friends in order to take notes of their feedback.

After each milestone, each new-implemented code was tested as much as possible to
make sure there are no bugs. Of course, doing the tests for your own program would be
obvious to find bugs. However, there might be errors cannot be noticed for the
programmer himself. So, at each stage of my development, I used to convert the jar
files into executable program in windows platform. This makes it easy to send it to my
family and honest friends who gave me valuable feedbacks.

I think the best way to test a game program is by letting users to use it and gives their
feedbacks. This helps me a lot to improve my program and to make it with no major
bugs.

University  of  Manchester  |  Testing   46  


 
SUDOKU  GAME   May  4,  2011  
 

7 Evaluation  
The evaluation part was important. It is the way to insure that the program meets its aim and
how accurate it was. There are questions I have used to evaluate my final program:

1) Performance – how good the application is working along with the time?
2) Result – is the outputs of the application correct?
3) Completion – does the application meet your needs?
4) Mislaid – what is missing from the program?

At each time the iteration is done with testing or coding, the questions above needed to be
answered in order to evaluate the progress. Moreover, these questions have been sent along
with the program to my family and friends to collect their feedbacks.

7.1 Unit  Testing  

There are unit tests to test the performance and results which I needed to improve the
program at each stage. The final program I have accomplished was satisfying all the
questions as the requirements asks.

7.1.1 Solver  and  Generator  


As mentioned before, the program uses two algorithms (backtracking and brute-
force) and logics to solve the puzzles. However, after doing some observations,
using the logics before implementing algorithms appear to solve the puzzle faster.
There are some puzzles, which cannot be solved only by logics. Therefore, the need
of these two algorithms was essential for solving the users’ puzzles.

However, deciding which solver has a good performance does not depend on which
solver technique is solving the puzzle with an interesting way. There is a way to
represent the performance for the solver techniques which is:

• The time – how much time the solver needs in order to solve a puzzle. This
could be measured by asking the program to perform 400 puzzles to solve
and then show the total time. Therefore, I could compare between the solvers
and come up with the fastest solver technique.

In figure 34, it shows the performance of applying backtracking, brute-force


algorithms and logics with backtracking for solving puzzles. The test was to run the
program for each algorithm over 400 puzzles in each level and calculate the average
time. However, it shows that the faster way of implementing the solving is to use the
logics before implementing the search algorithms. Specifically with easy level, the
logics can solve most of the puzzles without the need of using the algorithm.
 

University  of  Manchester  |  Evaluation   47  


 
SUDOKU  GAME   May  4,  2011  
 

350  
easy  
300  
medium  
250   hard  

200  
Time  (s)  

150  

100  

50  

0  
Backtracking   Brute-­‐force   logics  with  Backtracking  

Figure 34: Algorithms Performance for 400 puzzles

7.2 Features  and  GUI  

The features mentioned before in section 3.2 are implemented in the program.
However, there are more features would make the program even better. For example,
the program can show more hints using the logics explained in section 2.4. Therefore,
users can ask for cells that have a naked single solution. Also, users can check if their
solution so far is correct or not by asking the program.

The program allows users to create and save their own puzzles and to make the
program solve the valid ones. However, the program does not insert puzzles that are
created by users into the game system.

There is no score system in the program. There is only statistics, which shows for each
level the best and average time. The program could have a score system that would
make the user more interesting in playing and allow users to keep tracking their
performance.

On the other hand, the GUI was not high level of professional. It does have some
colouring features, but there are no graphical pictures as a background. There are
options provided for the game and create solver window. However, these options could
be improved to have more features rather than just colouring. To have options such as
changing the font and font size would make it easier for users who have bad eyesight.

University  of  Manchester  |  Evaluation   48  


 
SUDOKU  GAME   May  4,  2011  
 

8 Conclusions  
8.1 Achievements  Obtained    

The main objectives explained in section 1.4 before has been achieved successfully. I
have created a Sudoku game application. In order to create the whole application, I
needed to learn about the algorithms for generating and solving Sudoku. There are
many ways of producing the solution of a puzzle. The idea was to integrate the solver
logics with one of the algorithms in order to improve the efficiency time. The
application uses two algorithms and human logics to solve all real Sudoku. Solver
logics are implemented before the backtracking algorithm to reduce searching for a
solution. Furthermore, the application is capable to generate 3 levels of difficulties. So,
users can play the level that is more enjoyable and satisfiable for them. In addition, the
application enables users to create or copy any valid puzzle (i.e. newspapers) and then
have the solution.

The GUI created in the application was designed successfully with more options and
features, so users should find it more attractive than other applications. There are no
ambiguities in using the application. Moreover, I have added the instructions to explain
how the game is played and how to use the whole application. But since users like to
start playing rather than reading the instruction, the application has a clear sequence of
use. Even the shortcuts can be easily figured out.

On the other hand, this application has been transformed to another platform. I have
used eclipse program in order to code the application into android software. The coding
language was written in java and C#. Android is an open platform and easy to find
errors. This is the first phone application I have done. It was challenging, but due to less
time I had in the end of the project, it was really simple. In addition, coding the solver
logics increases my ability to become a better programmer thinker. Overall, the final
application satisfied all the objectives outlined in the introduction.

8.2 Future  Extensions    

The code implemented in the application has been well organised and also includes
comments everywhere. Therefore, I believe that the application can be easily developed
more, especially for the android version. However, there are many approaches that
could be done in order to improve and extend the functionality of the application. For
example, the generator can generate different size of Sudoku grids (3x3, 12x12, …,
etc). Also, solver functionality can be improved to work faster with different grid sizes.
Finally, the generating of levels could be more accurate than using loops.

University  of  Manchester  |  Conclusions   49  


 
SUDOKU  GAME   May  4,  2011  
 

9 References  
[1] Hoghton,  G.  (2005,  May).  Guy's  Online  Sudoku  Solver.  Retrieved  Jan  13,  2011  from  sudoku  
duck:  http://www.sudoku-­‐duck.com/bfs/  

[2] Johnson,  D.,  Garey,  M.,  &  Tarjan,  R.  (1976,  Dec  4).  The  Planar  Hamiltonian  Circuit  Problem  is  
NP-­‐Complete.  Retrieved  Jan  16,  2011  from  princeton:  
http://www.cs.princeton.edu/courses/archive/spr04/cos423/handouts/the%20planar%20ha
miltonian.pdf  

[3] Lee,  W.-­‐M.  (2006).  Programming  Sudoku.  New  York:  Apress.  

[4] Lewis,  R.  (2007).  Metaheuristics  can  Solve  Sudoku  Puzzles.  Retrieved  Jan  16,  2011  from  
Publications  and  Presentations:  http://www.rhydlewis.eu/  

[5] Lweis,  R.  (2007,  Apr).  On  the  Combination  of  Constraint  Programming  and  Stochastic  Search:  
The  Sudoku  Case.  Retrieved  Jan  13,  2011  from  Publications  and  Presentations:  
http://www.rhydlewis.eu/talks/DortmundTalkRhydLewis.pdf  

[6] Pramberg,  J.  (2010,  Aug  3).  Sudoku  Solver  and  Generator.  Retrieved  Jan  13,  2011  from  Code  
Project:  http://www.codeproject.com/KB/game/sudoku.aspx  

[7] Silas,  G.  (2009).  The  algorithm  behind  a  sudoku  solver.  Retrieved  Jan  15,  2011  from  Yahoo!  
Answers:  http://answers.yahoo.com/question/index?qid=20090810061953AAR1mud  

[8] Smallbone,  N.  (2005).  Sudoku  Solver  -­‐  source  code.  Retrieved  Jan  13,  2011  from  Sudoku  
Solver:  http://www.8325.org/sudoku/source.html  

[9] Tellenbach,  P.  (2007,  Mar  18).  Backtracking  to  solve  a  sudoku  puzzle.  Retrieved  Jan  13,  2011  
from  Heimetli:  http://www.heimetli.ch/ffh/simplifiedsudoku.html  

[10] What  is  Sudoku?  (2007,  Jan  9).  Retrieved  Jan  13,  2010  from  Notelay:  
http://www.notelay.com/articles/did_you_know/what_is_sudoku/  

[11] Yato,  T.,  &  Seta,  T.  (2002).  Complexity  and  Completeness  of  Finding  Another  Solution  and  Its  
Application  to  Puzzles.  Retrieved  Jan  16,  2011  from  Takayuki  Yato's  Site:  http://www-­‐
imai.is.s.u-­‐tokyo.ac.jp/~yato/data2/SIGAL87-­‐2.pdf  

[12] Zhang,  Y.  (2010).  The  Application  of  Exact  Cover  to  the  Creating  of  Sudoku  Puzzle.  Retrieved  
Jan  15,  2011  from  Math:  http://www.math.utah.edu/~yzhang/teaching/1030/Sudoku.pdf  

University  of  Manchester  |  References   50  


 

You might also like