在国际象棋比赛中获得国王的位置

发布于 2024-10-06 15:58:19 字数 775 浏览 10 评论 0原文

我正在写一个简单的国际象棋游戏,有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ可否回来 2024-10-13 15:58:19

最简单的方法是要求用户单击相关的图片框。您可以通过以下三种方式之一确定哪个 PictureBox 被单击:

  • 对每个 PictureBox 使用相同的事件处理程序进行订阅,但使用 PictureBox 中的标签来获取坐标
  • 对每个 PictureBox 使用相同的事件处理程序进行订阅,但仅使用引用到 PictureBox(sender)来查找它是哪一个并适当地获取坐标
  • 使用封装位置的 lambda 表达式进行订阅,如下所示:

    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            pictureBoxes[x, y].Click += (sender, args) =>设置KingPosition(x, y);
        }
    }
    

后一种方法的一个问题是通过 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:

  • Subscribe using the same event handler for each PictureBox, but use a Tag within the PictureBox to get the coordinates
  • Subscribe using the same event handler for each PictureBox, but just use the reference to the PictureBox (sender) to find which one it was and get the coordinates appropriately
  • Subscribe using a lambda expression which encapsulates the position, like this:

    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            pictureBoxes[x, y].Click += (sender, args) => SetKingPosition(x, y);
        }
    }
    

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 :)

桃气十足 2024-10-13 15:58:19

这是一个想法,而不是弄乱大量的 PictureBox。

创建一个名为 ChessBoardControl 的用户控件。 ChessBoardControl 将负责绘制给定的 ChessBoard 对象。

ChessBoardControl中,您可以重写OnMouseDown事件,然后根据鼠标的坐标,您可以轻松计算出哪个方格被点击。

Here's an idea, instead of messing with lots of PictureBoxes.

Create a UserControl called ChessBoardControl. The ChessBoardControl will take care of drawing ChessBoard objects that it is given.

In the ChessBoardControl, you can override the OnMouseDown event, and then from the coordinate of the mouse, you can easily calculate which square was clicked.

你的背包 2024-10-13 15:58:19

在您的新代码中,您有两个 for 循环迭代 ij,但在循环中您始终将事件处理程序分配给 pic[ x,y]。

因此,您只需为 pic[0,0] 分配一个处理程序。 xy 永远不会改变...

编辑:
坦率地说,我不确定我是否理解你的代码。您希望 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文