2010-05-29 171 views
0

我正在嘗試做一個else語句來告訴用戶遊戲以draw(tic tac toe game)結束。我知道它在哪裏起作用,如果有一個贏家,它會顯示另一個表單,通過if語句宣佈獲勝者,但我無法弄清楚它的抽籤部分。如果else語句需要幫助

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 


       } 
       //else statement here for draw what code would I put in? 



      } 


        return gameOver; 

     } 



    } 

} 
+0

嘗試重新格式化您的問題,請... – Betamoo 2010-05-29 14:49:13

回答

2

當所有空格填滿並且沒有贏家時,遊戲以平局結束。

您的支票不應該在for循環中檢查每個獲勝線,但應該單獨檢查,之後for循環。此時,請檢查所有控件的「Text」是否非空白,如果是,則繪製一個圖形(如果有人贏得了之前的代碼,則會發現該圖形)。

+0

你可以給我一個例子使用我的代碼 – 2010-05-29 17:39:47