井字棋游戏平局提示重复弹出8次问题求助
井字棋应用平局提示重复弹出问题求助
我开发了一款tic-tac-toe(井字棋)应用,整体功能正常,但存在一个问题:当游戏出现平局时,平局提示框会重复弹出8次。我参考的教程未提及该问题,无法自行修复,现附上相关C#代码,希望得到帮助:
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(); } }
问题原因
checkForWinner方法会遍历8种获胜组合,每次循环如果当前组合不是获胜情况,就会调用CheckDraw,平局时会触发8次调用,导致提示框弹8次。CheckDraw方法的判断逻辑错误,在遍历格子的循环内部每次都检查count == 9,会重复触发提示。
修复代码
1. 修正CheckDraw方法
public void CheckDraw() { int count = 0; for (int i = 0; i < table.Length; i++) { if (table[i] != null) count++; } // 遍历完所有格子后再判断是否平局 if (count == 9) { MessageBox.Show("平局!", "没有获胜者 :(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); // 可选:禁用所有按钮防止继续操作 DisableAllButtons(); } }
2. 修正checkForWinner方法
public void checkForWinner() { bool hasWinner = false; 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获胜!", "我们有赢家了!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); hasWinner = true; DisableAllButtons(); break; // 找到胜者后立即退出循环 } else if(combination.Equals("XXX")) { changeColor(one); changeColor(two); changeColor(three); MessageBox.Show("X获胜!", "我们有赢家了!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); hasWinner = true; DisableAllButtons(); break; // 找到胜者后立即退出循环 } } // 所有组合检查完毕,没有胜者才判断平局 if (!hasWinner) { CheckDraw(); } }
3. 添加辅助方法优化体验
// 禁用所有按钮,防止游戏结束后继续点击 private void DisableAllButtons() { button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; button5.Enabled = false; button6.Enabled = false; button7.Enabled = false; button8.Enabled = false; button9.Enabled = false; } // 在reset方法中恢复按钮可用状态 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; // 恢复按钮可用 button1.Enabled = true; button2.Enabled = true; button3.Enabled = true; button4.Enabled = true; button5.Enabled = true; button6.Enabled = true; button7.Enabled = true; button8.Enabled = true; button9.Enabled = true; countTurn = 0; table = new string[9]; }
内容的提问来源于stack exchange,提问作者Rares Petrescu
相关产品推荐
相关产品推荐

