2010-05-31 20 views
0

這是一個tic tac toe遊戲。我需要幫助做一個檢查語句,看看所有控件的文本是否非空白,如果是,你有一個平局(如果有人贏得了先前的代碼會發現)。你能用我的代碼給我一個很好的例子嗎?需要幫助做一個檢查聲明,以確保控件不是空白的

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
namespace MyGame 
{ 
    public class Result1 
    { 


     static private int[,] Winners = new int[,] 
        { 
         // main gameplay Ex: if x is on 0,1,2 x is the winner 
         {0,1,2}, 
         {3,4,5}, 
         {6,7,8}, 
         {0,3,6}, 
         {1,4,7}, 
         {2,5,8}, 
         {0,4,8}, 
         {2,4,6}, 
        }; 


     static public bool CheckWinner(Button[] myControls) 
     { 
      //bolean statement to check for the winner 
      bool gameOver = false; 
      for (int i = 0; i < 8; i++) 
      { 
       int a = Winners[i, 0]; 
       int b = Winners[i, 1]; 
       int c = Winners[i, 2]; 

       Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c]; 
       if (b1.Text == "" || b2.Text == "" || b3.Text == "") 
        continue; 

       if (b1.Text == b2.Text && b2.Text == b3.Text) 
       { 

        b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral; 
        b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
        gameOver = true; 
        xWinnerForm xWinnerForm = new xWinnerForm(); 
        xWinnerForm.ShowDialog(); //only works with show not showDialog method gets overloaded (b1.Text + " is the Winner"); to get around this I added and image showing the last player 


       } 


      } 


     return gameOver; 
     } 





    } 
} 

回答

0

注意你已經擁有的代碼,將檢查的空白處,並在那裏你會發現一個空的空間,所有的情況下沒有平局。如果你遍歷所有可能的空間並且沒有找到贏家或者空白空間,那麼它顯然是一個平局。

static public bool CheckWinner(Button[] myControls) 
{ 
    //bolean statement to check for the winner 
    bool gameOver = false; 
    bool foundEmpty = false; 
    for (int i = 0; i < 8; i++) 
    { 
     int a = Winners[i, 0]; 
     int b = Winners[i, 1]; 
     int c = Winners[i, 2]; 

     Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c]; 
     if (b1.Text == "" || b2.Text == "" || b3.Text == "") 
     { 
      foundEmpty = true; 
      continue; 
     } 

     if (b1.Text == b2.Text && b2.Text == b3.Text) 
     { 

      b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral; 
      b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
      gameOver = true; 
      xWinnerForm xWinnerForm = new xWinnerForm(); 
      xWinnerForm.ShowDialog(); //only works with show not showDialog method gets overloaded (b1.Text + " is the Winner"); to get around this I added and image showing the last player 


     } 


    } 
    if (!gameOver && !foundEmpty) 
    { 
     // must be a draw 
     gameOver = true; 
    } 

    return gameOver; 
} 
+0

非常感謝,非常棒! – 2010-05-31 14:15:49

0

它看起來像所有的控件都在myControls數組中。您可以使用LINQ而方便地查詢他們:

if (myControls.All(c => c.Text != "") { 
    // Draw 
} 
+0

我試過這個,但是當只有3個盒子被選中時它會觸發事件我該如何解決這個問題。我是編程新手,所以我不太瞭解它, – 2010-05-31 14:06:47