Apparently, in 1988, World Champion Gary Kasparov accidentally stalemated his opponent during a blitz tournament in Canada. Mr. Kasparov had a king, queen, and a bishop against Kiril Dimitrov Georgiev’s lone king. The problem Mr. Kasparov was facing was that of time running out. In his efforts to move quicker, he forced a stalemate when he had every advantage and reason to win!
I will probably never be as great as Mr. Kasparov, so I certainly mean no disrespect when bringing this up. My point being is that even world champions can stalemate. How does this apply to my JustChess app? Well, it seems to me that it is pretty important to clarify when the opponent can’t move, if they are in stalemate, or checkmate.
Fortunately, I figured out how to do just that.
The code wasn’t particularly grueling, it turned out the hard part was figuring out the order of play for pass and play verses playing against the computer. The time which you need to look for a checkmate or stalemate actually changes between those two. Here’s the important snippet, but as always, you can check it out on my GitHub.
moveOptions=""; if (!wTurn){ moveOptions= terminal("suggestMove,black"); } else { moveOptions= terminal("suggestMove,white"); } if (moveOptions.isEmpty()) { staleOrCheckMate(); } } // End clicked piece. public void staleOrCheckMate() { String status = terminal("checkmate"); if (status.equalsIgnoreCase("1")) { status = "checkmate!"; } else { status = "stalemate!"; } String turnIs = ""; if (wTurn) {turnIs="White is in ";} else {turnIs="Black is in ";} new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_info) .setTitle(turnIs + status) .setMessage( "Would you like to play a new game?") .setPositiveButton("View Board", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Do nothing. } }) .setNegativeButton("New Game", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // First you define it. Intent myintent = new Intent(MainActivity.this, IntroActivity.class); myintent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // Now you call it. startActivity(myintent); } }) .show(); // Make sure you show your popup or it wont work very well! }
The longest part of this project was actually testing it against the computer! It’s easy to checkmate it right now, but I found it was a pesky bugger to stalemate! Be sure to give it a try, my GitHub has all of the updates of the app already built for you to download and test out!
Linux – keep it simple.