I am a new programmer. I found this shuffle buttons game. I understand most of what is going on here but I don't get what is the role of "flag" and what does "i" do.. once it is i=1 but then it says "while(i<=8)".. and then what role does it have here: "a[i]". I would be grateful if someone could explain.
namespace Shuffle_Numere
{
public partial class Form1 : Form
{
int num;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button1.Text;
button1.Text = "";
}
if (button4.Text == "")
{
button4.Text = button1.Text;
button1.Text = "";
}
ChekWin();
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Text == "")
{
button1.Text = button2.Text;
button2.Text = "";
}
if (button3.Text == "")
{
button3.Text = button2.Text;
button2.Text = "";
}
if (button5.Text == "")
{
button5.Text = button2.Text;
button2.Text = "";
}
ChekWin();
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button3.Text;
button3.Text = "";
}
if (button6.Text == "")
{
button6.Text = button3.Text;
button3.Text = "";
}
ChekWin();
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == "")
{
button1.Text = button4.Text;
button4.Text = "";
}
if (button5.Text == "")
{
button5.Text = button4.Text;
button4.Text = "";
}
if (button7.Text == "")
{
button7.Text = button4.Text;
button4.Text = "";
}
ChekWin();
}
private void button5_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button5.Text;
button5.Text = "";
}
if (button4.Text == "")
{
button4.Text = button5.Text;
button5.Text = "";
}
if (button6.Text == "")
{
button6.Text = button5.Text;
button5.Text = "";
}
if (button8.Text == "")
{
button8.Text = button5.Text;
button5.Text = "";
}
ChekWin();
}
private void button6_Click(object sender, EventArgs e)
{
if (button3.Text == "")
{
button3.Text = button6.Text;
button6.Text = "";
}
if (button5.Text == "")
{
button5.Text = button6.Text;
button6.Text = "";
}
if (button9.Text == "")
{
button9.Text = button6.Text;
button6.Text = "";
}
ChekWin();
timer1.Enabled = true;
timer1.Start();
}
private void button7_Click(object sender, EventArgs e)
{
if (button4.Text == "")
{
button4.Text = button7.Text;
button7.Text = "";
}
if (button8.Text == "")
{
button8.Text = button7.Text;
button7.Text = "";
}
ChekWin();
}
private void button8_Click(object sender, EventArgs e)
{
if (button5.Text == "")
{
button5.Text = button8.Text;
button8.Text = "";
}
if (button7.Text == "")
{
button7.Text = button8.Text;
button8.Text = "";
}
if (button9.Text == "")
{
button9.Text = button8.Text;
button8.Text = "";
}
ChekWin();
timer1.Enabled = true;
timer1.Start();
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == "")
{
button6.Text = button9.Text;
button9.Text = "";
}
if (button8.Text == "")
{
button8.Text = button9.Text;
button9.Text = "";
}
ChekWin();
}
public void ChekWin()
{
num = num + 1;
label2.Text = num.ToString();
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 == "")
{
if (MessageBox.Show("Congratulations! You won in "+num+" moves asnd "+timp+" seconds.", "Message text", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
label2.Text = "0";
timp = 0;
timer1.Stop();
label4.Text = "0";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Shuffle()
{
int i, j, Rn;
int[] a = new int[9];
Boolean flag = false;
i = 1;
do
{
Random rnd = new Random();
Rn = ((rnd.Next(0, 8)) + 1);
for (j = 1; j <= i; j++)
{
if (a[j] == Rn)
{
flag = true;
break;
}
}
if (flag == true)
{
flag = false;
}
else
{
a[i] = Rn;
i = i+1;
}
}
while (i <= 8);
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 = "";
num = 0;
label2.Text = "0";
timer1.Stop();
label4.Text = "0";
}
private void button10_Click(object sender, EventArgs e)
{
Shuffle();
}
int timp = 0;
private void timer1_Tick(object sender, EventArgs e)
{
timp++;
label4.Text = timp.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
Shuffle();
}
}
}
Aucun commentaire:
Enregistrer un commentaire