JavaScript Chess Simulator: Part 2
JavaScript Chess Simulator: Chapter 2
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.
Recording Moves
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 :
Nb1-c3+
|
||||||||||
e2xd3
|
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 :
Function Name | Example | Usage |
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
str_theMove="e2-e4+";
// do we have a check in there ?
if (str_theMove.indexOf('+')>0)
{
check=true;
}
else
{
check=false;
}
}
// is it a capture
str_moveType=str_theMove.charAt(2);
if (str_moveType=='x')
{
capture=true;
}
else
{
capture=false;
}
// get the from location
str_fromLoc=str_theMove.substring(0,2);
// get the to location
str_toLoc=str_theMove.substr(3,2)
At the end of this the variables have the following values:
str_theMove="e2-e4"
str_moveType="x"
capture=false
check=true
str_fromLoc="e2"
str_toLoc="e4"
Additional information on these functions and their uses can be found as part of WebMonkey's Advanced Javascript Tutorial, especially Lesson two: pages two and three.
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
URL: https://webreference.com/programming/javascript/javachess/chap2