Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hello to everybody,
I am doing an exercise with about just knight's movement on chess table...
I filled in flowLayoutPanel with button using nested for loop and my button's named btn[x,y]....I can drag pictureBox1.Image from outside on buttons...the knight must be moved as chess game( the L shaped)....when I dragdrop btn.Image I can see Red button BackColors according to the rules but I can't move the knight according to the rules.Where I am doing mistakes ?I felt tired to think about it.
How do I do this ?

C#
private void btn_MouseDown(object sender, MouseEventArgs e)
{   // Here I keep knight .
    Button button = sender as Button; 
    int[] array1 = (int[])button.Tag;
    int x = array1[0];
    int y = array1[1];
    if (e.Button == MouseButtons.Left && button.Image != null)
    {
        button.DoDragDrop(button.Image, DragDropEffects.Move);
    }
}
void btn_DragEnter(object sender, DragEventArgs e)
{// Here knight is moving
    Button button = (Button)sender;
    e.Effect = DragDropEffects.All;
    int[] array1 = (int[])button.Tag;
    int x = array1[0];
    int y = array1[1];
    //button.Image = null;
}


void btn_DragDrop(object sender, DragEventArgs e)
{ // ANd Here it must be implement the rules.When I left mouse left click button I can see the red BackColor as accordance with the rules.but my knight is going everywhere in chess table .İt shouldn't be like this 
    Button button = (Button)sender;
    button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
    int[] array1 = (int[])button.Tag;
    int x = array1[0];
    int y = array1[1];

       if ((x + 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x + 1 <= 7))
        {
            btn[x + 1, y + 2].BackColor = Color.Red;                    
        }
        if ((x + 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x + 1 <= 7))
        {
            btn[x + 1, y - 2].BackColor = Color.Red;
        }
        if ((x - 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x - 1 <= 7))
        {
            btn[x - 1, y + 2].BackColor = Color.Red;
        }
        if ((x - 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x - 1 <= 7))
        {
            btn[x - 1, y - 2].BackColor = Color.Red;
        }
        if ((x + 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x + 2 <= 7))
        {
            btn[x + 2, y + 1].BackColor = Color.Red;
        }
        if ((x + 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x + 2 <= 7))
        {
            btn[x + 2, y - 1].BackColor = Color.Red;
        }
        if ((x - 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x - 2 <= 7))
        {
            btn[x - 2, y + 1].BackColor = Color.Red;
        }
        if ((x - 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x - 2 <= 7))
        {
            btn[x - 2, y - 1].BackColor = Color.Red;
        }

    btn[x , y].DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}
Posted
Updated 28-Jun-12 3:24am
v6
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 2:24am    
This is not really a question but rather a request to do some of your job. What don't you understand about the methods of debugging? -- that's the key.
--SA
y.baris 28-Jun-12 4:02am    
I tried to debug but I am so new in C# ...it's not job just an exercise in my course
y.baris 28-Jun-12 4:11am    
I do not want you to do my exercise all..Just tell me where I am doing mistakes.And that's why I sent long source code maybe somebody can see my fault
Tim Corey 28-Jun-12 9:26am    
I'll put my recommendations here instead of as an answer since I'm not going to directly answer your question - I would recommend that you start with a good foundation in C# before you dive into a problem like this. It looks like you are trying to do complicated logic without understanding the basics that form that logic. That can be very frustrating for you. I know it is difficult to learn before jumping in, but that will make you much happier in the long run.
y.baris 28-Jun-12 10:09am    
Thanks for your recommendations but I want to do it myself and I will(even if frustates me :) )
Cause I know if I want to learn I have to do examples ,not all in theorically.I am trying to study theoric and making practices ..
I am just curious where am I doing mistakes ?

Hi,
Go through this link-
Chess Program in C#[^]

Drag and drop explained here with coding-
http://sharpchess.com/forums/viewtopic.php?t=26[^]

WPF: P2P Chess[^]
Yet Another Chess Board Control[^]
 
Share this answer
 
v3
Comments
y.baris 28-Jun-12 2:22am    
Can you help me with on my code ?
I saw your link but it's so complicated for me
y.baris 28-Jun-12 2:26am    
onn your example there is no drag and drop events..Dragdrop is making this harder
Tim Corey 28-Jun-12 9:23am    
Good recommendations.
Jαved 29-Jun-12 6:12am    
Thanks Tim.
Hello
Follow this algorithm:

