kingsafe

One of the really important rules of playing chess, in fact, the rule upon which the entire game hinges, is king safety. You can’t make a move that puts your king in an unsafe position. This position of unsafe-ness is referred to as ‘check’. If you can’t find a way to get out of the position of being in ‘check’, then you are in ‘checkmate’.

Of course, if you are not in ‘check’ but any move would make you so, it is called a ‘stalemate’.

Thus, you can see the absolute need for a method or function to assess if the king is in check or not. So, I borrowed a method from my previous work in Java, the justchess engine. Of course it had to be edited for C++, but since both languages are object oriented programming languages, then it wasn’t too difficult.

You can check the commit at my GitLab for the full scoop, but essentially, my method assumes that the king is safe unless proven otherwise. This saves time, because at any point, if the king is proven unsafe, or in check, it will stop recursing through the board and other pieces and return a status of false: the king is not safe.

Linux – keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *