在国际象棋比赛中获得国王的位置
我正在写一个简单的国际象棋游戏,有 3 个棋子,一侧有一个国王和一个王后,另一侧只有一个国王,国王和王后应该用较少的动作来交配单独的国王。在这个项目中,首先我们应该从用户然后据此做其他工作,我想知道如何从用户那里得到国王应该呆在哪个地方?我实现了带有 64 个图片框的表单。我将为位置编写类,非常感谢
编辑:
我从 Jon Skeet 先生的回答中写了这段代码: 它有什么错误?因为当我点击时它没有做任何事情,谢谢
PictureBox[,] pic = new PictureBox[8, 8];
public PictureBox SetKingImage(int x,int y)
{
pic[x,y].Image=Image.FromFile("pic/siyahsah2.JPG");
return pic[x, y];
}
public void GetClickedPicturebox()
{
int x, y;
for(x=0;x<8;x++)
{
for(y=0;y<8;y++)
{
pic[x, y] = new PictureBox();
pic[x, y].Click += (object sender, System.EventArgs e) =>SetKingImage(x, y);
}
}
}
i m writing a simple chess game that has 3pieces,a king and a queen one side and other side just a king,the king and queen should mate alone king with less movements.in this project,first we should get the place of alone king from user and then according to that do other works,i want to know how can i get from user that in which place should king stay?i implemented form with 64 pictureboxes.i will write class for positions,thanks so much
edited:
i wrote this code from Mr Jon Skeet answer:
what mistake does it have?because it doesnt do anything when i click,thanks
PictureBox[,] pic = new PictureBox[8, 8];
public PictureBox SetKingImage(int x,int y)
{
pic[x,y].Image=Image.FromFile("pic/siyahsah2.JPG");
return pic[x, y];
}
public void GetClickedPicturebox()
{
int x, y;
for(x=0;x<8;x++)
{
for(y=0;y<8;y++)
{
pic[x, y] = new PictureBox();
pic[x, y].Click += (object sender, System.EventArgs e) =>SetKingImage(x, y);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是要求用户单击相关的图片框。您可以通过以下三种方式之一确定哪个 PictureBox 被单击:
sender
)来查找它是哪一个并适当地获取坐标使用封装位置的 lambda 表达式进行订阅,如下所示:
后一种方法的一个问题是通过 lambda 表达式取消订阅事件相对痛苦 - 并且您可能想要取消订阅 单击第一个按钮时的所有事件处理程序。
另一种方法是将事件处理程序保留在适当的位置,并在时机不对时忽略它们:)
The simplest approach would be to ask the user to click on the relevant picture box. You could determine which PictureBox was clicked on in one of three ways:
sender
) to find which one it was and get the coordinates appropriatelySubscribe using a lambda expression which encapsulates the position, like this:
One problem with the latter approach is that unsubscribing from an event via a lambda expression is relatively painful - and you probably want to unsubscribe all the event handlers when the first button is clicked.
An alternative is to leave the event handlers in place, and ignore them when it's not the right time :)
这是一个想法,而不是弄乱大量的 PictureBox。
创建一个名为
ChessBoardControl
的用户控件。ChessBoardControl
将负责绘制给定的ChessBoard
对象。在
ChessBoardControl
中,您可以重写OnMouseDown
事件,然后根据鼠标的坐标,您可以轻松计算出哪个方格被点击。Here's an idea, instead of messing with lots of PictureBoxes.
Create a UserControl called
ChessBoardControl
. TheChessBoardControl
will take care of drawingChessBoard
objects that it is given.In the
ChessBoardControl
, you can override theOnMouseDown
event, and then from the coordinate of the mouse, you can easily calculate which square was clicked.在您的新代码中,您有两个
for
循环迭代 i 和 j,但在循环中您始终将事件处理程序分配给 pic[ x,y]。因此,您只需为 pic[0,0] 分配一个处理程序。 x 和 y 永远不会改变...
编辑:
坦率地说,我不确定我是否理解你的代码。您希望
GetClickedPicturebox
执行什么操作?你什么时候打电话?如果您打算调用它来获取单击的 PictureBox,那么这是错误的,因为您将事件处理程序附加到新的 PictureBox 实例,而不是窗体上的实例。
如果此方法是作为初始化方法,那么它可能是错误的,因为您只是创建新的 PictureBox 实例,但没有在表单上显示它们。如果您的窗体上有 PictureBox,则它们是不同的实例,并且您没有将事件处理程序附加到正确的实例。
In your new code you have two
for
loops iterating over i and j, but in the loop you are always assigning an event handler to pic[x,y].So you're only assigning an handler to pic[0,0]. x and y never change...
Edit:
Frankly I'm not sure I understand your code. What did you want the
GetClickedPicturebox
to do? And when are you calling it?If you meant to call it in order to get the PictureBox that was clicked, it's wrong because you are attaching the event handlers to new PictureBox instances, instead of to those that are on your Form.
If this method was meant as an initialization method, then it's probably wrong because you are simply creating new PictureBox instances, but you are not showing them on your Form. If there are PictureBox on your Form, they are different instances and you did not attach the event handlers to the correct ones.