problem with moving pieces[solved]
Posted: Fri May 21, 2010 8:58 am
So I'm working on a chess program and am working on legal moves, so far I have pawns and knights fully implemented but am having a little bit more trouble with the rook, how the pieces move on the board is the user selects a piece that piece is on tempSquare, and selects the square they want to move it to (selectedSquare), I then do a check to see if that piece is a rook, if it is then it goes through the code below. 12 is a vertical move, and 1 is a horizontal move. The problem with this code is that if there is a free square beside the rook it can move anywhere, and when it moves horizontal it doesn't register blocked squares, I've been trying to think of a different way to approach this problem for the last couple of days, well at least when I have time to do so, anyone have any ideas on how I should go about solving this?
Code: Select all
void checkRook()
{
setBlocked(12);
setBlocked(1);
}
Code: Select all
void setBlocked(int x)
{
//UP the board
if (tempSquare > selectedSquare)
{
for (int i = tempSquare-x; i > selectedSquare; i-=x)
{
if (textBoard[i] != EMPTY)
{
blocked = true;
}
}
checkBlocked();
}
//Down the board
else if (tempSquare < selectedSquare)
{
for (int i = tempSquare+x; i < selectedSquare; i+=x)
{
if (textBoard[i] != EMPTY)
{
blocked = true;
}
}
checkBlocked();
}
}
Code: Select all
void checkBlocked()
{
if (blocked == false)
{
movePiece();
}
else if (blocked == true)
{
blocked = false;
}
}