随机播放按钮游戏c#
Shuffle buttons game c#
我是一名新程序员。我找到了这个洗牌按钮游戏。我明白这里发生的大部分事情,但是
- 我不明白 "flag" 和
的作用是什么
i
是做什么的.. 一旦它是 i=1
但是然后它说 while(i<=8)
.. 和
- 那么它在这里有什么作用:
a[i]
。
如果有人能解释一下,我将不胜感激。
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();
}
}
}
what does "i"
i
在这里用作计数器和索引变量。
but then it says "while(i<=8)"..
作为计数器,它用于 while-loop or the do-while-loop
变量将在循环内递增(值将增加),只要条件(i<=8)
保持不变,循环就会继续true
what role does it have here: "a[i]".
这里用作索引变量。这意味着它用于表示数组 a
中的位置。这是使用 [ ]
operator
如您所见,i
在此行递增:
i = i+1;
基本上是用来遍历数组,访问数组的不同位置。
编辑:抱歉监督了标志部分
I don't get what is the role of "flag"
标志基本上是信号。它们用于为某些操作设置条件。通常信号是 fired/set 到 true
当另一个条件满足时某个事件发生。在你的情况下,当 j
位置 a
中的数字等于随机开奖号码。
if (a[j] == Rn)
{
flag = true;
break;
}
这结束了 for 循环并阻止了 else 块的执行:
else
{
a[i] = Rn;
i = i+1;
}
我是一名新程序员。我找到了这个洗牌按钮游戏。我明白这里发生的大部分事情,但是
- 我不明白 "flag" 和 的作用是什么
i
是做什么的.. 一旦它是i=1
但是然后它说while(i<=8)
.. 和- 那么它在这里有什么作用:
a[i]
。
如果有人能解释一下,我将不胜感激。
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();
}
}
}
what does "i"
i
在这里用作计数器和索引变量。
but then it says "while(i<=8)"..
作为计数器,它用于 while-loop or the do-while-loop
变量将在循环内递增(值将增加),只要条件(i<=8)
保持不变,循环就会继续true
what role does it have here: "a[i]".
这里用作索引变量。这意味着它用于表示数组 a
中的位置。这是使用 [ ]
operator
如您所见,i
在此行递增:
i = i+1;
基本上是用来遍历数组,访问数组的不同位置。
编辑:抱歉监督了标志部分
I don't get what is the role of "flag"
标志基本上是信号。它们用于为某些操作设置条件。通常信号是 fired/set 到 true
当另一个条件满足时某个事件发生。在你的情况下,当 j
位置 a
中的数字等于随机开奖号码。
if (a[j] == Rn)
{
flag = true;
break;
}
这结束了 for 循环并阻止了 else 块的执行:
else
{
a[i] = Rn;
i = i+1;
}