如何检查我的按钮文本是否相同? [英] How to check if the text of my buttons are the same?

查看:83
本文介绍了如何检查我的按钮文本是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Tic Tac Toe游戏。


目前,当有人赢得比赛时,我需要一些帮助。


我的鳕鱼看起来像这样:


  public partial class Form1:Form

    {

        int skrivaut = 1;



       按钮[,]数组;

        public Form1()

        {

            InitializeComponent();




            array = new Button [,] {{button1,button2,button3},

                                    {button4,button5,button6},

                                    {button7,button8,button9}};
$


            if(array [0,0] == array [0,1]&& array [0,1] == array [0,2])

            {

                MessageBox.Show("游戏结束!我们有一个胜利者");

            }¥b $ b           否则if(array [1,0] == array [1,1]&& array [1,1] == array [1,2])

            {

                MessageBox.Show("游戏结束!我们有一个胜利者");

            }¥b $ b           否则if(array [2,0] == array [2,1]&& array [2,1] == array [2,2])

            {

                MessageBox.Show("游戏结束!我们有一个胜利者");

            }¥b $ b            else if(array [0,0] == array [1,1]&& array [0,0] == array [2,2])

            {

                MessageBox.Show("游戏结束!我们有一个胜利者");

            }¥b $ b           否则if(array [0,2] == array [1,1]&& array [0,2] == array [2,2])

            {

                MessageBox.Show("游戏结束!我们有一个胜利者");

            }¥b $ b         } 

        private void button1_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button1.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button1.Text =" X";

            }¥b $ b        }


        private void button2_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button2.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button2.Text =" X";

            }¥b $ b        }


        private void button3_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button3.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button3.Text =" X";

            }¥b $ b        }


        private void button6_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button6.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button6.Text =" X";

            }¥b $ b        }


        private void button5_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button5.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button5.Text =" X";

            }¥b $ b        }


        private void button4_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button4.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button4.Text =" X";

            }¥b $ b        }


        private void button9_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button9.Text =" O";
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button9.Text =" X";

            }¥b $ b        }


        private void button8_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button8.Text =" O"; $
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button8.Text =" X";

            }¥b $ b        }


        private void button7_Click(object sender,EventArgs e)

        {

            skrivaut + = 1;

$
            if(skrivaut%2 == 0)

            {

                button7.Text =" O";
            }¥b $ b            if(skrivaut%2 == 1)

            {

                button7.Text =" X";

            }¥b $ b        }


任何想法?



        

解决方案

我会查看以下示例,不要复制它,而是要从中学习。


https://www.codeproject.com/Articles/2400 /井字棋-在-C


I'm trying to cod the Tic Tac Toe game.

At the moment I need some help telling when someone have won the game.

My cod looks like this:

 public partial class Form1 : Form
    {
        int skrivaut = 1;

        Button[,] array;
        public Form1()
        {
            InitializeComponent();


            array = new Button[,] { {button1, button2, button3 },
                                    {button4, button5, button6 },
                                    {button7, button8, button9 }};

            if (array[0, 0] == array[0, 1] && array[0, 1] == array[0, 2])
            {
                MessageBox.Show("Game over! We have a winner");
            }
            else if (array[1, 0] == array[1, 1] && array[1, 1] == array[1, 2])
            {
                MessageBox.Show("Game over! We have a winner");
            }
            else if (array[2, 0] == array[2, 1] && array[2, 1] == array[2, 2])
            {
                MessageBox.Show("Game over! We have a winner");
            }
            else if (array[0, 0] == array[1, 1] && array[0, 0] == array[2, 2])
            {
                MessageBox.Show("Game over! We have a winner");
            }
            else if (array[0, 2] == array[1, 1] && array[0, 2] == array[2, 2])
            {
                MessageBox.Show("Game over! We have a winner");
            }
         } 
        private void button1_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button1.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button1.Text = "X";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button2.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button2.Text = "X";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button3.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button3.Text = "X";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button6.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button6.Text = "X";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button5.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button5.Text = "X";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button4.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button4.Text = "X";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button9.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button9.Text = "X";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button8.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button8.Text = "X";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            skrivaut += 1;

            if (skrivaut % 2 == 0)
            {
                button7.Text = "O";
            }
            if (skrivaut % 2 == 1)
            {
                button7.Text = "X";
            }
        }

Any idea?

        

解决方案

I would look to the following example, not to copy it but to learn from it.

https://www.codeproject.com/Articles/2400/Tic-Tac-Toe-in-C


这篇关于如何检查我的按钮文本是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