Professional Documents
Culture Documents
Ahmed Almuhrij PDF
Ahmed Almuhrij PDF
Sudoku
Game
Third Year Project Report
University of Manchester
School of Computer Science
Student
Name:
Ahmed
Abdulkarim
Almuhrij
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.
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.
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
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
1 Introduction
1.1 Sudoku
and
Its
Rules
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.
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:
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:
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.
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.
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:
In both of them, the interface of the program is boring. There are no features to make
the game more enjoyable.
First, introducing two implementations available online for solving a Sudoku puzzle;
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].
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;
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.
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.
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.
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.
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:
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.
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.
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.
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.
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].
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.
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:
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].
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].
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.
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.
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
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.
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.
2 4
9 6 7 8
5 1
3
4
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
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].
6 2
9 3
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.
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.
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.
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.
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:
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.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;
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].
Row 2
Row 3
Row
4
Row
5
Row
6
Row 7
Row 8
Row 9
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.
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].
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.
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.
Solver
1
Features
Colour
Options
Cell
limit
Conflict Check
Instruction Save/Load
s
Timer
High
Score
&
Avg
Hints
Statistics
2
Features
Colour
Options
Cell
limit
Interface
Conflict
Check
Code
Save/Load
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.
Sudoku
GUI
For each package, there are classes inside them. They are described as followed:
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.
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.
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:
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.
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.
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.
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.
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
• 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.
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.
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.
This is where the puzzles are presented to users. The sub-sections explain the main
feature implementations.
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:
In each game, the user is provided with 6 hints to use. The implementation is
explained in the pseudo code below:
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:
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 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.
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:
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:
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.
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.
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.
The contents of the file are shown in figure 13. Explaining briefly each one of the
content:
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.
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.
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.
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.
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.
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.
This provides a user to create a puzzle and asking the program to solve it. A screen shot
has been taken in figure 26.
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.
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.
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).
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.
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.
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.
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.
350
easy
300
medium
250
hard
200
Time
(s)
150
100
50
0
Backtracking
Brute-‐force
logics
with
Backtracking
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.
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.
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.
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
[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