i m making chess in java swing and does not getting how to implement check & checkmate condition. any sugessions plz

thnx in advance

Check is extremely simple actually. If any piece can currently move to the king's position, then the king is in check. Since you already have to implement the ability to allow players to move any given piece to any square (that their piece can move to), deciding if those pieces can be moved to the king's square should be trivial.

For checkmate, it is a little harder, but first decide whether the king can move his piece to a square that puts him out of check (by temporarily 'pretending' the king is at a different square, and seeing if he is in check still, and doing that for every square around him). If he can't, it still might not be checkmate. So now you have to see if there is any piece that can either be moved to a square that blocks the 'check' or that can take the piece that is causing the checkmate.

I suggest you make high usage of the "write a method that does one thing and one thing only" principle. Don't write spaghetti code because that could get you into a world of trouble here.

I actually might write a chess game in Swing when I get a chance just for the heck of it.

Edited 7 Years Ago by BestJewSinceJC: n/a

Comments
this man is nothing....................................................But THE BEST
cool.

In your PM, you asked "where should I implement the check condition". Typically do not PM daniweb members, keep questions to the threads. But I will answer anyway.

After each turn, you will need to check for certain things. For example, after a player attempts to move, you will need to verify that they are not moving into check. Again, I highly suggest that you implement the "isThisCheck" method so that it takes a square (i.e. the array index for a position on the board) as a parameter. That way, the check method can be versatile and it can "pretend" the king is at any particular square and decide whether it is check or not for the king to be there. So you need the isThisCheck method for two things

1. To check that player 1 isn't moving their king into check, or otherwise moving a piece that causes their king to be in check. (i.e. there was a bishop in the way so the king wasn't in check, but then you moved the bishop, putting the king in check... that is an illegal move).
2. To check whether or not player 1 puts player 2's king in check.
3. Because of this twofold need for the method, you may also want a parameter that is used to distinguish which player's king you're seeing if it is in check or not.

Anyway, you'll basically have a while loop that continuously waits for players to take their turn and only once one player has been put into checkmate (or once a stalemate occurs) will you exit that while loop. So the answer to your question is that the "check" method can be implemented in a separate method, but in the while loop, it will be called at the two points I mentioned above - just before player X makes their move (to make sure he's not putting himself in check) and just after player X moves (to see if he put the other player in check).

This article has been dead for over six months. Start a new discussion instead.