如何在此分数中添加1

时间:2013-11-02 21:04:38

标签: c# .net visual-studio-2010 visual-studio visual-studio-2008

我正在做一个tic tac toe游戏, 我希望当我完成游戏时,我的意思是X或O Win(一个X或O块)我希望圆数从1变为2并且我清除所有字段(我使用按钮作为字段和按钮文本作为X或O) 这是代码的一部分:B1,B2,B3变量,当它们的值为1表示在该字段上完成X时

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();
    }

1 个答案:

答案 0 :(得分:0)

您需要将变量round存储在表单之外。当你每次销毁表格时,当前的轮数也会丢失。

您需要将它存储在打开表单的任何位置。我还建议在打开表单之前增加整数,并将增加的值传递给表单进行显示。通过这种方式,您可以在一个地方保留所有关于回合的逻辑。