2013-11-02 33 views
-2

我正在做一個tic tac toe遊戲, 我希望當我完成遊戲時,我的意思是X或O Win(一個X或O的塊)我希望這個回合數字從1改變爲2,我清除了所有的字段(我用按鈕作爲字段,按鈕文本爲X或O) 這是代碼的一部分:B1,B2,B3變量,當他們有1作爲值意味着一個X是在那場如何添加1到此分數

if (B1 == 1 && B2 == 1 && B3 == 1) 
     { 
      MessageBox.Show("X Win"); 
      Form1 Form = new Form1(); 
      Form.Dispose(); 
     } 

完成,在Form.Load我寫了這個:

private void Form1_Load(object sender, EventArgs e) 
    { 
     round++; 
     label3.Text = "Round: " + round.ToString(); 
    } 

它不工作任何解決方案?

,我這裏定義了var

public Form1() 
    { 
     InitializeComponent(); 
    } 
    byte player = 1; 
    byte B1 = 0; 
    byte B2 = 0; 
    byte B3; 
    byte B4; 
    byte B5; 
    byte B6; 
    byte B7; 
    byte B8; 
    byte B9; 
    byte round; 
    byte Player1, Player2; 

整個代碼:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    byte player = 1; 
    byte B1 = 0; 
    byte B2 = 0; 
    byte B3; 
    byte B4; 
    byte B5; 
    byte B6; 
    byte B7; 
    byte B8; 
    byte B9; 
    byte round; 
    byte Player1, Player2; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B1 == 0) 
      { 
       button1.Text = "X"; 
       B1 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B1 == 0) 
      { 
       button1.Text = "O"; 
       B1 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B2 == 0) 
      { 
       button2.Text = "X"; 
       B2 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B2 == 0) 
      { 
       button2.Text = "O"; 
       B2 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B3 == 0) 
      { 
       button3.Text = "X"; 
       B3 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B3 == 0) 
      { 
       button3.Text = "O"; 
       B3 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button5_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B5 == 0) 
      { 
       button5.Text = "X"; 
       B5 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B5 == 0) 
      { 
       button5.Text = "O"; 
       B5 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B4 == 0) 
      { 
       button4.Text = "X"; 
       B4 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B4 == 0) 
      { 
       button4.Text = "O"; 
       B4 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button9_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B9 == 0) 
      { 
       button9.Text = "X"; 
       B9 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B9 == 0) 
      { 
       button9.Text = "O"; 
       B9 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button7_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B7 == 0) 
      { 
       button7.Text = "X"; 
       B7 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B7 == 0) 
      { 
       button7.Text = "O"; 
       B7 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button8_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B8 == 0) 
      { 
       button8.Text = "X"; 
       B8 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B8 == 0) 
      { 
       button8.Text = "O"; 
       B8 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     if (player == 1) 
     { 
      if (B6 == 0) 
      { 
       button6.Text = "X"; 
       B6 = 1; 
       player = 2; 
      } 
     } 
     else if (player == 2) 
     { 
      if (B6 == 0) 
      { 
       button6.Text = "O"; 
       B6 = 2; 
       player = 1; 
      } 
     } 
    } 

    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (B1 == 1 && B2 == 1 && B3 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B4 == 1 && B5 == 1 && B6 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B7 == 1 && B8 == 1 && B9 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B1 == 1 && B4 == 1 && B7 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B2 == 1 && B5 == 1 && B8 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B3 == 1 && B6 == 1 && B9 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B1 == 1 && B5 == 1 && B9 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 
     if (B3 == 1 && B5 == 1 && B7 == 1) 
     { 
      MessageBox.Show("X Win"); 
     } 

    } 

    private void button10_Click(object sender, EventArgs e) 
    { 
     Form2 Form2 = new Form2(); 
     Form2.Show(); 
     this.Hide(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     round++; 
     label3.Text = "Round: " + round.ToString(); 
    } 
+0

你在哪裏定義了'round'? – Szymon

+1

我認爲我們需要更完整的概述。如果round不是全局變量,它將不起作用。 –

+0

你會聲明64個方法,比如'private void buttonNNN_Click(object sender,EventArgs e)'如果它是一個象棋遊戲嗎? –

回答

0

您需要存儲變量round以外的形式。當你每次摧毀表格時,當前的回合數字也會丟失。

無論您打開表單的哪個位置,您都需要存儲它。我還建議您在打開表單並將增加的值傳遞給表單以顯示之前增加圓數。這樣你就可以在一個地方保留關於輪次的所有邏輯。

+0

你從哪裏打開你的Form1? – Szymon