0

I'm trying to build a Checkers/Draughts game in C#. The project is build using Windows Form.

What I did is to decompose the game board. So I have a "Board" class. and "BoardSquare" class (this class draw a square in the right position and color)

The board is build from 8 X 8 BoardSquare.

I thought about this solution, the square needs to know if it's empty or not, and if it is not empty it means that there is a "GameSoldier" on it.

So when I want to move the soldier to another square I need to click on the current square to get the GameSoldier, and then to press on the destination square.

So what I want to do is to add mouse functionality to the BoardSquare class to get the properties of the squares, like positions. Any ideas?

bash.d
  • 13,029
  • 3
  • 29
  • 42
Elior
  • 3,178
  • 6
  • 37
  • 67
  • Well, you have spent some thoughts on this. Get in touch with WinForms, see how event handling works and you will get a start! – bash.d Feb 20 '13 at 15:45
  • Sounds like a homework. What have you tried so far? Any code snippets? Basically, the click event of your square should return the position property and its content. – Sashenka Feb 20 '13 at 15:45

3 Answers3

1

You can get the coordinates of the cursor from the EventArgs when the board is clicked. To translate these coordinates to a square on the board, you have to layout the tiles from top left to bottom right, across rows and store them in an indexed list or array.

The X coordinate (in "tiles") is then Math.Floor(X_Clicked / TileWidth)

And the Y coordinate is Math.Floor(Y_Clicked / TileHeight)

For example, if your tiles are 50 pixels wide and 50 pixels high, and the user clicks at (329, 186) this would be the tile at:

329 / 50 = 6,58 = 6

186 / 50 = 3,72 = 3

The tile at (6, 3) was clicked. To convert this to an index in the list/array:

x + (y * tiles_in_a_row) = 6 + (3 * 8) = 30, so Tile[30] was clicked.

Remember that this is a zero-based coordinate system, so the first tile is at (0, 0).

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • thanks, the all game is draw by the Graphics. so what i did, is to create a square class and what i'm trying to do is to add the square class the mouse functionality, so the user will be able to press on it – Elior Feb 22 '13 at 09:59
1

There's couple of ways to go about this, your boardsquare class might get in the way of the simple one because it will take focus.

When you click on an occupied board square it's mouse event fires. It's position on the board can be identified by it's Top / Height and Left / width Armed with that you can then pass that up to a BoardSquareSelected event handler defined on the board. When you click on an unoccupied square mouse position is x / square width ,y / square height You'll need a bit more logic for first click is occupied, second isn't legal move and such. You could look at a drag and drop derivative as well. But personally I'd get rid of boardsquare as a component. If board has an 8x8 array of squares and you use Invalidate(rect) to avoid redrawing the entire thing on any change, you don't really need it. Then all your selection stuff is wholly encapsulated by the board class.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • The user can select to board size. it can be 4 X 4 , 8 X 8 or 10 X 10 this why I create BoardSquare class – Elior Feb 20 '13 at 16:38
  • That's a property of board. How big the square is on the screen is a property of the board. Saying it's property of square implies you could have different sized squares on the same board. Besides you just ran into square getting the way, if you insist on keeping it, expect a few more. Think of it this way, up to 100 less components in your app... – Tony Hopkinson Feb 20 '13 at 20:36
0

What you want to do is add an Eventhandler for the MouseClick event of the control you are rendering you're board in. In the EventArgs for that event you will be able to retrieve the mouse coordinates. You can then combine that information with the size of the control itself to calculate which square has been clicked.

Here is some more information:

Obtain Position/Button of mouse click on DoubleClick event

(especially Moose's answer)

Community
  • 1
  • 1
b0wter
  • 148
  • 7
  • Thanks for your answer. I'm not using the PictureBox control.. the all game needs to be draw on the form, with the Graphics – Elior Feb 20 '13 at 15:52
  • Sorry for the late answer, I am sure you already figured it out. Just for the record, it doesn't matter in what kind of control you're drawing. They all behave the same. So you can directly paint onto a form the very same way as paint into a PictureBox. – b0wter Apr 30 '13 at 13:13