Create Sliding Puzzle Game (Shuffle Game) C# Windows Form Application
Hello Everyone
Today I am going to Show how you can create Sliding Puzzle Game (Shuffle Game).
There are two forms in this application. First Form is for displaying name of application it contains two buttons and one image. Here is the representation of display form.
First button is play button that will redirect to another form and second button is for closing application.
This is our second form which is main form.
There are three methods in this form
- CheckButton();
public void CheckButton(Button bttn1, Button bttn2)
{
if (bttn2.Text == "")
{
bttn2.Text = bttn1.Text;
bttn1.Text = "";
}
}
- CheckSolve();
public void checksolve()
{
num = num + 1;
label1.Text = num + " Clicks";
if (button1.Text == "1" && button2.Text == "2" && button3.Text == "3" &&
button4.Text == "4" && button5.Text == "5" && button6.Text == "6" &&
button7.Text == "7" && button8.Text == "8" && button9.Text == "9" &&
button10.Text == "10" && button11.Text == "11" && button12.Text == "12" &&
button13.Text == "13" && button14.Text == "14" && button15.Text == "15" &&
button16.Text == "")
{
MessageBox.Show("Congratz You have did in " + num + " Clicks" );
}
}
- Shuffle();
public void shuffle()
{
int i, j, Rn;
int[] a = new int [16];
Boolean flage = false;
i = 1;
do
{
Random rnd = new Random();
Rn = Convert.ToInt32((rnd.Next(0,15) + 1));
for (j = 1; j <= i; j++)
{
if (a[j] == Rn)
{
flage = true;
break;
}
}
if (flage == true)
{
flage = false;
}
else
{
a[i] = Rn;
i = i + 1;
}
}
while(i <= 15);
button1.Text = Convert.ToString(a[1]);
button2.Text = Convert.ToString(a[2]);
button3.Text = Convert.ToString(a[3]);
button4.Text = Convert.ToString(a[4]);
button5.Text = Convert.ToString(a[5]);
button6.Text = Convert.ToString(a[6]);
button7.Text = Convert.ToString(a[7]);
button8.Text = Convert.ToString(a[8]);
button9.Text = Convert.ToString(a[9]);
button10.Text = Convert.ToString(a[10]);
button11.Text = Convert.ToString(a[11]);
button12.Text = Convert.ToString(a[12]);
button13.Text = Convert.ToString(a[13]);
button14.Text = Convert.ToString(a[14]);
button15.Text = Convert.ToString(a[15]);
button16.Text = "";
num = 0;
}
{
int i, j, Rn;
int[] a = new int [16];
Boolean flage = false;
i = 1;
do
{
Random rnd = new Random();
Rn = Convert.ToInt32((rnd.Next(0,15) + 1));
for (j = 1; j <= i; j++)
{
if (a[j] == Rn)
{
flage = true;
break;
}
}
if (flage == true)
{
flage = false;
}
else
{
a[i] = Rn;
i = i + 1;
}
}
while(i <= 15);
button1.Text = Convert.ToString(a[1]);
button2.Text = Convert.ToString(a[2]);
button3.Text = Convert.ToString(a[3]);
button4.Text = Convert.ToString(a[4]);
button5.Text = Convert.ToString(a[5]);
button6.Text = Convert.ToString(a[6]);
button7.Text = Convert.ToString(a[7]);
button8.Text = Convert.ToString(a[8]);
button9.Text = Convert.ToString(a[9]);
button10.Text = Convert.ToString(a[10]);
button11.Text = Convert.ToString(a[11]);
button12.Text = Convert.ToString(a[12]);
button13.Text = Convert.ToString(a[13]);
button14.Text = Convert.ToString(a[14]);
button15.Text = Convert.ToString(a[15]);
button16.Text = "";
num = 0;
}
Here is the short implementation of this game. If you have any query you can comment.
Here is source code.
Create Sliding Puzzle Game (Shuffle Game) C# Windows Form Application
Reviewed by Unknown
on
1:18 AM
Rating:
No comments: