Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This article shows how to create a SuDoKu Online Game.

After Click on New Puzzle

SuDoKu.JPG

After Click on Check Answer

SuDoKuAnswer.JPG

Using the Code

 
namespace puzzle
{
public partial class _Default : System.Web.UI.Page
{

int[][] jaggedArray = new int[9][];
int countclick;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label2.Text = "";
Label3.Visible = false;
}
else
{
Panel1.Visible = false;
countclick = 0;
Label3.Visible = false;
Session["count"] = countclick;
Button1_Click(this.Button1, e);
}
for (int check1 = 1; check1 <= 81; check1 = check1 + 1)
{
TextBox objTextBox1 = (TextBox)form1.FindControl("TextBox" + check1);
objTextBox1.Attributes.Add("onkeypress", "return isNumberKey(event);");
}
}
private void arrays1()
{
int j,k;
int[] myarray = new int[9];
myarray=(int[])Session["h"];
jaggedArray[0] = new int[9];
jaggedArray[1] = new int[9];
jaggedArray[2] = new int[9];
jaggedArray[3] = new int[9];
jaggedArray[4] = new int[9];
jaggedArray[5] = new int[9];
jaggedArray[6] = new int[9];
jaggedArray[7] = new int[9]; 
jaggedArray[8] = new int[9];
int[][] jaggedArray12 = new int[9][];
jaggedArray12[0] = new int[9];
jaggedArray12[1] = new int[9];
jaggedArray12[2] = new int[9];
jaggedArray12[3] = new int[9];
jaggedArray12[4] = new int[9];
jaggedArray12[5] = new int[9];
jaggedArray12[6] = new int[9];
jaggedArray12[7] = new int[9];
jaggedArray12[8] = new int[9];
for (j = 1; j <= 9; j++)
{
for (k = 1; k <= 9; k++)
{
jaggedArray12[j - 1][k - 1] = myarray[k - 1];
}
}
for (int ab = 1; ab <= 8; ab++)
{
for (int cd = 0; cd <= 8; cd++)
{
if (ab < 3)
{
if ((cd + 3) <= 8)
jaggedArray12[ab][cd] = jaggedArray12[ab - 1][cd + 3];
else
jaggedArray12[ab][cd] = jaggedArray12[ab - 1][cd - 6];
}
else
{
if((cd+1)<=8)
jaggedArray12[ab][cd] = jaggedArray12[ab - 3][cd + 1];
else
jaggedArray12[ab][cd] = jaggedArray12[ab - 3][0];
}
}
}
for (int i = 0; i <= 8; i++)
{
for (j = 0; j <= 8; j++)
{
jaggedArray[i][j] = jaggedArray12[i][j];
}
}
int second = Convert.ToInt32(DateTime.Now.Second.ToString());
if (second != 0)
{
if ((second %4) == 0)
{
for (int i = 0; i <= 8; i++)
{
jaggedArray12[i][0] = jaggedArray[i][1];
jaggedArray12[i][1] = jaggedArray[i][0];
jaggedArray12[i][4] = jaggedArray[i][5];
jaggedArray12[i][5] = jaggedArray[i][4];
jaggedArray12[i][7] = jaggedArray[i][8];
jaggedArray12[i][8] = jaggedArray[i][7];
}
}
if ((second % 4) == 1)
{
for (int i = 0; i <= 8; i++)
{
jaggedArray12[i][1] = jaggedArray[i][2];
jaggedArray12[i][2] = jaggedArray[i][1];
jaggedArray12[i][3] = jaggedArray[i][5];
jaggedArray12[i][5] = jaggedArray[i][3];
jaggedArray12[i][6] = jaggedArray[i][8];
jaggedArray12[i][8] = jaggedArray[i][6];
}
}
if ((second % 4) == 2)
{
for (int i = 0; i <= 8; i++)
{
jaggedArray12[i][0] = jaggedArray[i][2];
jaggedArray12[i][2] = jaggedArray[i][0];
jaggedArray12[i][4] = jaggedArray[i][5];
jaggedArray12[i][5] = jaggedArray[i][4];
jaggedArray12[i][6] = jaggedArray[i][7];
jaggedArray12[i][7] = jaggedArray[i][6];
}
}
if ((second % 4) == 3)
{
for (int i = 0; i <= 8; i++)
{
jaggedArray12[i][0] = jaggedArray[i][2];
jaggedArray12[i][2] = jaggedArray[i][0];
jaggedArray12[i][3] = jaggedArray[i][4];
jaggedArray12[i][4] = jaggedArray[i][3];
jaggedArray12[i][6] = jaggedArray[i][8];
jaggedArray12[i][8] = jaggedArray[i][6];
}
}
}
Session["Solution"] = jaggedArray12;
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
for (int check2 = 1; check2 <= 81; check2 = check2 + 1)
{
TextBox objTextBox1 = (TextBox)form1.FindControl("TextBox" + check2);
objTextBox1.Enabled = true;
}
int k1 = Convert.ToInt32(Session["Count"]);
k1++;
Session["Count"] = k1; 
int z;
int[] myarray3 = new int[9];
int[] myarray4 = new int[9];
for (z = 0; z <= 8; z++)
{
Random rnd = new Random();
Byte[] b = new Byte[9];
rnd.NextBytes(b);
if (Convert.ToInt32(b[z]) <= 9 && Convert.ToInt32(b[z]) != 0)
{
myarray3[z] = Convert.ToInt32(b[z]);
}
else if (Convert.ToInt32(b[z]/10) <= 9 && Convert.ToInt32(b[z]) != 0)
{
if(Convert.ToInt32(b[z]/10)>0)
myarray3[z] = Convert.ToInt32(b[z]/10);
}
else if (Convert.ToInt32(b[z] / 100) <= 9 && Convert.ToInt32(b[z]) != 0)
{
if (Convert.ToInt32(b[z] / 100) > 0)
myarray3[z] = Convert.ToInt32(b[z] / 100);
}
int k=myarray3[z];
int op =0;
foreach (int k10 in myarray3)
{
if(k==k10)
{
op =op + 1;
}
}
if (op > 1)
{ 
while(k == myarray3[z])
{
int sa = 0;
int k9;
rnd.NextBytes(b);
if (Convert.ToInt32(b[z]) <= 9 && Convert.ToInt32(b[z]) != 0)
{
myarray3[z] = Convert.ToInt32(b[z]);
}
else if (Convert.ToInt32(b[z] / 10) <= 9 && Convert.ToInt32(b[z]) != 0)
{
if (Convert.ToInt32(b[z] / 10) > 0)
myarray3[z] = Convert.ToInt32(b[z] / 10);
}
else if (Convert.ToInt32(b[z] / 100) <= 9 && Convert.ToInt32(b[z]) != 0)
{
if (Convert.ToInt32(b[z] / 100) > 0)
myarray3[z] = Convert.ToInt32(b[z] / 100);
}
k9 = myarray3[z];
foreach (int k10 in myarray3)
{
if (k9 == k10)
{
sa = sa + 1;
}
}
if (sa > 1)
{
k = myarray3[z];
}
}
}
}
int[] myarray1 = new int[9];
myarray1 = myarray3;
Session["h"] = myarray1;
this.arrays1();
this.sam1();
}
private void sam1()
{
int[][] jaggedArray2 = new int[9][];
jaggedArray2 = (int[][])Session["Solution"];
int y = 0;
int x = 0;
for(int z=1;z<=81;z++)
{
TextBox txtBox=(TextBox)this.FindControl("TextBox" +z);
txtBox.Text = jaggedArray2[y][x].ToString();
if (z % 9 == 0)
{
y = y + 1;
x = 0;
}
else if(z % 9 != 0)
{
x = x + 1;
}
}
int i = jaggedArray[0][0];
int i2 = jaggedArray[0][8];
if (i >3)
{
for (int mj = 1; mj <= 30; mj = mj + 1)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
if (objTextBox.Text == i.ToString())
{
objTextBox.Text = "";
}
}
for (int mj = 50; mj <= 81; mj = mj + 1)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
if (objTextBox.Text == (i-1).ToString())
{
objTextBox.Text = "";
}
}
for (int mj = i2; mj <= 81; mj = mj + 2)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
objTextBox.Text = "";
}
}
else
{
if (i == 1)
{
TextBox1.Text = "";
}
for (int mj = 1; mj <= 30; mj = mj + 1)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
if (objTextBox.Text == (i+1).ToString())
{
objTextBox.Text = "";
}
}
for (int mj = 50; mj <= 81; mj = mj + 1)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
if (objTextBox.Text == (i + 3).ToString())
{
objTextBox.Text = "";
}
}
for (int mj = i; mj <= 81; mj = mj + 2)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
objTextBox.Text = "";
}
}
for (int mj = 1; mj <= 81; mj = mj + 1)
{
TextBox objTextBox = (TextBox)form1.FindControl("TextBox" + mj);
if (objTextBox.Text != "")
{
objTextBox.Enabled = false;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Label3.Visible = true;
int[][] jaggedArray2 = new int[9][];
if (Session["Solution"] != null)
{
jaggedArray2 = (int[][])Session["Solution"];
}
else
{
Label2.Text = "Time out";
}
int d= 0;
int f = 0;
for (int z = 82; z <= 162; z++)
{
TextBox txtBox = (TextBox)this.FindControl("TextBox" + z);
txtBox.Text = jaggedArray2[d][f].ToString();
if (z % 9 == 0)
{
d = d + 1;
f = 0;
}
else if (z % 9 != 0)
{
f = f + 1;
}
}
int[] array6 = new int[81];
int[] array5 = new int[81];
for (int check2 = 1; check2 <= 81; check2 = check2 + 1)
{
TextBox objTextBox1 = (TextBox)form1.FindControl("TextBox" + check2);
if (objTextBox1.Text != "")
{
array5[check2-1] = Convert.ToInt32(objTextBox1.Text);
}
else
{
array5[check2-1] = 0;
}
}
int ch = 0;
for (int check3 = 82; check3 <= 162; check3 = check3 + 1)
{
TextBox objTextBox1 = (TextBox)form1.FindControl("TextBox" + check3);
if (objTextBox1.Text != "")
{
array6[ch] = Convert.ToInt32(objTextBox1.Text);
}
else
{
array6[ch] = 0;
}
ch = ch + 1;
}
int chk1=0;
for (int chk = 1; chk <= 81; chk = chk + 1)
{
if (array5[chk-1] != array6[chk-1])
{
chk1 = chk1 + 1;
}
}
if (chk1 > 1)
{
Label2.Text = "Your Answer is wrong...Please check the above answer";
}
else
{
Label2.Text = "Your Answer is correct";
}
}
}
}
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGood One .............
NitinTiwari02
21:32 17 Mar '09  
Thumbs Up Good Keep the spirit up........... Big Grin

