Click here to Skip to main content
15,890,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example i have only two pieces`pawn and bishop,i need to check that two pieces located on the same line,it's mean that bishop can hit pawn, then when i click on button Check, i need All the diagonal drawers available for the bishop to be painted in red.

What I have tried:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            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)
        {
           
            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);
            }
        }


 private void button2_Click(object sender, EventArgs e)
        {
           //This is my Check button,How can i check it? what should i write here?
        }
Posted
Updated 6-Mar-18 21:49pm
Comments
Richard MacCutchan 7-Mar-18 5:01am    
You are still doing it wrong. Learn how to create proper classes and use inheritance and common methods. Until you learn how to write proper C# code you are not going to make any progress with this.

We can't just solve every problem for you!
You haven't appeared to implement a board, which you need to do in order to do this task, as well as the last one: C# simple chess error[^]

I would strongly suggest that you approach this by creating an abstract Piece class which has a location, and a "side" (Black or White) and a Board class that encapsulates a 2D array of Piece instances.
Then declare your Pawn class as a Piece, your Rook as a Piece, and so on.
You can then start delegating tasks to the classes.
By adding an abstract ShowMoves method to the Piece class and passing it a Board you can get the actual piece to generate the legal moves from its current position - and use that to generate a "map" of where to draw red squares.
Similarly, you delegate the Paint task to the Board - passing it the graphics context - and it delegates actual piece painting to the Piece class through another abstract method.

What you appear to be doing at the moment is jumping into code without actually planning what you need - and that doesn't generate a good, solid application at the end, it generates a hard-to-maintain mess that is riddled with bugs.
 
Share this answer
 
Comments
Maciej Los 7-Mar-18 3:52am    
5ed!
All wrong! You've been talking about this with Richard MacCutchan[^], especially here: C# simple chess error[^].

The conclusion from above discussion is: you should work on data, not on its string representation!

I'd strongly recommend to take a look at "working example": Chess Program in C#[^]. That's the way you have to do this.

More examples you'll find on: CodeProject's KnowledgeBase[^]
 
Share this answer
 
v3

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