Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET
Article

SuDoKu--(Online Game)

Rate me:
Please Sign up or sign in to vote.
3.56/5 (10 votes)
6 Oct 2008CPOL 32.8K   640   21   12
SuDoKu--(Online Game)

Introduction

This article shows how to create a SuDoKu Online Game.

Image 1

After Click on New Puzzle

SuDoKu.JPG

After Click on Check Answer

SuDoKuAnswer.JPG

Using the Code

C#
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";
}
}
}
}

License

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


Written By
Engineer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
gstolarov8-Mar-11 4:43
gstolarov8-Mar-11 4:43 
GeneralGood One ............. Pin
NitinTiwari0217-Mar-09 20:32
NitinTiwari0217-Mar-09 20:32 
GeneralRe: Good One ............. Pin
Sam T Mathew17-Mar-09 20:38
Sam T Mathew17-Mar-09 20:38 
GeneralNice Article Pin
Dipak Rade17-Mar-09 20:28
Dipak Rade17-Mar-09 20:28 
GeneralRe: Nice Article Pin
Sam T Mathew17-Mar-09 20:30
Sam T Mathew17-Mar-09 20:30 
GeneralHi Mathew Very expensive but great... Pin
Ivan Timmel7-Oct-08 8:40
Ivan Timmel7-Oct-08 8:40 
GeneralRe: Hi Mathew Very expensive but great... Pin
Sam T Mathew7-Oct-08 20:21
Sam T Mathew7-Oct-08 20:21 
GeneralRe: Hi Mathew i advance the idea Pin
Ivan Timmel7-Oct-08 22:13
Ivan Timmel7-Oct-08 22:13 
GeneralVery nice article Pin
sRupali7-Oct-08 1:14
sRupali7-Oct-08 1:14 
GeneralRe: Very nice article Pin
Sam T Mathew7-Oct-08 1:17
Sam T Mathew7-Oct-08 1:17 
GeneralVery Interesting Pin
merlin98129-Sep-08 5:48
professionalmerlin98129-Sep-08 5:48 
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 Pin
Sam T Mathew7-Oct-08 0:03
Sam T Mathew7-Oct-08 0:03 

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

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