Slimey Games: Unity Sudoku Solver Tutorial Part 2

Slimey Games: Unity Sudoku Solver Tutorial Part 2

Tutorials about Game Development using Unity, MonoDevelop and C#-Scripting

RTS-Project parts 4 to 6 - Buildings and units

RTS-Project parts 7 and 8 - Cursor and movement

RTS-Project parts 9 and 10 - Resources and unit production

RTS-Project part 11 - Set rally point and sell buildings

RTS-Project part 12 - Build a harvester and collect resources

RTS-Project part 13 - Building construction and a new worker unit

RTS-Project part 17- Saving and Loading Players

Unity Sudoku Solver Tutorial - Introduction

Unity Sudoku Solver Tutorial - Part 1 - Drawing the GUI

Unity Sudoku Solver Tutorial - Part 2 - Setting Sudoku numbers

Unity Sudoku Solver Tutorial - Part 3 - Load an example Sudoku

Hello again! This time we will implement a way for our users to fill the Sudoku with numbers. This is the first way to present an unsolved Sudoku to the solving algorithms we are going to implement later on.

The first thing we are going to change is the OnGUI() method in SudokuGUI.cs. After setting the GUI.skin variable, enterif(Data.ShowSudoku){}

and enter the rest of the existing code into the curly brackets. Then, add the following to the end of the OnGUI() method.for (int i=0;i<3;i++){

if(Data.ShowNumberMenu[i,j,k,l,]==true){

The variables ShowSudoku and ShowNumberMenu dont exist in Data.cs. Add the two variables to the file Data.cs.public static int[,,,] ColorCode{get;set;}

public static bool ShowSudoku{get;set;}

We then need to initialize these variables. Use the Start() method of SudokuGUI.cs to do this.Data.ShowNumberMenu=new bool[3,3,3,3];

We further need the ChooseNumber()-method inside of SudokuGUI.cs that lets us write numbers into our Sudoku. I implemeted the method like this:private void ChooseNumber(int i, int j, int k, int l){

leftPos=Screen.width/2-subGridWidth/2+x*(buttonWidth+padding);

topPos=Screen.height/2-subGridWidth/2+y*(buttonHeight+padding);

if (GUI.Button(new Rect(leftPos,topPos,buttonHeight,buttonWidth),number.ToString())){

Data.Sudoku[i,j,k,l]=number;

Data.ShowNumberMenu[i,j,k,l]=false;

Data.ColorCode[i,j,k,l]=2;

if (GUI.Button(new Rect(leftPos,topPos,buttonHeight,buttonWidth),number.ToString())){

Data.Sudoku[i,j,k,l]=number;

Data.ShowNumberMenu[i,j,k,l]=false;

Data.ColorCode[i,j,k,l]=0;

This method gets the integers i,j,k,l that

define one specific entry in our Sudoku. We then draw a menu consisting

of numbers from 0 to 9. When one of the numbers is clicked, we set the

Sudoku entry i,j,k,l to this number. If the clicked number is not 0, we

want to show this by changing the background color of the button in the

This is why we first need another variable inside Data.cs:public static int[,,,] ColorCode{get;set;}

which we need to initialize inside the Start() method in SudokuGUI.cs:Data.ColorCode=new int[3,3,3,3];

We use the color code, to give a specific button a specific texture. All button textures are going to be stored stored inside a public variable. Addpublic Texture2D[] buttonTextures;

to the top of SudokuGUI.cs.

To opn and close the menu for the choice of each Sudoku entry we need to accomodate the method DrawSudokuButton():private void DrawSudokuButton(int i, int j, int k, int l){

float leftPos=Screen.width/2-sudokuWidth/2+j*(3*buttonWidth+2*padding+bigPadding)

float topPos=3*bigPadding+padding+buttonHeight+i*(3*buttonHeight+2*padding+bigPadding)

sudokuSkin.button.normal.background=buttonTextures[Data.ColorCode[i,j,k,l]];

if (GUI.Button(new Rect(leftPos,topPos,buttonWidth,buttonHeight),Data.Sudoku[i,j,k,l].ToString())){

Data.ShowNumberMenu[i,j,k,l]=true;

One last thing is left to change: The array buttonTextures is empty at the moment, this will produce an error when we choose a value for one of our Sudoku cells.

Select the main camera object in Unity. Click on the little arrow next to the Button Textures entry and set the size of the array to 3. Add the button.png to Element 0 and the button_hover.png to Element 1. Add this

image first to the GUI->Images folder and then drag it onto Element 2. The result should now look like this:

Click to show/hide Unity web player

Unity Sudoku Solver Tutorial - Introduction