2016-12-04 65 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