Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi ,

I'm having ASP .NET course and in beginner level , it's too interesting and so powerful .
while my seeking for more information I found your website and it's so useful and helped me to understand unclear subjects .

I'm having a challenge to solve a Home Work , to build Fifteens game in ASP .Net using C# and Web Service , while the Web Service must handle the random fill of table , choose which buttons to be replaced and to visible or invisible and to check end of game .

I have build the Fifteens game in Default.aspx.cs which was my first home work and it's working great here it's :
  using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    private const int sizeBoard = 15;
    private const int sizeRow = 4;
    private int[] arrayIndex = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
<pre>
protected void Page_LoadComplete(object sender, EventArgs e)
{
    if (checkFinalGame())
        showMessage();
    return;
}
 
protected void Page_Load(object sender, EventArgs e)
{
 
    createButton();
    createTable();
 
    if (!Page.IsPostBack)
    {
        TableBtn.BorderStyle = BorderStyle.Inset;
        TableBtn.BorderWidth = Unit.Pixel(3);
        initTable();
    }
 
}
 
private void createButton()
{
    for (int i = 0; i &lt; sizeRow; i++)
    {
        for (int j = 0; j &lt; sizeRow; j++)
        {
 
            Button dynamicButton = new Button();
            dynamicButton.ID = i.ToString() + "," + j.ToString();
            dynamicButton.Click += new System.EventHandler(dynamicButton_Click);
 
            if ((i == sizeRow - 1) && (j == sizeRow - 1))
            {
                dynamicButton.Visible = false;
            }
 
            Controls.Add(dynamicButton);
        }
    }
}
 
private void createTable()
{
    int rows = sizeRow;
    int columns = sizeRow;
 
    for (int i = 0; i &lt; rows; i++)
    {
        TableRow newRow = new TableRow();
        TableBtn.Controls.Add(newRow);
        for (int j = 0; j &lt; columns; j++)
        {
            TableCell newCell = new TableCell();
            newRow.Controls.Add(newCell);
            if (FindControl(i.ToString() + "," + j.ToString()) != null)
            {
                newCell.Controls.Add(FindControl(i.ToString() + "," + j.ToString()));
            }
 
        }
 
    }
}
 
private void initTable()
{
    int rows = sizeRow;
    int columns = sizeRow;
    Random random = new Random();
 
    for (int i = 0; i &lt; rows; i++)
    {
        for (int j = 0; j &lt; columns; j++)
        {
            Button btn = ((Button)(TableBtn.Rows[i].Cells[j].FindControl(i.ToString() + "," + j.ToString())));
            if (!((i == sizeRow - 1) && (j == sizeRow - 1)))
            {
                Boolean flagExist = false;
                int num = 0;
 
                while (!flagExist)
                {
                    num = random.Next(15) + 1;
                    if (arrayIndex[num - 1] == 0)
                    {
                        arrayIndex[num - 1] = i * sizeRow + j + 1;
                        btn.BackColor = System.Drawing.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255));
                        btn.Text = num.ToString();
                        flagExist = true;
                    }
                }
            }
            btn.Width = 50;
            btn.Height = 50;
        }
    }
}
 
private TableCell getCell(String prmID)
{
    int i = int.Parse(prmID.Substring(0, 1));
    int j = int.Parse(prmID.Substring(2, 1));
    try
    {
        return (TableCell)this.TableBtn.Rows[i].Cells[j];
    }
    catch
    {
        return null;
    }
 
}
 
private Button getButton(String prmID)
{
    int i = int.Parse(prmID.Substring(0, 1));
    int j = int.Parse(prmID.Substring(2, 1));
    try
    {
        return (Button)this.TableBtn.Rows[i].Cells[j].FindControl(prmID);
    }
    catch
    {
        return null;
    }
 
}
 
private void dynamicButton_Click(object sender, System.EventArgs e)
{
 
    int i = int.Parse(((Button)sender).ID.Substring(0, 1));
    int j = int.Parse(((Button)sender).ID.Substring(2, 1));
 
    String ID;
    ID = (i).ToString() + "," + (j + 1).ToString();
 
    if ((getCell(ID) != null) && (getCell(ID).Controls[0].Visible == false))
    {
        changeButton(((Button)sender).ID, ID);
    }
 
    ID = (i).ToString() + "," + (j - 1).ToString();
    if ((j &gt; 0) && (getCell(ID) != null) && (getCell(ID).Controls[0].Visible == false))
    {
        changeButton(((Button)sender).ID, ID);
    }
 
    ID = (i + 1).ToString() + "," + (j).ToString();
    if ((getCell(ID) != null) && (getCell(ID).Controls[0].Visible == false))
    {
        changeButton(((Button)sender).ID, ID);
    }
 
    ID = (i - 1).ToString() + "," + (j).ToString();
    if ((i&gt;0) && (getCell(ID) != null) && (getCell(ID).Controls[0].Visible == false))
    {
        changeButton(((Button)sender).ID, ID);
    }
 
}
 
private void showMessage()
{
 
    ClientScript.RegisterStartupScript(typeof(Page), "confirm", "&lt;script&gt;var conf=confirm('End Game! Play Again');if (conf==true){ window.location = 'Default.aspx'}else{window.close();}&lt;/script&gt;");
}
 
private Boolean checkFinalGame()
{
    int index = 1;
    for (int i = 0; i &lt; 1; i++)
    {
        for (int j = 0; j &lt; 2; j++)
        {
            Button btn = ((Button)(TableBtn.Rows[i].Cells[j].FindControl(i.ToString() + "," + j.ToString())));
            if (btn.Text != index.ToString())
                return false;
            index++;
        }
    }
    return true;
}
 
private void changeButton(String PrevID, String NextID)
{
    getButton(NextID).Text = getButton(PrevID).Text;
    getButton(NextID).Visible = true;
    getButton(NextID).BackColor = getButton(PrevID).BackColor;
    getButton(PrevID).Visible = false;
}

}

I did learned about Web Service , and I did practice on it but I couldn't implement the required as above ,
Can you please help me ?

Thanks
Kasem
Posted 25-Feb-13 17:15pm
Comments
Sergey Alexandrovich Kryukov at 25-Feb-13 23:01pm
   
Probably not, because this is not a question, just a code dump. This is a Questions & Answers forum, not a dating site for students and advisers. :-)
All the help you can get should be confined on this page. If you need something else, this is a wrong site.
—SA

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

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 335
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100