0

I'm making a little chess game in wpf, and I'm having trouble to find the right way I should move and display pieces on the chessboard...

To draw the chessboard I use a UniformGrid (8 * 8) which I fill on init in "code behind" with Rectangle shapes.

Then I want to add my pieces on this grid, which are png images in a Image control I also create in "code behind".

I thought about putting these images in the same cell of the grid as my Rectangle controls, but I see the issue that I wouldn't be able to animate the move of a piece that way, since it will go from one location to another directly.

So I thought about adding the pieces to the parent control, and then use the RenderTransform property to set their positions.

The thing is, I can't figure out what I have to do so that if the parent control is resized the pieces stay at the right position.

Is there a way to set the translation once and for all without having to redefine it at resize? I thought about assigning the position and size of the Rectangle elements on the pieces control, but this doesn't seem to be doable in wpf...

Am I just doing it all the wrong way and there is a better option to do this?

Thanks for your help, any suggestions are welcome.

ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • I would place the piece image in the square it belongs to on the `Grid`, and when a move is made, animate the move then change the piece's `Grid.Row` and `Grid.Column` to the new location at the end of the animation. That way you can calculate your animation based on whatever size the grid is at the time the animation runs. – Rachel Oct 09 '13 at 17:22
  • This question actually reminds me of [another answer](http://stackoverflow.com/a/13588066/302677) I wrote about WPF for a Minesweeper game. You might be interested in checking it. The game is different, however the concept and MVVM structure would be very similar. – Rachel Oct 09 '13 at 17:23
  • @Rachel Thanks for the suggestion :). Actually I don't use the MVVM pattern here since I just want a display in WPF (and perhaps later with directx or anything else), so all the properties and logic should remain in another layer. I just want positions to be updated. Also how would the animation work if I put the piece in grid cell? I'm having trouble to understand that – ppetrov Oct 09 '13 at 17:37
  • Well, you'd probably use an `ItemsControl` to display an 8x8 Grid, and have a `Row` and `Column` property on your piece model that's in the items's source. You'd bind the `ItemsSource` to your `List CurrentPieces`, and bind the `Grid.Row` and `Grid.Column` properties in the `ItemContainerStyle`. In the code-behind on move, you'd trigger an animation, then update the `Row` and `Column` properties of the piece object to their new value. – Rachel Oct 09 '13 at 18:01

0 Answers0