Nitin

GeneralRe: Good One .............
Sam T Mathew
21:38 17 Mar '09  
Big Grin Thank u very much..Nitin...
GeneralNice Article
Dipak Rade
21:28 17 Mar '09  
Nice artile Sam.
But too heavy.
You can still improve.
GeneralRe: Nice Article
Sam T Mathew
21:30 17 Mar '09  
Thank u for comments...
GeneralHi Mathew Very expensive but great...
Ivan Timmel
9:40 7 Oct '08  
Hi Mathew Try this:

i do this to do more interactive the game.
Table[,] is the game. With Find() method you can obtain an array of numbers for put on the board. That can help the user for resolve the game.
You can use that to implement an array of buttons with numbers 0-9 and enable or disable the buttons for help the player.
		public int[] Find(int columna, int fila)
{
ArrayList arr = new ArrayList();
LinealFind(arr, columna, fila);
CuadraticFind(arr, columna, fila);
int[] r = new int[9];
for (int i = 0; i < 9; i++)
{
r[i] = i + 1;
}
for (int i = 0; i < arr.Count; i++)
{
r[(int) arr[i] - 1] = 0;
}
return r;
}

private static void LinealFind(ArrayList a, int column, int rows)
{
for (int i = 0; i < 9; i++)
{
if (table[i, column] != 0 && i != rows)
{
a.Add(table[i, column]);

}
if (table[rows, i] != 0 && i != column)
{
a.Add(table[rows, i]);

}
}
}
private static void CuadraticFind(ArrayList a, int column, int rows)
{
int pointx = column/3;
int pointy = rows/3;
Point p = new Point(rows, column);
for (int i = pointx*3; i < pointx*3 + 3; i++)
{
for (int k = pointy*3; k < pointy*3 + 3; k++)
{
Point p2 = new Point(k, i);
if (table[k, i] != 0 && p != p2)
{
a.Add(table[k, i]);
}
}
}
}
public int[,] Table {get turn table; }
set { table = value; }
}
Good Appetite Mathew Big Grin ...
GeneralRe: Hi Mathew Very expensive but great...
Sam T Mathew
21:21 7 Oct '08  
Thank u very much.
i will try to implement your code in this application.
GeneralRe: Hi Mathew i advance the idea
Ivan Timmel
23:13 7 Oct '08  
this struct can help
public struct SudokuCell
{
private int value;
private int x;
private int y;
public SudokuCell(int x, int y)
{
if (((x < 0) || (x >= 9)) || ((y < 0) || (y >= 9)))
{
throw new ArgumentException("the values of the state of cell(0-8)");
}
this.x = x;
this.y = y;
this.value = 0;
}

public SudokuCell(int x, int y, int value) : this(x, y)
{
if ((value < 1) || (value > 9))
{
throw new ArgumentException("the values of the cells (1-9)");
}
this.value = value;
}

public int X
{
get
{
return this.x;
}
}
public int Y
{
get
{
return this.y;
}
}
public int Value
{
get
{
return this.value;
}
}
}
and one class to work with the struct
public class SudokuGameState
{
// Fields
private SudokuCell[] originalProblem;
private SudokuCell[] playerCells;

// Methods
public SudokuGameState(SudokuCell[] originalProblem, SudokuCell[] playerCells)
{
if (originalProblem == null)
{
throw new ArgumentNullException("originalProblem");
}
this.originalProblem = originalProblem;
this.playerCells = playerCells;
}

// Properties
public SudokuCell[] OriginalProblem
{
get
{
return this.originalProblem;
}
}

public SudokuCell[] PlayerCells
{
get
{
return this.playerCells;
}
}
}
Finally i implement this to save and load the game:
public class SudokuIOHelper
{
// Methods
public static SudokuGameState LoadFromFile(string filePath)
{
SudokuGameState state;
using (StreamReader reader = new StreamReader(filePath))
{
try {
int num = int.Parse(reader.ReadLine());
SudokuCell[] originalProblem = new SudokuCell[num];
for (int i = 0; i < num; i++)
{
string[] strArray = reader.ReadLine().Split(new char[] { ',' });
originalProblem[i] = new SudokuCell(int.Parse(strArray[0]), int.Parse(strArray[1]), int.Parse(strArray[2]));
}
num = int.Parse(reader.ReadLine());
SudokuCell[] playerCells = new SudokuCell[num];
for (int j = 0; j < num; j++)
{
string[] strArray2 = reader.ReadLine().Split(new char[] { ',' });
playerCells[j] = new SudokuCell(int.Parse(strArray2[0]), int.Parse(strArray2[1]), int.Parse(strArray2[2]));
}
state = new SudokuGameState(originalProblem, playerCells);
}
catch (Exception exception)
{
throw new Exception("Ha ocurido lgun error leyendo el archivo. Verifique que tega el formato correcto", exception);
}
}
return state;
}

public static void SaveProblem(string filePath, int[,] initialProblem)
{
int num = 0;
for (int i = 0; i < initialProblem.GetLength(0); i++)
{
for (int j = 0; j < initialProblem.GetLength(1); j++)
{
if (initialProblem[i, j] != 0)
{
num++;
}
}
}
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(num);
for (int k = 0; k < initialProblem.GetLength(0); k++)
{
for (int m = 0; m < initialProblem.GetLength(1); m++)
{
if (initialProblem[k, m] != 0)
{
writer.WriteLine("{0},{1},{2}", k, m, initialProblem[k, m]);
}
}
}
writer.WriteLine(0);
}
}

