Hello Everyone,
I am writing chess code in C++ using a main cpp file and functions only.. SO basically am done with everything except the checkmate condition... the only conditions for checkmate i have checked are the movements that king can make.. but there might be other possibilities of saving from the check like moving other pieces.. So any suggestions on how to implement :/... I would really appreciate any help..

Thanks in Advance

Recommended Answers

All 5 Replies

I think you'd have to first check to see if there are any possible moves your king can make... and then if the attacking piece is not a knight, you will probably have to check every other piece on the board to see if it can move anywhere inside the line of the attacking piece and the king. lastly, every piece should be tested to see if it can actually kill the attacking piece.

if the king cannot make a legal move, and the attacking piece is a knight, then the only possible scenario is a checkmate (unless the knight itself can be taken.) No block can occur.

When all three conditions occur; no legal move for king, no block can occur, and the attacking piece cannot be taken, only then can you conclude checkmate.

commented: Good suggestions. +15

There are four conditions that must be met for checkmate to occur:

1) The king is in check.
2) There is no move that the king can make that takes it out of check.
3) It is not possible to capture the attacking piece.
4) It is not possible to interpose a piece between the king and the attacking piece.

(3) and (4) are impossible if there is more than one attacking piece.
(4) is impossible if the attacking piece is a knight.

commented: Good suggestions +15

Thanks alot guys.. found it really helpful... :)

What about the following scenario?

1) Your king is not in check.
2) It's your move and any move you make places your king into check.

Is that checkmate?

What about the following scenario?

1) Your king is not in check.
2) It's your move and any move you make places your king into check.

Is that checkmate?

No. It's stalemate, and the game is drawn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.