C#简单的国际象棋错误 [英] C# simple chess error

查看:125
本文介绍了C#简单的国际象棋错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有棋盘,在右边我有Combobox,文本框和按钮,从组合框我应该选择一块作品例如`bishop,pawn等等,在文本框中我应该写它的坐标例如`D5,然后单击按钮创建,但我也有测试按钮,它检查板上是否存在所有碎片,例如,如果我除了pawn之外我在板上有所有碎片,当我点击按钮Test时,它会给我MessageBox错误。

如何查看?

谢谢。



我尝试了什么:



我试过这样但是这是假的。



i have chess board,in the right side i have Combobox, textbox and buttons, from combobox i should choose type of piece for example` bishop,pawn and so on, in textbox i should write its coordinates for example`D5,and then click on button Create,but i have also Test button,which checks if all pieces exist on the board,for example if i have all pieces on the board besides pawn, when i click on button Test, it will give me MessageBox error.
How can i check it?
Thanks.

What I have tried:

I have tried like this but it's false.

private void Test_Click(object sender, EventArgs e)
        {
            if(comboBox1.SelectedItem != "Bishop" || comboBox1.SelectedItem != "Pawn")
            {
                MessageBox.Show("Error");
            }
        }










private void Form1_Paint(object sender, PaintEventArgs e)//This is my Chess board
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if((i+j) % 2 != 0)
                    gf.FillRectangle(Brushes.Black, (j + 1) * 50, (i+1)*50, 50, 50);
                }
                Label lb = new Label();
                lb.Text = Convert.ToChar('A' + i).ToString();
                lb.Font = new Font("Tahoma", 18);
                lb.Location = new Point(50 * (i + 1), 450);
                lb.Width = 50;
                lb.Height = 26;
                this.Controls.Add(lb);
            }
            for (int i = 8; i > 0; i--)
            {
                Label lab = new Label();
                lab.Text = i.ToString();
                lab.Font = new Font("Tahoma", 18);
                lab.Location = new Point(0, 50 *(9- i));
                lab.Width = 50;
                lab.Height = 26;
                this.Controls.Add(lab);
            }
        }


 private void create_Click_1(object sender, EventArgs e) //This is my Create button
        {
           
            if (comboBox1.SelectedItem == "Bishop")
            {
                string position = textBox1.Text;
                string first_letter = position[0].ToString().ToUpper();
                int x = (int)(first_letter[0] - 64);
                int y = int.Parse(position[1].ToString());
                gf.DrawImage(Properties.Resources.Bishop, x * 50, (8 - y + 1) * 50, 50, 50);
            }
            else if(comboBox1.SelectedItem == "Pawn")
            {
                string position = textBox1.Text;
                string first_letter = position[0].ToString().ToUpper();
                int x = (int)(first_letter[0] - 64);
                int y = int.Parse(position[1].ToString());
                gf.DrawImage(Properties.Resources.Pawn, x * 50, (8 - y + 1) * 50, 40, 50);
            }
        }

推荐答案

Richards改变,是正确的:想想当你使用||时会发生什么相反:

Richards change, is correct: think about what happens when you use "||" instead:
If the selected item is "Bishop" than it isn't "Pawn" - result == true.
If the selected item is "Pawn" than it isn't "Bishop" - result == true.
If the selected item neither "Bishop" nor "Pawn" - result == true.



使用&&相反它效果更好:


With "&&" instead it works better:

If the selected item is "Bishop" - result == false.
If the selected item is "Pawn" - result == false.
If the selected item is neither "Bishop" nor "Pawn" - result == true.





但是赢了' t帮助你做你想做的事情,因为除非你的代码专门删除它们,否则值保留在组合框中。



你需要做的是保持一个棋盘 - 一个8 x 8阵列 - 在你的记忆中。当用户添加一块时,将其放入适当的阵列位置。

当他检查测试按钮时,扫描板上的所有部分(通过查看阵列)查找放置什么物品。



But that won't help you with what you are trying to do, as the values remain in the combo box unless your code specifically removes them.

What you need to do is keep a "chess board" - a 8 x 8 array of pieces - in your memory. When he the user adds a piece, put it in the appropriate array location.
When he checks the "Test" button, scan through all the pieces on the board (by looking at the array) to find for what items are placed on it.


这篇关于C#简单的国际象棋错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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