-2

我正在做一个井字游戏,我希望当我完成游戏时,我的意思是 X 或 O 赢(一个 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();
    }
4

1 回答 1

0

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

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

于 2013-11-02T21:12:00.760 回答