开发者

geting the place of king in chess game

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 h开发者_如何学编程ow 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);


            }
          }
      }


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