Click here to Skip to main content
Licence CPOL
First Posted 25 Sep 2008
Views 14,901
Downloads 248
Bookmarked 21 times

SuDoKu--(Online Game)

By | 6 Oct 2008 | Article
SuDoKu--(Online Game)

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

License

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

About the Author

Sam T Mathew

Engineer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembergstolarov4:43 8 Mar '11  
GeneralGood One ............. PinmemberNitinTiwari0220:32 17 Mar '09  
GeneralRe: Good One ............. PinmemberSam T Mathew20:38 17 Mar '09  
GeneralNice Article PinmemberDipak Rade20:28 17 Mar '09  
GeneralRe: Nice Article PinmemberSam T Mathew20:30 17 Mar '09  
GeneralHi Mathew Very expensive but great... PinmemberIvan Timmel8:40 7 Oct '08  
GeneralRe: Hi Mathew Very expensive but great... PinmemberSam T Mathew20:21 7 Oct '08  
GeneralRe: Hi Mathew i advance the idea PinmemberIvan Timmel22:13 7 Oct '08  
GeneralVery nice article PinmembersRupali1:14 7 Oct '08  
GeneralRe: Very nice article PinmemberSam T Mathew1:17 7 Oct '08  
GeneralVery Interesting Pinmembermerlin9815: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 PinmemberSam T Mathew0:03 7 Oct '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Sam T Mathew
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid