Im attempting to make a chess game, to move the pieces im using picking, so the user clicks on a piece to set it as the selected piece, then on the square they want to move it to. This works the first time, but when you click on a different piece to make that the selected piece, and then an empty square to move it, it moves the first selected piece instead. Here's my code and thanks in advance to anyone who can help.
string[,] Board =
{
{"BRook1", "BKnight1", "BBisop1", "BQueen", "BKing", "BBishop2", "BKnight2", "BRook2"},
{"BPawn1", "BPawn2", "BPawn3", "BPawn4", "BPawn5", "BPawn6", "BPawn7", "BPawn8"},
{null, null, null, null, null, null, null, null },
{null, null, null, null, null, null, null, null },
{null, null, null, null, null, null, null, null },
{null, null, null, null, null, null, null, null },
{"WPawn1", "WPawn2", "WPawn3", "WPawn4", "WPawn5", "WPawn6", "WPawn7", "WPawn8"},
{"WRook1", "WKnight1", "WBishop1", "WQueen", "WKing", "WBishop2", "WKnight2", "WRook2"},
};
MouseState mouseState = Mouse.GetState();
decimal decMousex = mouseState.X;
decimal decMousey = mouseState.Y;
y = Convert.ToInt32(Math.Floor(decMousex / 75));
x = Convert.ToInt32(Math.Floor(decMousey / 75));
if (mouseState.LeftButton == ButtonState.Pressed
&& Board[x,y] != null && pieceSelected == null)
{
pieceSelected = Board[x, y];
selectedX = x;
selectedY = y;
}
else if (mouseState.LeftButton == ButtonState.Pressed
&& Board[x,y] == null && pieceSelected != null)
{
Board[selectedX, selectedY] = null;
Board[x, y] = pieceSelected;
pieceSelected = null;
}
Look at what's happening when the mouse is pressed on an empty square, the pieceSelected will move there but then immediately next time round become the pieceSelected again because the mouse is still down unless you're really fast with the click.