tags:

views:

211

answers:

2

Hi,

Firstly: apologies if this is a duplicate post. Things got a bit confusing as I'm trying to post/register at same time.

I started investigating running UCI chess engines from a simple WPF window, got the hang of having the chess engine running onf a different thread to the interface, and have created a reasonably servcieable text-based front end.

I'm getting a bit more ambitious now, and would like to start building a GUI with chess pieces on it that will feed the player's moves to the chess engine, and represent the engine's moves on the board as well. I'm aiming for draggable pieces rather than clicking squares.

My current attempts involve using draggable user controls for the pieces on a <canvas> element. I'd be really interested to hear how other, more experienced WPF/.NET programmers would approach this, as I'm not entirely convinced I'm on the right track.

For example: would it be better to use a uniform grid and drag piece data between child elements? Should I create an abstract 'piece' class from which pieces such as pawns could derive? That kind of thing.

Any thoughts? This isn't a homework assignment or anything, just something I'm noodling around with in my spare time as an exercise.

+1  A: 

There is a Sample WPF Chess Application at http://www.valil.com/winfx/. This might help you if building this.

Bhuvan
Valil's program is fantastic, thank you for the link, Bhuvan :)
dogsolitude_uk
+2  A: 

I have implemented a chess board for my Silverlight Online Chess system.

Here is how I did it.

  1. I made a seperate user control for the chess board
  2. I added a grid 8x8 onto the control
  3. I then added 64 Borders shading each one a different color (dark squares and light squares) Make sure to name each one. Each of the borders were placed on the grid using the Grid.Row and Grid.Col properties.
  4. Within each Border I added an Image that will hold the chess piece image.
  5. You will have to code some methods around setting the image to the correct chess piece based on your current game state.
  6. Each of the Images received the same event (this is important), all 64 call the same piece of code:

    MouseLeftButtonDown="Image_MouseLeftButtonDown" MouseMove="Image_MouseMove" MouseLeftButtonUp="Image_MouseLeftButtonUp"

The idea behind these 3 events is that we record when I click on the image (MouseLeftButtonDown) that gets me the origin of the click, then I call the event as the mouse is moving, that allows me to update the screen as the piece is moving, and the last event I record when I let go of the mouse button (MouseLeftButtonUp), this allows me to get the destination and send the move to my chess engine. Once the move is recorded by the chess engine I just redraw the chess board.

private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Image image = (Image)sender;
    Border border = (Border)image.Parent;

    image.CaptureMouse();
    isMouseCapture = true;
    mouseXOffset = e.GetPosition(border).X;
    mouseYOffset = e.GetPosition(border).Y;

    var chessPiece = (Image) sender;
    var chessSquare = (Border) chessPiece.Parent;

    var row = (byte) (Grid.GetRow(chessSquare));
    var column = (byte) (Grid.GetColumn(chessSquare) - 1);

    if (engine.HumanPlayer == ChessPieceColor.White)
    {
        SelectionChanged(row, column, false);
    }
    else
    {
        SelectionChanged((byte)(7 - row), (byte)(7 - column), false);
    }
}

SelectionChanged is my own method for recording what source square the user selected. isMouseCapture is also my own variable for recording when the user started draging the piece.

private void Image_MouseMove(object sender, MouseEventArgs e)
{

    Image image = (Image)sender;
    Border border = (Border)image.Parent;


    if (!currentSource.Selected)
    {
        image.ReleaseMouseCapture();
        isMouseCapture = false;

        translateTransform = new TranslateTransform();

        translateTransform.X = 0;
        translateTransform.Y = 0;

        mouseXOffset = 0;
        mouseYOffset = 0;
    }



    if (isMouseCapture)
    {
        translateTransform = new TranslateTransform();

        translateTransform.X = e.GetPosition(border).X - mouseXOffset;
        translateTransform.Y = e.GetPosition(border).Y - mouseYOffset;

        image.RenderTransform = translateTransform;

        CalculateSquareSelected((int)translateTransform.X, (int)translateTransform.Y, false);


    }
}

In the above CalculareSquareSelected converts the pixels moved to where I think the piece is moving to in the 8x8 chess board. For example say I moved 100 pixels and the chess board square is only 50 pixels than I moved 2 chess board squares.

private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (translateTransform == null)
    {
        return;
    }

    Image image = (Image)sender;

    image.ReleaseMouseCapture();
    isMouseCapture = false;

    if (translateTransform.X > 10 || translateTransform.Y > 10 || translateTransform.X < -10 || translateTransform.Y < -10)
    {
        CalculateSquareSelected((int)translateTransform.X, (int)translateTransform.Y, true);
    }
    translateTransform = new TranslateTransform();

    translateTransform.X = 0;
    translateTransform.Y = 0;

    mouseXOffset = 0;
    mouseYOffset = 0;

    image.RenderTransform = translateTransform;

}

If you have any questions feel free to contact me.

Adam Berent
Fantastic, thanK you very much Adam! This is exactly what I was looking for :) It's friday night, think I'll give it a go this evening, and I'll be sure to check out your website properly too.I had been attempting to bind the contents of a UniformGrid to the contents of an array... Your approach looks a lot more sensible.One further question: where did you get the graphics for the chess pieces? They look very familiar, similar to the ones in Fritz, Peshka etc. Are they a standard set of chess graphics from somewhere?
dogsolitude_uk
I actually paid someone to develop those chess piece graphics on rentacoder.com. It only cost me about $40.If you would like more information on developing a chess engine visit my computer chess blog at www.chessbin.com. There is lots of code examples there. Also feel free to get my contact info from adamberent.com and email me any questions you have.
Adam Berent