1. Keep x and y of knight
2. Move the Knight
3. Keep New x and y (newX and New Y)
4. isCorrect = false
5. If newX and newY has occupied then Knight will Move back to x,y and end
6. IfnewX - 1 = x And newY - 2 = y then isCorrect = true;
7. If newX - 1 = x And newY + 2 = y then isCorrect = true;
8. If newX + 1 = x And newY - 2 = y then isCorrect = true;
9. If newX + 1 = x And newY + 2 = y then isCorrect = true;
10. If isCorrect = false then Knight will Move back to x,y
11. end
 
Share this answer
 
v4
Comments
y.baris 28-Jun-12 2:43am    
Can you help me with my code ?
I tried lots of but Ican't handle it
Shahin Khorshidnia 28-Jun-12 2:52am    
No, it's better to change your code (just a little)!
y.baris 28-Jun-12 2:54am    
I think I am doing mistakes inside the btn_DrapDrop event...I did red button backcolors when I leave mouse left button .I can see (according the chess rules ) but knight should go like in chess(L shaped)
y.baris 28-Jun-12 4:15am    
where I am doing mistakes ?
Shahin Khorshidnia 28-Jun-12 10:27am    
Sorry Y.Baris, I can't pay enough attention to your code, ( too busy right now:( ) but the algorithm is simple enough to solve your problem.
You assign
C#
button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
in btn_DragDrop() without any test whether the knight is allowed to move here or not. I think that would be OK, if you had such a test in btn_DragEnter. You could then set DragEventArgs.DragDropEffects = None which would prevent btn_DragDrop() from being called.

Second, AFAIR, the call to DoDragDrop() doesn't belong in your btn_DragDrop() method. It's for starting the drag operation as you use it correctly in btn_MouseDown().
 
Share this answer
 
Comments
y.baris 28-Jun-12 3:11am    
thanks for your answer.
Then how can I move the knight according to rules.
is there btn[x.y].Move or something else ?
lukeer 28-Jun-12 10:19am    
The rules are the same you already apply to mark the possible fields. If you can ensure that all allowed fields, and only the allowed ones, are marked with a red background, you can simply test for the red background to determine whether or not a drag is allowed or not.
Tim Corey 28-Jun-12 9:23am    
Good points.
y.baris 28-Jun-12 10:24am    
I will try again but
if I can't do I will ask again to you :)
y.baris 3-Jul-12 9:02am    
thanks lukeer for your help :)
I used btn[x,y].AllowDrop = false but
if (btn[x,y].BackColor == Color.Red) AllowDrop property must be true ...And ok everything is good now




private void Form1_Load(object sender, EventArgs e)
{
C#
pictureBox1.AllowDrop = true;
btn[x, y] = new Button();
btn[x, y].AllowDrop = true;

}

XML
void btn_DragDrop(object sender, DragEventArgs e)
        {
            Button button = (Button)sender;
            button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);

            int[] dizi = (int[])button.Tag;
            int x = dizi[0];
            int y = dizi[1];

            for (int a = 0; a <= 7; a++)
            {
                for (int b = 0; b <= 7; b++)
                {
                    btn[a, b].AllowDrop = false;
                }
            }

            if ((x + 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x + 1 <= 7))
            {
                btn[x + 1, y + 2].BackColor = Color.Red;
                btn[x + 1, y + 2].AllowDrop = true;
            }
            if ((x + 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x + 1 <= 7))
            {
                btn[x + 1, y - 2].BackColor = Color.Red;
                btn[x + 1, y - 2].AllowDrop = true;
            }
            if ((x - 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x - 1 <= 7))
            {
                btn[x - 1, y + 2].BackColor = Color.Red;
                btn[x - 1, y + 2].AllowDrop = true;
            }
            if ((x - 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x - 1 <= 7))
            {
                btn[x - 1, y - 2].BackColor = Color.Red;
                btn[x - 1, y - 2].AllowDrop = true;
            }
            if ((x + 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x + 2 <= 7))
            {
                btn[x + 2, y + 1].BackColor = Color.Red;
                btn[x + 2, y + 1].AllowDrop = true;
            }
            if ((x + 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x + 2 <= 7))
            {
                btn[x + 2, y - 1].BackColor = Color.Red;
                btn[x + 2, y - 1].AllowDrop = true;
            }
            if ((x - 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x - 2 <= 7))
            {
                btn[x - 2, y + 1].BackColor = Color.Red;
                btn[x - 2, y + 1].AllowDrop = true;
            }
            if ((x - 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x - 2 <= 7))
            {
                btn[x - 2, y - 1].BackColor = Color.Red;
                btn[x - 2, y - 1].AllowDrop = true;
            }
        }
 
Share this answer
 
v2

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