棋桌上的骑士运动 [英] Knight movement in chess table
问题描述
大家好,
我正在做练习,只是骑士在棋盘上的动作...
我使用嵌套的for循环和我的按钮名为btn [x,y]的按钮填充了flowLayoutPanel ....我可以将pictureBox1.Image从按钮上的外部拖动...骑士必须像国际象棋游戏一样移动(当我拖曳btn图像时,我可以看到红色按钮BackColors按规则显示,但我不能按照规则移动骑士. .
我该怎么办?
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 ?
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);
}
推荐答案
浏览此链接-
C#中的国际象棋程序 [ http://sharpchess.com/forums/viewtopic.php?t=26 [ ^ ]
WPF:P2P国际象棋 [又一个国际象棋棋盘控件 [
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[^]
你好
请遵循以下算法:
1.保留骑士的x和y
2.移动骑士
3.保留New x和y(newX和New Y)
4. isCorrect = false
5.如果newX和newY已被占用,那么Knight将移回x,y并结束
6.如果newX-1 = x并且newY-2 = y,则isCorrect = true;
7.如果newX-1 = x并且newY + 2 = y,则isCorrect = true;
8.如果newX +1 = x并且newY-2 = y,则isCorrect = true;
9.如果newX +1 = x并且newY + 2 = y,则isCorrect = true;
10.如果isCorrect = false,则Knight将移回x,y
11.结束
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
您在btn_DragDrop()
中分配
button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
没有任何测试,是否允许骑士移动到这里.我认为,如果您在btn_DragEnter
中进行了这样的测试,那将是可以的.然后可以设置DragEventArgs.DragDropEffects = None
,这将阻止调用btn_DragDrop()
.
其次,AFAIR,对DoDragDrop()
的调用不属于您的btn_DragDrop()
方法.您可以在btn_MouseDown()
中正确使用它来开始拖动操作.
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()
.
这篇关于棋桌上的骑士运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!