Click here to Skip to main content
Licence CPOL
First Posted 25 Sep 2008
Views 14,907
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  
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 PinmembersRupali1:14 7 Oct '08  
GeneralRe: Very nice article PinmemberSam T Mathew1:17 7 Oct '08  
GeneralVery Interesting Pinmembermerlin9815:48 29 Sep '08  
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
Web02 | 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