In the previous section, we looked at the key components and requirements of this application, we created a board populated with pieces and added an information dialog box to show the key information about the game. In this chapter, we will cover the basic movement of each piece, the capture of one piece by another, special moves such as Castling and stepping backwards though the move list.
For our application, we are going to record our moves using the Algebraic Notation format, which is probably the most commonly used format for recording - or scoring - a Chess game. Each line records an individual move (I don't intend to provide a detailed description on the system in this article, as plenty are available elsewhere on the Internet). Most moves are made up of the following components :
Playing with Strings
The Move List is an array of strings, and since this chapter focuses on interacting with this list (to produce a move on the graphical board), we need to work with strings. In this chapter, we'll focus on the following String object functions :
|charAt||str_thePiece=str_theMove.charAt(0)||This function gets the character at the specified location in the string.|
|indexOf||str_theMove.indexOf('++')||This function gets the characters in the specified portion of the string, the parameters are the start and end position in the string.|
|substring||str_thePiece=str_theMove.substring(0,1)||This function gets the characters in the specified portion of the string, the parameters are the start and end position in the string.|
|substr||str_moveTo=str_theMove.substr(3,2)||This function gets the characters in the specified portion of the string, the parameters are the start position and the number of characters to return.|
To illustrate the use of these functions we have the following example:
// get a move
// do we have a check in there ?
// is it a capture
// get the from location
// get the to location
At the end of this the variables have the following values:
In Chapter 3, I'll create a few tweaks to the application that we created in the previous two sections. Here, we'll focus on validating the Move Lists and we'll create some enhancements for the User Interface.
Created: March 27, 2003
Revised: June 10, 2003