2010-05-21 210 views
1

我在想什麼我不能通過這種簡單的tic tac腳趾遊戲來弄清楚它。無法找到類型或命名空間名稱'Button'(您是否缺少using指令或程序集引用?)

另外我怎樣才能合併一個水平滾動條列表框來選擇播放器?玩家1 =(x)或玩家2 =(O)

在此先感謝!

主要形式 - xGameForm

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 MyGame 
{ 
    public partial class xGameForm : Form 
    { 

     public Button xGameButton9; 
     public Button xGameButton8; 
     public Button xGameButton7; 
     public Button xGameButton6; 
     public Button xGameButton5; 
     public Button xGameButton4; 
     public Button xGameButton3; 
     public Button xGameButton2; 
     public Button xGameButton1; 



     public Button[] _buttonArray; 
     public bool isX; 
     public bool isGameOver; 

     Button tempButton = (Button)sender; 

     public xGameForm() 

     { 
      InitializeComponent(); 
     } 

     public void xGameForm_Load(object sender, EventArgs e) 
     { 
      _buttonArray = new Button[9] { xGameButton1, xGameButton2, xGameButton3, xGameButton4, xGameButton5, xGameButton6, xGameButton7, xGameButton8, xGameButton9 }; 

      for (int i = 0; i < 9; i++) 

      this._buttonArray[i].Click += new System.EventHandler(this.ClickHandler); 

      InitTicTacToe(); 

     } 

     public void InitTicTacToe() 
     { 
      for (int i = 0; i < 9; i++) 
      { 
       _buttonArray[i].Text = ""; 
       _buttonArray[i].ForeColor = Color.Black; 
       _buttonArray[i].BackColor = Color.LightGray; 
       _buttonArray[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
      } 
      this.isX = true; 
      this.isGameOver = false; 
     } 

     public void ClickHandler(object sender, System.EventArgs e) 
     { 
      Button tempButton = (Button)sender; 
      if(this.isGameOver) 
      { 
       MessageBox.Show("press reset to start a new game!","Game End",MessageBoxButtons.OK); 
       return;   
      } 
      if(tempButton.Text != "") 
      { 
       return; 
      } 
      if(isX)  
       tempButton.Text = "X"; 
      else 
       tempButton.Text = "O"; 
      isX = !isX; 
      this.isGameOver = Result1.CheckWinner(_buttonArray); 
     } 

     public void xGameResetButton_Click(object sender, EventArgs e) 
     { 
      InitTicTacToe();  
     } 
} 
    } 

類結果1

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace MyGame 
{ 
    public class Result1 
    { 


     static private int[,] Winners = new int[,] 
        { 
         {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) 
     { 
      bool gameOver = false; 
      for (int i = 0; i < 8; i++) 
      { 
       int a = Winners[i, 0], b = Winners[i, 1], 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; 
        MessageBox.Show(b1.Text + " .... Wins the game!", "Game End", MessageBoxButtons.OK); 
        break; 
       } 
      } 
      return gameOver; 
     } 
    } 
} 

回答

1

Button處於System.Windows.Forms命名空間的類。
除非您導入命名空間,編譯器將不知道您想要哪個類。

您需要通過添加using System.Windows.Forms;到類的頂部導入命名空間..

+0

謝謝,工作!還有一個問題,我怎樣才能添加一個水平滾動條「xGameScrollBar」的列表框「xGameListBox」來選擇播放器? Player1 =(x)或Player2 =(O) – 2010-05-21 15:55:01

+0

我不確定你的意思。 – SLaks 2010-05-21 15:58:32

0

您需要添加一個using指令使用System.Windows.Forms的與您的文件RESULT1類,或明確引用System.Windows.Forms.Button。否則complire不知道按鈕是什麼。

相關問題