2016-12-04 24 views
0

关于程序中的错误:获取(适用于井字C#程序)

我写了一个C#代码制作井字。它是在Windows窗体应用程序(Visual Studio)中制作的。 在玩此游戏时,当X或O获胜时,方法=>checkForwinner()被称为可以进行水平,垂直和对角检查以确定胜利者。变量there_is_a_winner设置为true,并显示消息“获胜者被显示”。否则它会检查绘图。

错误:

当我遵守这个代码,它示出了0错误/警告/ messages.But的那inspite,这个代码是行不通的。它无法确定胜利者。弹出框,显示谁赢了/ draw..never执行,除此之外,此代码工作正常。我希望有人能帮忙。

在此先感谢。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    bool turn = true;//(To check turn) True means X's turn, False=Y turn 
    int turn_count = 0; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)/*About Section*/ 
    { 
     MessageBox.Show("This Program is of Tic Tac Toe. It was created by Me for his C# project.","Tic Tac Toe -About"); 
    } 

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)/*Exit Section*/ 
    { 
     Application.Exit(); 
    } 

    private void button_click(object sender, EventArgs e) 
    { 
     Button b = (Button)sender; 
     if(turn) 
      b.Text="x"; 
     else 
     b.Text="o"; 
     turn=!turn; 
     b.Enabled = false;//disable the button, to prevent double entering. 
     turn_count++; 
    } 
    private void checkForwinner() 
    { 
     bool there_is_a_winner= false; 

     //horizontal check 

     if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled)) 
      there_is_a_winner = true;//if above conditions are true, then bool variable=true. 
     else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled)) 
      there_is_a_winner = true; 

     else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled)) 
      there_is_a_winner = true; 

     //Vertical Check 
     else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled)) 
      there_is_a_winner = true; 

     else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled)) 
      there_is_a_winner = true; 

     else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled)) 
      there_is_a_winner = true; 

     //Diagonal Check 
     else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled)) 
      there_is_a_winner = true; 

     else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled)) 
      there_is_a_winner = true; 

     if (there_is_a_winner) 
     { 
      dissableButtons();// If there is a winner call for buttons to be disbaled. 

      String winner = ""; 
      if (turn) 
       winner = "0"; 
      else 
       winner = "x"; 
      MessageBox.Show(winner + "Wins!", "Congratulations!"); 
     } 
     else 
     { 
      if (turn_count == 9) 
       MessageBox.Show("Match Draw", "Result"); 
     } 


    } 
     private void dissableButtons() 
     { 
      try 
      { 
       foreach (Control c in Controls) 
       { 
        Button b = (Button)c; 
        b.Enabled = false;//If there is a winner, disable all the buttons on the form 
       } 

      } 
      catch { } 
     } 
    // New Game//Need to Reset Everything 
     private void toolStripMenuItem2_Click(object sender, EventArgs e) 
     { 
      turn = true; 
      turn_count = 0; 
      try 
      { 
       foreach (Control c in Controls) 
       { 
        Button b = (Button)c; 
        b.Enabled = true; 
        b.Text = "";//Initially we want blank Text 

       } 
      } 
      catch { } 
     } 
    } 
} 
+0

使用调试器,并通过您的代码步。另外“它不工作”太广泛 - 请解释您提供的输入和您期望的输出。 – Default

回答

2

问题是函数checkForwinner()永远不会在程序中的任何地方被调用。

我期望button_click函数将X或O放在适当的单元格中,所以当单击该按钮时,还需要检查是否有胜者。我建议你调用checkForwinner()作为button_click函数的最后一行,以便每次点击时都会执行检查。另外,作为样式注释,请将其重命名为checkForWinner,并使用大写字母W.还应该缩进行b.Text =“o”;像这样:

if(turn) 
     b.Text="x"; 
    else 
     b.Text="o"; 

话虽如此,我更喜欢有甚至单行花括号,所以我更喜欢这样的:

if(turn) 
    { 
     b.Text="x"; 
    } 
    else 
    { 
     b.Text="o"; 
    } 

即使是占用了更多的线条,它如果在“if”子句或“else”子句中添加另一行,并忘记添加那些重要的大括号,可以避免将来出现问题。这是一个很好的习惯 - 到处都是大括号。

+0

是的,checkForwinner()永远不会在程序中的任何地方被调用......这就是问题所在。谢谢。 – YoMama

1

当你问问题时你必须更具体。在这种情况下,你有更多的工作,因为我在这段代码中看到了几个重大的错误。我只想指出几个:

  1. 你不应该这样做:

    try 
    { 
    ... 
    } 
    catch {} 
    

如果你这样做,你“吃”之外,你永远不知道发生了什么。只有极少数情况下需要使用catch。不要使用捕获,直到你知道你在做什么。

取而代之的是可以显示在应用程序级的任何异常:

在计划

。CS,在方法主要补充:

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.ThreadException += Application_ThreadException; 
     AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 
     Application.Run(new Form()); 
    } 

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
      MessageBox.Show(((Exception)e.ExceptionObject).Message);  
    } 

    private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
    { 
      MessageBox.Show(e.Exception.Message);  
    } 
}  
  • 在你的方法button_click您使用if .. else ...但检查如果你不需要添加{ ... }

    if (...) { 
        code here 
    } 
    else { 
        code here 
    } 
    
  • 使用知名度限定符专用私有字段:

    private bool turn = true;//(To check turn) True means X's turn, False=Y turn  
    private int turn_count = 0; 
    
  • 代替Application.Exit();使用方法Form.Close()this.Close();

  • 如果使用if .. else if ..块,总是使用与例外最终else块。它会告诉你,你错过了什么:

    if (...) 
    { 
        ... 
    } 
    else if (...) 
    { 
        ... 
    } 
    else 
    { 
        throw new NotImplementedException("Not Implemented Yet"); 
    } 
    
  • +0

    谢谢...寻求答案。 – YoMama