Chess AI stalemate prevention and checkmate code

This may be useful in understanding.