public static void SaveToFile(string filePath, SudokuGameState gameState)
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(gameState.OriginalProblem.Length);
foreach (SudokuCell cell in gameState.OriginalProblem)
{
writer.WriteLine("{0},{1},{2}", cell.X, cell.Y, cell.Value);
}
if (gameState != null)
{
writer.WriteLine(gameState.PlayerCells.Length);
foreach (SudokuCell cell2 in gameState.PlayerCells)
{
writer.WriteLine("{0},{1},{2}", cell2.X, cell2.Y, cell2.Value);
}
}
else {
writer.WriteLine(0);
}
}
}
}

I think that is all maybe very sun i finish the idea and do a sudoku game.
but before i try to implement the generator of sudoku with minimal cost...
So, Mathew Good appetite.
GeneralVery nice article
sRupali
2:14 7 Oct '08  
Hi
I like ur article & idea also.
Just add comments in coding.
GeneralRe: Very nice article
Sam T Mathew
2:17 7 Oct '08  
Thank u very much
GeneralVery Interesting
merlin981
6:48 29 Sep '08  
Very interesting article. Perhaps you could expand it by commenting your code (always an excellent idea), and possibly discussing in the article why you chose to do things the way you did.

Four from me until more comments/article content is added.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LINQ Exchange - Learn about LINQ and Lambda Expressions Mentally Jumbled - Geek Blog
Joke of the Day and Random Jokes - ReallyFunnyQuickJokes.com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GeneralRe: Very Interesting
Sam T Mathew
1:03 7 Oct '08  
Hi Merlin,Thank u very much for ur message.


Last Updated 7 Oct 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010