好吧,我有一个抽搐脚趾应用程序。我做得很好,但有一个小问题。当它是抽签时,它会给我显示8次这样的信息,而我无法单独纠正这一点。其他一切都很好。在本教程中,我发现它没有解决这个特定的问题,那么我做错了什么呢?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] table = new string[9];
int countTurn = 0;
public String returnSymbol(int turn)
{
if (turn % 2 == 0)
{
return "O";
}
else
return "X";
}
public System.Drawing.Color determineColor(string symbol)
{
if(symbol.Equals("X"))
{
return System.Drawing.Color.LawnGreen;
}
else
{
return System.Drawing.Color.Aqua;
}
}
public void CheckDraw()
{
int count = 0;
for (int i = 0; i < table.Length; i++)
{
if (table[i] != null)
count++;
if (count == 9)
{
MessageBox.Show("That's a draw!", "We have no winner :(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
public void checkForWinner()
{
for(int i=0;i<8; i++)
{
string combination = "";
int one=0, two=0, three = 0;
switch(i)
{
case 0:
combination = table[0] + table[4] + table[8];
one = 0;
two = 4;
three = 8;
break;
case 1:
combination = table[2] + table[4] + table[6];
one = 2;
two = 4;
three = 6;
break;
case 2:
combination = table[0] + table[1] + table[2];
one = 0;
two = 1;
three = 2;
break;
case 3:
combination = table[3] + table[4] + table[5];
one = 3;
two = 4;
three = 5;
break;
case 4:
combination = table[6] + table[7] + table[8];
one = 6;
two = 7;
three = 8;
break;
case 5:
combination = table[0] + table[3] + table[6];
one = 0;
two = 3;
three = 6;
break;
case 6:
combination = table[1] + table[4] + table[7];
one = 1;
two = 4;
three = 7;
break;
case 7:
combination = table[2] + table[5] + table[8];
one = 2;
two = 5;
three = 8;
break;
}
if(combination.Equals("OOO"))
{
changeColor(one);
changeColor(two);
changeColor(three);
MessageBox.Show("O has won the game!", "We have a winner!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
if(combination.Equals("XXX"))
{
changeColor(one);
changeColor(two);
changeColor(three);
MessageBox.Show("X has won the game!", "We have a winner!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
CheckDraw();
}
}
}
public void reset()
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
button1.BackColor=System.Drawing.Color.White;
button2.BackColor=System.Drawing.Color.White;
button3.BackColor=System.Drawing.Color.White;
button4.BackColor=System.Drawing.Color.White;
button5.BackColor=System.Drawing.Color.White;
button6.BackColor=System.Drawing.Color.White;
button7.BackColor=System.Drawing.Color.White;
button8.BackColor=System.Drawing.Color.White;
button9.BackColor=System.Drawing.Color.White;
countTurn = 0;
table = new string[9];
}
public void changeColor(int number)
{
switch(number){
case 0:
button1.BackColor = System.Drawing.Color.OrangeRed;
break;
case 1:
button2.BackColor = System.Drawing.Color.OrangeRed;
break;
case 2:
button3.BackColor = System.Drawing.Color.OrangeRed;
break;
case 3:
button4.BackColor = System.Drawing.Color.OrangeRed;
break;
case 4:
button5.BackColor= System.Drawing.Color.OrangeRed;
break;
case 5:
button6.BackColor = System.Drawing.Color.OrangeRed;
break;
case 6:
button7.BackColor = System.Drawing.Color.OrangeRed;
break;
case 7:
button8.BackColor = System.Drawing.Color.OrangeRed;
break;
case 8:
button9.BackColor = System.Drawing.Color.OrangeRed;
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
countTurn++;
table[0]=returnSymbol(countTurn);
button1.BackColor = determineColor(table[0]);
button1.Text = table[0];
checkForWinner();
}
private void button2_Click(object sender, EventArgs e)
{
countTurn++;
table[1] = returnSymbol(countTurn);
button2.BackColor = determineColor(table[1]);
button2.Text = table[1];
checkForWinner();
}
private void button3_Click(object sender, EventArgs e)
{
countTurn++;
table[2] = returnSymbol(countTurn);
button3.BackColor = determineColor(table[2]);
button3.Text = table[2];
checkForWinner();
}
private void button4_Click(object sender, EventArgs e)
{
countTurn++;
table[3] = returnSymbol(countTurn);
button4.BackColor = determineColor(table[3]);
button4.Text = table[3];
checkForWinner();
}
private void button5_Click(object sender, EventArgs e)
{
countTurn++;
table[4] = returnSymbol(countTurn);
button5.BackColor = determineColor(table[4]);
button5.Text = table[4];
checkForWinner();
}
private void button6_Click(object sender, EventArgs e)
{
countTurn++;
table[5] = returnSymbol(countTurn);
button6.BackColor = determineColor(table[5]);
button6.Text = table[5];
checkForWinner();
}
private void button7_Click(object sender, EventArgs e)
{
countTurn++;
table[6] = returnSymbol(countTurn);
button7.BackColor = determineColor(table[6]);
button7.Text = table[6];
checkForWinner();
}
private void button8_Click(object sender, EventArgs e)
{
countTurn++;
table[7] = returnSymbol(countTurn);
button8.BackColor = determineColor(table[7]);
button8.Text = table[7];
checkForWinner();
}
private void button9_Click(object sender, EventArgs e)
{
countTurn++;
table[8] = returnSymbol(countTurn);
button9.BackColor = determineColor(table[8]);
button9.Text = table[8];
checkForWinner();
}
private void TryAgainBtn_Click(object sender, EventArgs e)
{
reset();
}
}
}发布于 2022-08-04 10:17:04
问题是,您的CheckDraw方法是在外层for(int i=0;i<8; i++)循环中调用的。更改CheckDraw以返回如下所示的bool:
public bool CheckDraw()
{
int count = 0;
for (int i = 0; i < table.Length; i++)
{
if (table[i] != null)
count++;
if (count == 9)
{
MessageBox.Show("That's a draw!", "We have no winner :(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return true;
}
}
return false;
}然后在CheckForWinner()中改变
else
{
CheckDraw();
}至
else
{
if (CheckDraw())
{
break;
}
}附带说明:我认为您的实现允许更改已经播放的字段的状态。所以你可以通过把O改为X来赢得比赛.
发布于 2022-08-04 10:46:08
实际上,您可以将CheckDraw函数简化为以下内容
public void CheckDraw()
{
for (int i = 0; i < table.Length; i++)
{
//on the first `null` entry it can't be a draw anymore, so return
if (table[i] == null)
return;
}
//if the loop found a value in all cells it's a draw ...
MessageBox.Show("That's a draw!", "We have no winner :(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}或者更简单,如果您使用LINQ
public void CheckDraw()
{
if (table.All(x => x != null)) {
MessageBox.Show("That's a draw!", "We have no winner :(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}但这个功能不是你真正的问题。问题在checkForWinner中。一旦你找到胜利者你就该退出。只有当没有一个获胜的情况导致一个胜利者,也检查一下是否平局。
public void checkForWinner()
{
for(int i=0;i<8; i++)
{
string combination = "";
int one=0, two=0, three = 0;
switch(i)
{
...
}
if(combination.Equals("OOO"))
{
...
MessageBox.Show("O has won the game!", "We have a winner!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; //return from checkforWinner as we already have a winner
}
else if(combination.Equals("XXX"))
{
...
MessageBox.Show("X has won the game!", "We have a winner!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; //return from checkforWinner as we already have a winner
}
}
//if we arrived here, it could still be a draw ...
checkDraw();
}如果您事先创建了一个获胜情况列表,然后迭代并检查这些情况,您还可以大大简化您的checkForWinner。因为检查总是相同的:检查表中的三个条目是否相等。如果是的话,各自的玩家都赢了.
public void checkForWinner()
{
var winningList = new List<(int a, int b, int c)> {
(0, 1, 2),
(3, 4, 5),
//...
};
foreach (var t in winningList) {
if (table[t.a] != null && table[t.a] == table[t.b] && table[t.a] == table[t.c]) {
changeColor(t.a);
changeColor(t.b);
changeColor(t.c);
MessageBox.Show($"{table[t.a]} has won the game!", ...);
return; //return from checkforWinner as we already have a winner
}
}
//if we arrived here, it could still be a draw ...
CheckDraw();
}正如另一条注释中所述:大多数按钮单击处理程序仅在表查找的索引中有所不同。您可以很容易地将一个Tag附加到每个按钮(甚至可以根据按钮的名称确定它),这样您就可以对所有按钮使用相同的单击处理程序,而不必重复相同的代码9次.
https://stackoverflow.com/questions/73233998
复制相似问题