Click here to Skip to main content
15,894,324 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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);
            }
        }
Posted
Updated 6-Mar-18 3:16am
v2
Comments
Richard MacCutchan 6-Mar-18 6:46am    
That logical operator should be &&, not ||. But what about all the other pieces?
Suren97 6-Mar-18 6:52am    
i don't need all pieces, now i need only to check for that two pieces`pawn and bishop, i also tried operator && but it's not working too
Richard MacCutchan 6-Mar-18 8:42am    
Then you are doing something wrong. But since you have not given any details we cannot guess what.
Suren97 6-Mar-18 8:45am    
i can send you my Form1_Paint function
Richard MacCutchan 6-Mar-18 8:58am    
If you have useful information about your problem, then please use the Improve question link above, and add it to your original query.

1 solution

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.


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.
 
Share this answer
 
v2
Comments
Suren97 6-Mar-18 8:22am    
Where and how should i declare 8 x 8 array of pieces?
OriginalGriff 6-Mar-18 10:12am    
MyType[,] board = new MyType{8,8];

Where "MyType" is whatever type is applicable for storing your pieces: probably an abstract ChessPiece class, with concrete Pawn, Rook, Knight, Bishop, King, and Queen classes derived from that.
mvdk72 6-Mar-18 10:05am    
You can use a two dimensional array (see : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays). Best place to declare it is globally based on what I read here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900