# Thread: Valid move checking in chess

1. ## Valid move checking in chess

I'm writing a chess game, to be played over
a socket. In my eyes, a function that made
a chess move would have to do something

int move(int old_r, int old_c, int new_r, int new_c)
{
if (checkMov(old_r, old_c, new_r, new_c)) {
board[new_r][new_c] = board[old_r][old_c];
board[old_r][old_c] = 0;
}
else printf("Invalid move.\n");

return 0;
}

My question is, how would a function that
examines if a move is valid work? I'm thinking that I could use a bunch of nested
conditionals, but that would be kind of nasty.
Does anyone else have a better way?

Transmitt

2. as a matter of fact, I do (: This might not be too clear to you as I explain this, but if it doesn't make sense, let me know and I'll clarify it...instead of having rows and columns, number the squares 1-64...now, you can use modulus (%) to help you out a little...say you are moving the rook towards the other side of the board...from square 1 to square 9.

1%8=1
9%8=1

all you have to check is that the % is the same when moving the rook that direction. Otherwise, you just add or subtract from the square number, and make sure it is in the same row...which could be done with if statements, if you would like...or you can do that with % too, a little differently, if you can't figure it out, ask (: Now, to move something like a knight, you can do it like this...

space - 16, space + 16, space + 18, space - 18, and 4 others, but you should get the point...then you check to make sure that it's in the proper row...it's actually kinda tough. Maybe you should ignore this post. Maybe I would think better earlier in the day...I'll think about your problem and post again later, probably (:

3. most likely your gonna end up with a lot of nested if's,
especially with checking particular piece moves.

4. I just happened to stumble across this, and I remembered your post...anyway, the bitboard looks intruiging if you think you are up to it.

http://www.aihorizon.com/essays/chessai/boardrep.htm

5. ...and about 10 seconds after posting that, I stumbled across this.

http://www.aihorizon.com/essays/chessai/index.htm

6. ## Thanks

Thanks for the help, Blackrat.
I really appreciate it. =)

- transmitt

7. well, it's an interesting topic, at least. I hope you get it working.