spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / javachess / chap2 current pageTo page 2To page 3
[next]

JavaScript Chess Simulator: Chapter 2

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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+
 
N b1 - c3 +
[piece identifier] <start file>
<start rank>
<movetype> <end file>
<end rank>
[move comments]
e2xd3
 
  e2 x d3  
[piece identifier] <start file>
<start rank>
<movetype> <end file>
<end rank>
[move comments]

As you can see from the above examples, the important components for the move are elements two and four, which are the current location of the piece on the board as represented by its coordinates (e.g. b1 or e2) and the coordinates of where it is moving to. We'll have a look at the others later in this tutorial.

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.


home / programming / javascript / javachess / chap2 current pageTo page 2To page 3
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: June 10, 2003

URL: http://webreference.com/programming/javascript/javachess/chap2