// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
var documentObjectModel = null;
var socket = null;
var internetClient = null;
var application = null;
var messageBox = null;
// INCLUDE THE NECESSARY OBJECTS.
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Development) == "undefined") Development = new Object ();
Development . MessageBox = function ()
{
this . window = null;
this . initializedPredicate = 0;
this . initiate = function ()
{
this . initializedPredicate = 0;
this . recycleWindow ();
return;
};
this . initialize = function ()
{
var html;
this . finalize ();
this . initializedPredicate = 1;
this . recycleWindow ();
html = "";
html = html + "\n";
html = html + "\n";
html = html + "
\n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " MessageBox\n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " Message Box\n";
html = html + "\n";
html = html + "
\n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + "
\n";
html = html + "\n";
html = html + " \n";
html = html + "\n";
html = html + " \n";
this . window = window . open ('', (new Date ()) . toString (), 'height = 300, menubar = 0, resizable = 1, scrollbars = 1, status = 0, toolbar = 0, width = 900');
this . window . document . writeln (html);
this . window . document . close ();
return;
};
this . finalize = function ()
{
this . initializedPredicate = 0;
this . recycleWindow ();
return;
}
this . recycleWindow = function ()
{
if (this . window != null)
this . window . close ();
this . window = null;
return;
};
this . getInitializedPredicate = function ()
{
if (this . initializedPredicate == 0 ) return (0);
if (this . window == null ) return (0);
if (typeof (this . window . existsPredicate) == "undefined" ) return (0);
return (1);
};
this . addType = function (type)
{
if (this . getInitializedPredicate () == 1)
if (typeof (this . window . addType) != "undefined")
this . window . addType (type);
return;
};
this . addMessage = function (type, text)
{
if (this . getInitializedPredicate () == 1)
if (typeof (this . window . addMessage) != "undefined")
this . window . addMessage (type, text);
return;
};
this . removeMessages = function ()
{
if (this . initializedPredicate == 1)
if (typeof (this . window . removeMessages) != "undefined")
this . window . removeMessages ();
return;
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Network) == "undefined") Network = new Object ();
Network . DocumentObjectModel = function ()
{
this . document = null;
this . getReleaseCode = function ()
{
return ("200608261504");
};
this . getSupportedPredicate = function ()
{
};
this . initialize = function ()
{
this . document = document;
};
this . appendNode = function (mainNode, appendageNode)
{
mainNode . appendChild (appendageNode);
};
this . clearNodes = function (node)
{
while (node . hasChildNodes ()) node . removeChild (node . firstChild);
};
this . createNode = function (tagName)
{
return (this . document . createElement (tagName));
};
this . createTextNode = function (text)
{
return (this . document . createTextNode (text));
};
this . getDocument = function ()
{
return (this . document);
};
this . getNode = function (nodeName)
{
return (this . document . getElementById (nodeName));
};
this . getValue = function (text)
{
return (parseInt (text));
};
this . getLayerX = function (cursorEvent)
{
if (typeof (cursorEvent) != "undefined")
if (typeof (cursorEvent . layerX) != "undefined")
return (cursorEvent . layerX);
if (typeof (event) != "undefined")
if (typeof (event . offsetX) != "undefined")
return (event . offsetX);
return (0);
};
this . getLayerY = function (cursorEvent)
{
if (typeof (cursorEvent) != "undefined")
if (typeof (cursorEvent . layerY) != "undefined")
return (cursorEvent . layerY);
if (typeof (event) != "undefined")
if (typeof (event . offsetY) != "undefined")
return (event . offsetY);
return (0);
};
this . getKeyCode = function (keyEvent)
{
var keyCode = 0;
if (navigator . userAgent . indexOf ("MSIE") == - 1)
keyCode = keyEvent . which;
else
keyCode = window . event . keyCode;
return (keyCode);
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Network) == "undefined") Network = new Object ();
Network . Socket = function ()
{
this . division = null;
this . script = null;
this . initialize = function ()
{
this . division = documentObjectModel . createNode ("div");
this . division . id = "NetworkSocket";
documentObjectModel . appendNode (documentObjectModel . getDocument () . body, this . division);
documentObjectModel . clearNodes (this . division);
this . script = null;
};
this . execute = function (command)
{
this . script = documentObjectModel . createNode ("script");
this . script . id = "NetworkSocket_Script";
this . script . type = "text/javascript";
this . script . src = command;
documentObjectModel . clearNodes (this . division);
documentObjectModel . appendNode (this . division, this . script);
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Network) == "undefined") Network = new Object ();
Network . InternetClient = function ()
{
this . initialize = function ()
{
};
this . getPageHeight = function ()
{
var height = 0;
if (typeof (window . innerHeight) == 'number')
height = window.innerHeight; // MOZILLA
else if (document . documentElement && document . documentElement . clientHeight)
height = document . documentElement . clientHeight; // INTERNET EXPLORER, "STANDARDS COMPLIANT MODE"
else if (document . body && document . body . clientHeight)
height = document . body . clientHeight; // INTERNET EXPLORER, "IE4 COMPATIBLE"
return (height);
};
this . getPageWidth = function ()
{
var width = 0;
if (typeof (window . innerWidth) == 'number')
width = window.innerWidth; // MOZILLA
else if (document . documentElement && document . documentElement . clientWidth)
width = document . documentElement . clientWidth; // INTERNET EXPLORER, "STANDARDS COMPLIANT MODE"
else if (document . body && document . body . clientWidth)
width = document . body . clientWidth; // INTERNET EXPLORER, "IE4 COMPATIBLE"
return (width);
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . Application = function ()
{
this . auxiliaryApplication = null;
this . state = null;
this . panel = null;
this . board = null;
this . pieces = null;
this . offMarkerNodes = null;
this . onMarkerNodes = null;
this . enPassantPosition = null;
this . ownCheckPredicate = 0;
this . opponentCheckPredicate = 0;
this . cache = null;
// CASTLE PREDICATES
// Castling also depends on the king not being in check.
this . ownKingMovedPredicate = 0; // the player must never have moved the king.
this . ownQueensideRookMovedPredicate = 0; // the player must never have moved the involved rook.
this . ownKingsideRookMovedPredicate = 0; // the player must never have moved the involved rook.
this . ownQueensidePathClosedPredicate = 0; // there can not be any pieces between the king and the rook.
this . ownKingsidePathClosedPredicate = 0; // there can not be any pieces between the king and the rook.
this . ownQueensideCheckPredicate = 0; // the player may not move if the castle puts the king in check.
this . ownKingsideCheckPredicate = 0; // the player may not move if the castle puts the king in check.
this . gameCode = null;
this . ownPin = null;
this . opponentPin = null;
// TIME-RELATED ENTITIES
this . connectTimeDelay = 0;
this . connectTimeLimit = 0;
this . connectTimeCount = 0;
this . receiveTimeDelay = 0;
this . receiveTimeLimit = 0;
this . receiveTimeCount = 0;
this . cachePictures = function ()
{
this . cache = new Array ();
this . cache [ 0] = new Image (); this . cache [ 0] . src = "/games/chess/Chess/pictures/marker.off.jpg";
this . cache [ 1] = new Image (); this . cache [ 1] . src = "/games/chess/Chess/pictures/marker.on.jpg";
this . cache [ 2] = new Image (); this . cache [ 2] . src = "/games/chess/Chess/pictures/piece.primary.pawn.png";
this . cache [ 3] = new Image (); this . cache [ 3] . src = "/games/chess/Chess/pictures/piece.primary.pawn.gif";
this . cache [ 4] = new Image (); this . cache [ 4] . src = "/games/chess/Chess/pictures/piece.secondary.pawn.png";
this . cache [ 5] = new Image (); this . cache [ 5] . src = "/games/chess/Chess/pictures/piece.secondary.pawn.gif";
this . cache [ 6] = new Image (); this . cache [ 6] . src = "/games/chess/Chess/pictures/piece.primary.rook.png";
this . cache [ 7] = new Image (); this . cache [ 7] . src = "/games/chess/Chess/pictures/piece.primary.rook.gif";
this . cache [ 8] = new Image (); this . cache [ 8] . src = "/games/chess/Chess/pictures/piece.secondary.rook.png";
this . cache [ 9] = new Image (); this . cache [ 9] . src = "/games/chess/Chess/pictures/piece.secondary.rook.gif";
this . cache [10] = new Image (); this . cache [ 0] . src = "/games/chess/Chess/pictures/piece.primary.knight.png";
this . cache [11] = new Image (); this . cache [ 1] . src = "/games/chess/Chess/pictures/piece.primary.knight.gif";
this . cache [12] = new Image (); this . cache [12] . src = "/games/chess/Chess/pictures/piece.secondary.knight.png";
this . cache [13] = new Image (); this . cache [13] . src = "/games/chess/Chess/pictures/piece.secondary.knight.gif";
this . cache [14] = new Image (); this . cache [14] . src = "/games/chess/Chess/pictures/piece.primary.bishop.png";
this . cache [15] = new Image (); this . cache [15] . src = "/games/chess/Chess/pictures/piece.primary.bishop.gif";
this . cache [16] = new Image (); this . cache [16] . src = "/games/chess/Chess/pictures/piece.secondary.bishop.png";
this . cache [17] = new Image (); this . cache [17] . src = "/games/chess/Chess/pictures/piece.secondary.bishop.gif";
this . cache [18] = new Image (); this . cache [18] . src = "/games/chess/Chess/pictures/piece.primary.queen.png";
this . cache [19] = new Image (); this . cache [19] . src = "/games/chess/Chess/pictures/piece.primary.queen.gif";
this . cache [20] = new Image (); this . cache [10] . src = "/games/chess/Chess/pictures/piece.secondary.queen.png";
this . cache [21] = new Image (); this . cache [11] . src = "/games/chess/Chess/pictures/piece.secondary.queen.gif";
this . cache [22] = new Image (); this . cache [22] . src = "/games/chess/Chess/pictures/piece.primary.king.png";
this . cache [23] = new Image (); this . cache [23] . src = "/games/chess/Chess/pictures/piece.primary.king.gif";
this . cache [24] = new Image (); this . cache [24] . src = "/games/chess/Chess/pictures/piece.secondary.king.png";
this . cache [25] = new Image (); this . cache [25] . src = "/games/chess/Chess/pictures/piece.secondary.king.gif";
}
this . initialize = function ()
{
this . auxiliaryApplication = new Object ();
this . ownCheckPredicate = 0;
this . opponentCheckPredicate = 0;
this . enPassantPosition = null;
this . opponentPreviousMove = "";
this . ownMoves = new Array ();
this . opponentMoves = new Array ();
this . state = new Chess . State ();
this . state . initialize ();
this . panel = new Chess . Panel ();
this . panel . initialize ();
this . board = new Chess . Board ();
this . board . initialize ();
this . connectTimeLimit = 180000;
this . connectTimeDelay = 4000;
this . connectTimeCount = 0;
this . receiveTimeLimit = 120000;
this . receiveTimeDelay = 500;
this . receiveTimeCount = 0;
this . pieces = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
// POPULATE THE PIECES ARRAY WITH PIECES.
for (var rowIndex = 0; rowIndex < 8; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < 8; columnIndex = columnIndex + 1)
{
this . pieces [rowIndex] [columnIndex] = new Chess . Piece ();
this . pieces [rowIndex] [columnIndex] . initialize ();
this . pieces [rowIndex] [columnIndex] . setOwnPredicate (rowIndex > 3 ? 1 : 0);
}
this . state . reset ();
this . render ();
// ATTACH THE GRAPHICS NODES.
this . offMarkerNodes = new Array ();
this . onMarkerNodes = new Array ();
this . offMarkerNodes [0] = documentObjectModel . createNode ("div");
this . offMarkerNodes [0] . style . position = "absolute";
this . offMarkerNodes [0] . style . top = "0px";
this . offMarkerNodes [0] . style . left = "0px";
this . offMarkerNodes [0] . style . height = "50px";
this . offMarkerNodes [0] . style . width = "50px";
this . offMarkerNodes [0] . style . zIndex = "1";
this . offMarkerNodes [0] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.off.jpg)";
this . offMarkerNodes [0] . style . display = "none";
this . offMarkerNodes [1] = documentObjectModel . createNode ("div");
this . offMarkerNodes [1] . style . position = "absolute";
this . offMarkerNodes [1] . style . top = "0px";
this . offMarkerNodes [1] . style . left = "0px";
this . offMarkerNodes [1] . style . height = "50px";
this . offMarkerNodes [1] . style . width = "50px";
this . offMarkerNodes [1] . style . zIndex = "1";
this . offMarkerNodes [1] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.off.jpg)";
this . offMarkerNodes [1] . style . display = "none";
this . offMarkerNodes [2] = documentObjectModel . createNode ("div");
this . offMarkerNodes [2] . style . position = "absolute";
this . offMarkerNodes [2] . style . top = "0px";
this . offMarkerNodes [2] . style . left = "0px";
this . offMarkerNodes [2] . style . height = "50px";
this . offMarkerNodes [2] . style . width = "50px";
this . offMarkerNodes [2] . style . zIndex = "1";
this . offMarkerNodes [2] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.off.jpg)";
this . offMarkerNodes [2] . style . display = "none";
this . offMarkerNodes [3] = documentObjectModel . createNode ("div");
this . offMarkerNodes [3] . style . position = "absolute";
this . offMarkerNodes [3] . style . top = "0px";
this . offMarkerNodes [3] . style . left = "0px";
this . offMarkerNodes [3] . style . height = "50px";
this . offMarkerNodes [3] . style . width = "50px";
this . offMarkerNodes [3] . style . zIndex = "1";
this . offMarkerNodes [3] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.off.jpg)";
this . offMarkerNodes [3] . style . display = "none";
this . onMarkerNodes [0] = documentObjectModel . createNode ("div");
this . onMarkerNodes [0] . style . position = "absolute";
this . onMarkerNodes [0] . style . top = "0px";
this . onMarkerNodes [0] . style . left = "0px";
this . onMarkerNodes [0] . style . height = "50px";
this . onMarkerNodes [0] . style . width = "50px";
this . onMarkerNodes [0] . style . zIndex = "1";
this . onMarkerNodes [0] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.on.jpg)";
this . onMarkerNodes [0] . style . display = "none";
this . onMarkerNodes [1] = documentObjectModel . createNode ("div");
this . onMarkerNodes [1] . style . position = "absolute";
this . onMarkerNodes [1] . style . top = "0px";
this . onMarkerNodes [1] . style . left = "0px";
this . onMarkerNodes [1] . style . height = "50px";
this . onMarkerNodes [1] . style . width = "50px";
this . onMarkerNodes [1] . style . zIndex = "1";
this . onMarkerNodes [1] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.on.jpg)";
this . onMarkerNodes [1] . style . display = "none";
this . onMarkerNodes [2] = documentObjectModel . createNode ("div");
this . onMarkerNodes [2] . style . position = "absolute";
this . onMarkerNodes [2] . style . top = "0px";
this . onMarkerNodes [2] . style . left = "0px";
this . onMarkerNodes [2] . style . height = "50px";
this . onMarkerNodes [2] . style . width = "50px";
this . onMarkerNodes [2] . style . zIndex = "1";
this . onMarkerNodes [2] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.on.jpg)";
this . onMarkerNodes [2] . style . display = "none";
this . onMarkerNodes [3] = documentObjectModel . createNode ("div");
this . onMarkerNodes [3] . style . position = "absolute";
this . onMarkerNodes [3] . style . top = "0px";
this . onMarkerNodes [3] . style . left = "0px";
this . onMarkerNodes [3] . style . height = "50px";
this . onMarkerNodes [3] . style . width = "50px";
this . onMarkerNodes [3] . style . zIndex = "1";
this . onMarkerNodes [3] . style . backgroundImage = "url(/games/chess/Chess/pictures/marker.on.jpg)";
this . onMarkerNodes [3] . style . display = "none";
this . containerNode = documentObjectModel . createNode ("div");
this . containerNode . style . position = "relative";
this . containerNode . style . top = "0px";
this . containerNode . style . left = "0px";
this . containerNode . style . height = (408) + "px";
this . containerNode . style . left = "50%";
this . containerNode . style . zIndex = "0";
if (navigator . userAgent . indexOf ("MSIE") != - 1)
{
this . containerNode . style . width = (this . panel . width + 25 + 1 + this . board . width + 4 + 4 + 4 + 2 + 2) + "px";
this . containerNode . style . marginLeft = "-" + ((this . panel . width + 25 + 1 + this . board . width + 4 + 4 + 4 + 2 + 2) / 2) + "px";
}
else
{
this . containerNode . style . width = (this . panel . width + 25 + 1 + this . board . width + 2) + "px";
this . containerNode . style . marginLeft = "-" + ((this . panel . width + 25 + 1 + this . board . width + 2) / 2) + "px";
}
hideWaitMessage ();
documentObjectModel . getNode ("game") . style . textAlign = "left";
documentObjectModel . appendNode (documentObjectModel . getNode ("game"), this . containerNode);
documentObjectModel . appendNode (this . containerNode, this . panel . getNode ());
documentObjectModel . appendNode (this . containerNode, this . board . getNode ());
documentObjectModel . appendNode (this . board . getNode (), this . offMarkerNodes [0]);
documentObjectModel . appendNode (this . board . getNode (), this . offMarkerNodes [1]);
documentObjectModel . appendNode (this . board . getNode (), this . offMarkerNodes [2]);
documentObjectModel . appendNode (this . board . getNode (), this . offMarkerNodes [3]);
documentObjectModel . appendNode (this . board . getNode (), this . onMarkerNodes [0]);
documentObjectModel . appendNode (this . board . getNode (), this . onMarkerNodes [1]);
documentObjectModel . appendNode (this . board . getNode (), this . onMarkerNodes [2]);
documentObjectModel . appendNode (this . board . getNode (), this . onMarkerNodes [3]);
for (var rowIndex = 0; rowIndex < 8; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < 8; columnIndex = columnIndex + 1)
documentObjectModel . appendNode (this . board . getNode (), this . pieces [rowIndex] [columnIndex] . getNode ());
this . ownPin = "8877";
this . panel . displayOpponentPlayerTypePrompt (this . setOpponentPlayerTypeDelegate);
this . cachePictures ();
};
this . finalize = function ()
{
this . disconnectEventCues ();
this . state . finalize ();
this . panel . finalize ();
this . board . finalize ();
this . state = null;
this . panel = null;
this . board = null;
this . pieces = null;
};
this . recordGame = function (winnerTitle)
{
var gameName;
var styleName;
var opponentType;
// Only the primary player is to record a game.
if (this . playerRank . toLowerCase () == "primary")
{
gameName = "Chess";
styleName = "International";
if (this . opponentPlayerType == "virtual")
{
opponentType = "virtual";
}
else
{
opponentType = "actual";
}
if (winnerTitle . toLowerCase () == "stalemate")
{
winnerTitle = "stalemate";
}
else if (winnerTitle . toLowerCase () == "primary")
{
winnerTitle = "primary";
}
else if (winnerTitle . toLowerCase () == "secondary")
{
winnerTitle = "secondary";
}
else
{
winnerTitle = "";
}
socket . execute ("/games/chess/Chess/php" + "/" + "recordGame" + "." + "p" + "h" + "p?refreshCode=" + this . getRefreshCode () + "&gameName=" + gameName + "&styleName=" + styleName + "&opponentType=" + opponentType + "&winnerTitle=" + winnerTitle);
}
return;
};
this . setOpponentPlayerTypeDelegate = function (opponentPlayerType)
{
if (opponentPlayerType == "virtual")
{
application . opponentPlayerType = opponentPlayerType;
application . panel . hideOpponentPlayerTypePrompt ();
application . activateKayla ();
}
else
{
application . opponentPlayerType = opponentPlayerType;
application . panel . hideOpponentPlayerTypePrompt ();
application . panel . displayPinPrompt (application . ownPin, application . setOpponentPinDelegate);
application . panel . opponentPinArea . focus ();
}
};
this . activateKayla = function ()
{
kayla = new Chess . Kayla ();
kayla . initialize ();
this . ownPin = "0000"; // fake pin -- allow the guest to move first
this . opponentPin = "0001"; // fake pin -- allow the guest to move first
this . playerRank = "primary"; // forced
this . panel . showPlayerMessage (this . playerRank);
this . startTurn ();
return;
};
this . setOpponentPinDelegate = function (pin)
{
application . setOpponentPin (pin);
};
this . setOpponentPin = function (pin)
{
if (this . connectingPredicate == 1) return; // return immediately if we have already been here.
this . connectingPredicate = 1;
this . opponentPin = pin;
this . panel . showPins (this . ownPin, this . opponentPin);
if (this . opponentPlayerType == "actual")
{
socket . execute ("/games/chess/Chess/php/connect.php?refreshcode=" + this . getRefreshCode () + "&ownPin=" + this . ownPin + "&opponentPin=" + this . opponentPin);
}
this . connectTimeCount = 0;
this . connect ();
};
this . connect = function ()
{
this . panel . setMessage ("low", "connecting");
if (this . connectTimeCount > this . connectTimeLimit)
{
this . connectTimeCount = 0;
this . panel . setMessage ("high", "continue?");
this . panel . showOkayButton ("application . connect ()");
}
else if (this . gameCode == null) // IF NOT CONNECTED, THEN FIND OUT IF A CONNECTION HAS BEEN MADE.
{
if (this . opponentPlayerType == "actual")
{
socket . execute ("/games/chess/Chess/php/getGameCode.php?refreshcode=" + this . getRefreshCode () + "&ownPin=" + this . ownPin + "&opponentPin=" + this . opponentPin);
}
setTimeout ("application . connect ()", this . connectTimeDelay);
this . connectTimeCount = this . connectTimeCount + this . connectTimeDelay;
}
else
{
this . panel . showPlayerMessage (this . playerRank);
if (this . playerRank == "primary")
{
this . startTurn ();
}
else
{
for (var rowIndex = 0; rowIndex < 8; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < 8; columnIndex = columnIndex + 1)
this . pieces [rowIndex] [columnIndex] . reverse ();
this . state . reverse ();
this . render ();
this . endTurn ();
}
}
};
this . startTurn = function ()
{
this . panel . setMessage ("medium", "your turn");
this . disconnectEventCues ();
this . ownMove = null;
this . evaluate (this);
this . trimMoves ();
if (this . ownKingMovedPredicate == 0)
if (this . ownQueensideRookMovedPredicate == 0)
if (this . ownQueensidePathClosedPredicate == 0)
if (this . ownQueensideCheckPredicate == 0)
if (this . ownCheckPredicate == 0)
this . panel . showCastlePrompt (this . castleDelegate, 1, 0);
if (this . ownKingMovedPredicate == 0)
if (this . ownKingsideRookMovedPredicate == 0)
if (this . ownKingsidePathClosedPredicate == 0)
if (this . ownKingsideCheckPredicate == 0)
if (this . ownCheckPredicate == 0)
this . panel . showCastlePrompt (this . castleDelegate, 0, 1);
if (this . ownKingMovedPredicate == 0)
if (this . ownQueensideRookMovedPredicate == 0)
if (this . ownKingsideRookMovedPredicate == 0)
if (this . ownQueensidePathClosedPredicate == 0)
if (this . ownKingsidePathClosedPredicate == 0)
if (this . ownQueensideCheckPredicate == 0)
if (this . ownKingsideCheckPredicate == 0)
if (this . ownCheckPredicate == 0)
this . panel . showCastlePrompt (this . castleDelegate, 1, 1);
if ((this . ownCheckPredicate == 1) && (this . opponentCheckPredicate == 1))
this . panel . showCheckMessage ("both");
else if (this . ownCheckPredicate == 1)
this . panel . showCheckMessage ("own");
else if (this . opponentCheckPredicate == 1)
this . panel . showCheckMessage ("opponent");
else
this . panel . hideCheckMessage ();
if ((this . legalMoves . length == 0) && (this . ownCheckPredicate == 1))
{
this . surrender ("checkmate");
}
else if ((this . legalMoves . length == 0) && (this . ownCheckPredicate == 0))
{
this . surrender ("stalemate");
}
else
{
this . connectEventCues ();
}
};
this . endTurn = function ()
{
this . panel . hideCaptures ();
this . panel . hideCastlePrompt ();
if (this . opponentPlayerType == "virtual")
{
this . panel . setMessage ("low", "machine's turn");
}
else
{
this . panel . setMessage ("low", "your friend's turn");
}
this . disconnectEventCues ();
this . opponentMove = null;
this . evaluate (this);
this . trimMoves ();
if ((this . ownCheckPredicate == 1) && (this . opponentCheckPredicate == 1))
this . panel . showCheckMessage ("both");
else if (this . ownCheckPredicate == 1)
this . panel . showCheckMessage ("own");
else if (this . opponentCheckPredicate == 1)
this . panel . showCheckMessage ("opponent");
else
this . panel . hideCheckMessage ();
this . receiveTimeCount = this . receiveTimeDelay * 2;
messageBox . addMessage ("Events", "Asking for the opponent's move.");
setTimeout ("application . getMove ()", (this . opponentPlayerType == "virtual") ? 1000 : this . receiveTimeDelay * 2);
return;
};
this . castleDelegate = function (sideName)
{
application . castle ("own", sideName);
application . render ();
application . setMove (sideName, 0, 0, 0, 0);
return;
};
this . castle = function (player, sideName)
{
if (this . playerRank == "primary")
{
if ((player == "own" ) && (sideName == "queen")) {this . state . move (7, 4, 7, 2); this . state . move (7, 0, 7, 3);}
if ((player == "own" ) && (sideName == "king" )) {this . state . move (7, 4, 7, 6); this . state . move (7, 7, 7, 5);}
if ((player == "own" ) && (sideName == "queen")) {this . ownKingMovedPredicate = 1;}
if ((player == "own" ) && (sideName == "king" )) {this . ownKingMovedPredicate = 1;}
if ((player == "opponent") && (sideName == "queen")) {this . state . move (0, 4, 0, 2); this . state . move (0, 0, 0, 3);}
if ((player == "opponent") && (sideName == "king" )) {this . state . move (0, 4, 0, 6); this . state . move (0, 7, 0, 5);}
if ((player == "opponent") && (sideName == "queen")) {this . showMarkers (0, 4, 0, 2); this . showAuxiliaryMarkers (0, 0, 0, 3);}
if ((player == "opponent") && (sideName == "king" )) {this . showMarkers (0, 4, 0, 6); this . showAuxiliaryMarkers (0, 7, 0, 5);}
}
else
{
if ((player == "own" ) && (sideName == "queen")) {this . state . move (7, 3, 7, 5); this . state . move (7, 7, 7, 4);}
if ((player == "own" ) && (sideName == "king" )) {this . state . move (7, 3, 7, 1); this . state . move (7, 0, 7, 2);}
if ((player == "own" ) && (sideName == "queen")) {this . ownKingMovedPredicate = 1;}
if ((player == "own" ) && (sideName == "king" )) {this . ownKingMovedPredicate = 1;}
if ((player == "opponent") && (sideName == "queen")) {this . state . move (0, 3, 0, 5); this . state . move (0, 7, 0, 4);}
if ((player == "opponent") && (sideName == "king" )) {this . state . move (0, 3, 0, 1); this . state . move (0, 0, 0, 2);}
if ((player == "opponent") && (sideName == "queen")) {this . showMarkers (0, 3, 0, 5); this . showAuxiliaryMarkers (0, 7, 0, 4);}
if ((player == "opponent") && (sideName == "king" )) {this . showMarkers (0, 3, 0, 1); this . showAuxiliaryMarkers (0, 0, 0, 2);}
}
application . render ();
return;
};
this . surrender = function (type)
{
this . panel . hideCheckMessage ();
this . hideMarkers ();
if (type == "stalemate")
{
this . recordGame ("stalemate");
this . panel . setMessage ("high", "stalemate");
}
else
{
this . recordGame ("secondary");
if (this . opponentPlayerType == "virtual")
{
this . panel . setMessage ("high", "The machine won. Thank you for playing.");
}
else
{
this . panel . setMessage ("high", "Your friend won. Thank you for playing.");
}
}
this . setMove (type, 0, 0, 0, 0);
this . disconnectEventCues ();
};
this . setMove = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex, promotionTypeText)
{
this . hideMarkers ();
if (fromRowIndex == "queen") this . ownMove = "castlequeenside" + this . ownPin;
else if (fromRowIndex == "king") this . ownMove = "castlekingside" + this . ownPin;
else if (fromRowIndex == "stalemate") this . ownMove = "stalemate" + this . ownPin;
else if (fromRowIndex == "checkmate") this . ownMove = "checkmate" + this . ownPin;
else this . ownMove = (fromRowIndex + "," + fromColumnIndex + "," + toRowIndex + "," + toColumnIndex + (promotionTypeText != null ? "," + promotionTypeText : ""));
messageBox . addMessage ("Events", "Sending the move: " + this . ownMove);
if (this . opponentPlayerType == "actual")
{
socket . execute ("/games/chess/Chess/php/setMove.php?refreshcode=" + this . getRefreshCode () + "&gameCode=" + this . gameCode + "&ownPin=" + this . ownPin + "&opponentPin=" + this . opponentPin + "&move=" + this . ownMove);
}
if (fromRowIndex != "stalemate")
if (fromRowIndex != "checkmate")
this . endTurn ();
};
this . getMove = function ()
{
if (this . opponentPlayerType == "virtual")
{
this . panel . setMessage ("low", "machine's turn");
}
else
{
this . panel . setMessage ("low", "your friend's turn");
}
if (this . gameExistsPredicate == 0)
{
this . panel . setMessage ("high", "this game is no longer active");
return;
}
if (this . receiveTimeCount > this . receiveTimeLimit)
{
this . receiveTimeCount = 0;
this . panel . setMessage ("high", "continue?");
this . panel . showOkayButton ("application . getMove ()");
}
else if (this . opponentMove == this . opponentPreviousMove)
{
// SENDMOVE FAILED, SO RESEND IT.
this . opponentMove = null;
if (this . opponentPlayerType == "actual")
{
socket . execute ("/games/chess/Chess/php/setMove.php?refreshcode=" + this . getRefreshCode () + "&gameCode=" + this . gameCode + "&ownPin=" + this . ownPin + "&opponentPin=" + this . opponentPin + "&move=" + this . ownMove);
}
setTimeout ("application . getMove ()", this . receiveTimeDelay * 2);
this . receiveTimeCount = this . receiveTimeCount + this . receiveTimeDelay * 2;
}
else if ((this . opponentMove == null) || (this . opponentMove == this . ownMove) || (this . opponentMove == ("castlequeenside" + this . ownPin)) || (this . opponentMove == ("castlekingside" + this . ownPin)))
{
if (this . opponentPlayerType == "actual")
{
socket . execute ("/games/chess/Chess/php/getMove.php?refreshcode=" + this . getRefreshCode () + "&gameCode=" + this . gameCode + "&ownPin=" + this . ownPin + "&opponentPin=" + this . opponentPin);
}
setTimeout ("application . getMove ()", this . receiveTimeDelay);
this . receiveTimeCount = this . receiveTimeCount + this . receiveTimeDelay;
}
else if (this . opponentMove == ("castlequeenside" + this . opponentPin))
{
this . opponentPreviousMove = this . opponentMove;
this . castle ("opponent", "queen")
this . startTurn ();
}
else if (this . opponentMove == ("castlekingside" + this . opponentPin))
{
this . opponentPreviousMove = this . opponentMove;
this . castle ("opponent", "king")
this . startTurn ();
}
else if (this . opponentMove == "stalemate" + this . opponentPin)
{
this . opponentPreviousMove = this . opponentMove;
this . panel . hideCheckMessage ();
this . panel . setMessage ("high", "stalemate");
this . recordGame ("stalemate");
}
else if (this . opponentMove == "checkmate" + this . opponentPin)
{
this . opponentPreviousMove = this . opponentMove;
this . panel . hideCheckMessage ();
this . panel . setMessage ("high", "congratulations!");
this . recordGame ("primary");
}
else
{
this . opponentPreviousMove = this . opponentMove;
var opponentMoveArray = this . opponentMove . split (",");
var fromRowIndex = parseInt (opponentMoveArray [0], 10);
var fromColumnIndex = parseInt (opponentMoveArray [1], 10);
var toRowIndex = parseInt (opponentMoveArray [2], 10);
var toColumnIndex = parseInt (opponentMoveArray [3], 10);
this . showMarkers (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex);
// THE CAPTURE OF A ROOK IS TREATED AS THE ROOK HAVING MOVED -- THIS IS USED FOR CASTLE PREDICATES.
if ((this . playerRank == "primary" ) && (toRowIndex == 7) && (toColumnIndex == 0)) this . ownQueensideRookMovedPredicate = 1;
if ((this . playerRank == "primary" ) && (toRowIndex == 7) && (toColumnIndex == 7)) this . ownKingsideRookMovedPredicate = 1;
if ((this . playerRank == "secondary") && (toRowIndex == 7) && (toColumnIndex == 0)) this . ownKingsideRookMovedPredicate = 1;
if ((this . playerRank == "secondary") && (toRowIndex == 7) && (toColumnIndex == 7)) this . ownQueensideRookMovedPredicate = 1;
// DETECT A PAWN-PROMOTION.
if ((this . state . pieceCodes [fromRowIndex] [fromColumnIndex] == this . state . pawnCode) && (toRowIndex == 7))
{
if (opponentMoveArray [4] == "rook" ) this . state . pieceCodes [fromRowIndex] [fromColumnIndex] = this . state . rookCode;
if (opponentMoveArray [4] == "knight") this . state . pieceCodes [fromRowIndex] [fromColumnIndex] = this . state . knightCode;
if (opponentMoveArray [4] == "bishop") this . state . pieceCodes [fromRowIndex] [fromColumnIndex] = this . state . bishopCode;
if (opponentMoveArray [4] == "queen" ) this . state . pieceCodes [fromRowIndex] [fromColumnIndex] = this . state . queenCode;
}
// DETECT A TWO-SQUARE MOVE BY A PAWN STARTING IN HOME-ROW ... THIS NEEDS TO BE RECORDED TO ALLOW ONE TO USE 'EN PASSANT'.
this . enPassantPosition = null;
if (fromRowIndex == 1)
if (toRowIndex == 3)
if (this . state . pieceCodes [fromRowIndex] [fromColumnIndex] == this . state . pawnCode)
this . enPassantPosition = new Array (toRowIndex, toColumnIndex);
// IF THE MOVE IS EN-PASSANT, THEN REMOVE THE CAPTURED PIECE.
if (fromColumnIndex != toColumnIndex)
if (this . state . pieceCodes [fromRowIndex] [fromColumnIndex] == this . state . pawnCode)
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . emptyCode)
{
this . state . pieceCodes [toRowIndex - 1] [toColumnIndex] = this . state . emptyCode;
this . panel . showCaptures (new Array ("Pawn"));
}
// LET THE PLAYER KNOW WHICH PIECE WAS CAPTURED, IF ANY.
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . pawnCode ) this . panel . showCaptures (new Array ("Pawn"));
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . rookCode ) this . panel . showCaptures (new Array ("Rook"));
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . knightCode) this . panel . showCaptures (new Array ("Knight"));
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . bishopCode) this . panel . showCaptures (new Array ("Bishop"));
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . queenCode ) this . panel . showCaptures (new Array ("Queen"));
if (this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . kingCode ) this . panel . showCaptures (new Array ("King")); // This should never happen.
// DUPLICATE THE OPPONENT'S MOVE ON THIS BOARD.
this . state . move (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex, this . enPassantPosition);
this . render ();
// START THE PLAYER'S NEXT TURN.
this . startTurn ();
}
return;
};
this . getTranslatedMove = function (move)
{
if (move == ("castlequeenside" + this . ownPin)) return (move);
if (move == ("castlekingside" + this . ownPin)) return (move);
if (move == ("stalemate" + this . ownPin)) return (move);
if (move == ("checkmate" + this . ownPin)) return (move);
if (move == ("castlequeenside" + this . opponentPin)) return (move);
if (move == ("castlekingside" + this . opponentPin)) return (move);
if (move == ("stalemate" + this . opponentPin)) return (move);
if (move == ("checkmate" + this . opponentPin)) return (move);
var moveArray;
moveArray = move . split (",");
move = "";
move = move + (this . board . rowCount - 1 - parseInt (moveArray [0], 10));
move = move + ",";
move = move + (this . board . columnCount - 1 - parseInt (moveArray [1], 10));
move = move + ",";
move = move + (this . board . rowCount - 1 - parseInt (moveArray [2], 10));
move = move + ",";
move = move + (this . board . columnCount - 1 - parseInt (moveArray [3], 10));
if (moveArray . length == 5)
move = move + "," + moveArray [4];
return (move);
};
this . trimMoves = function ()
{
this . legalMoves = new Array ();
this . illegalMoves = new Array ();
this . auxiliaryApplication . state = new Chess . State ();
this . auxiliaryApplication . state . initialize ();
for (var index = 0; index < this . ownMoves . length; index = index + 1)
{
var fromRowIndex;
var fromColumnIndex;
var toRowIndex;
var toColumnIndex;
this . auxiliaryApplication . state . copy (this . state);
fromRowIndex = (this . ownMoves [index] . split (",")) [0];
fromColumnIndex = (this . ownMoves [index] . split (",")) [1];
toRowIndex = (this . ownMoves [index] . split (",")) [2];
toColumnIndex = (this . ownMoves [index] . split (",")) [3];
this . auxiliaryApplication . state . move (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex);
this . evaluate (this . auxiliaryApplication);
if (this . auxiliaryApplication . ownCheckPredicate == 1)
this . illegalMoves . push (this . ownMoves [index]);
else
this . legalMoves . push (this . ownMoves [index]);
}
};
this . trimOpponentMoves = function ()
{
this . legalOpponentMoves = new Array ();
this . illegalOpponentMoves = new Array ();
this . auxiliaryApplication . state = new Chess . State ();
this . auxiliaryApplication . state . initialize ();
for (var index = 0; index < this . opponentMoves . length; index = index + 1)
{
var fromRowIndex;
var fromColumnIndex;
var toRowIndex;
var toColumnIndex;
this . auxiliaryApplication . state . copy (this . state);
fromRowIndex = (this . opponentMoves [index] . split (",")) [0];
fromColumnIndex = (this . opponentMoves [index] . split (",")) [1];
toRowIndex = (this . opponentMoves [index] . split (",")) [2];
toColumnIndex = (this . opponentMoves [index] . split (",")) [3];
this . auxiliaryApplication . state . move (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex);
this . evaluate (this . auxiliaryApplication);
if (this . auxiliaryApplication . opponentCheckPredicate == 1)
this . illegalOpponentMoves . push (this . opponentMoves [index]);
else
this . legalOpponentMoves . push (this . opponentMoves [index]);
}
};
this . evaluate = function (effectiveApplication)
{
effectiveApplication . ownMoves = new Array ();
effectiveApplication . opponentMoves = new Array ();
for (var rowIndex = 0; rowIndex < this . board . rowCount; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < this . board . columnCount; columnIndex = columnIndex + 1)
{
// ANALYZE THE OWN-PAWN MOVES.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . pawnCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
// CHECK FOR EN PASSANT.
if (this . enPassantPosition != null)
if (rowIndex == this . enPassantPosition [0])
if (columnIndex == (this . enPassantPosition [1] - 1))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex + 1));
if (this . enPassantPosition != null)
if (rowIndex == this . enPassantPosition [0])
if (columnIndex == (this . enPassantPosition [1] + 1))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex - 1));
// CHECK FOR DOUBLE-MOVE IF THE PAWN IS ON ITS HOME ROW.
if ((rowIndex == 6) && (effectiveApplication . state . pieceCodes [rowIndex - 1] [columnIndex] == effectiveApplication . state . emptyCode) && (effectiveApplication . state . pieceCodes [rowIndex - 2] [columnIndex] == effectiveApplication . state . emptyCode))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 2) + "," + (columnIndex));
// AS LONG AS THE PAWN IS NOT ON THE FARTHEST ROW FROM HOME ROW (WHICH SHOULD NEVER HAPPEN ANYWAY DUE TO PAWN-PROMOTION) THE PAWN CAN BE MOVED FORWARD TO ONE EMPTY SPACE.
if ((rowIndex > 0) && (effectiveApplication . state . pieceCodes [rowIndex - 1] [columnIndex] == effectiveApplication . state . emptyCode))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex));
// THE PAWN CAN ONLY CAPTURE DIAGONALLY.
if ((rowIndex > 0) && (columnIndex > 0) && (effectiveApplication . state . pieceCodes [rowIndex - 1] [columnIndex - 1] != effectiveApplication . state . emptyCode) && (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex - 1] == 0))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex - 1));
if ((rowIndex > 0) && (columnIndex < (effectiveApplication . state . columnCount - 1)) && (effectiveApplication . state . pieceCodes [rowIndex - 1] [columnIndex + 1] != effectiveApplication . state . emptyCode) && (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex + 1] == 0))
effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex + 1));
}
// ANALYZE THE OPPONENT-PAWN MOVES.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . pawnCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
// NO NEED TO CHECK FOR EN PASSANT HERE ... SINCE THAT POSSIBILITY DEPENDS ON ONE'S OWN MOVE.
// CHECK FOR DOUBLE-MOVE IF THE PAWN IS ON ITS HOME ROW.
if ((rowIndex == 1) && (effectiveApplication . state . pieceCodes [rowIndex + 1] [columnIndex] == effectiveApplication . state . emptyCode) && (effectiveApplication . state . pieceCodes [rowIndex + 2] [columnIndex] == effectiveApplication . state . emptyCode))
effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 2) + "," + (columnIndex));
// AS LONG AS THE PAWN IS NOT ON THE FARTHEST ROW FROM HOME ROW (WHICH SHOULD NEVER HAPPEN ANYWAY DUE TO PAWN-PROMOTION) THE PAWN CAN BE MOVED FORWARD TO ONE EMPTY SPACE.
if ((rowIndex < (effectiveApplication . state . rowCount - 1)) && (effectiveApplication . state . pieceCodes [rowIndex + 1] [columnIndex] == effectiveApplication . state . emptyCode))
effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex));
// THE PAWN CAN ONLY CAPTURE DIAGONALLY.
if ((rowIndex < (effectiveApplication . state . rowCount - 1)) && (columnIndex > 0) && (effectiveApplication . state . pieceCodes [rowIndex + 1] [columnIndex - 1] != effectiveApplication . state . emptyCode) && (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex - 1] == 1))
effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex - 1));
if ((rowIndex < (effectiveApplication . state . rowCount - 1)) && (columnIndex < (effectiveApplication . state . columnCount - 1)) && (effectiveApplication . state . pieceCodes [rowIndex + 1] [columnIndex + 1] != effectiveApplication . state . emptyCode) && (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex + 1] == 1))
effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex + 1));
}
// ANALYZE THE OWN-KNIGHT MOVES.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . knightCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
// START AT THE TOP-TOP-LEFT AND ROTATE CLOCKWISE.
if (((rowIndex - 2) >= 0) && ((rowIndex - 2) <= 7) && ((columnIndex - 1) >= 0) && ((columnIndex - 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 2] [columnIndex - 1] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 2) + "," + (columnIndex - 1));
if (((rowIndex - 2) >= 0) && ((rowIndex - 2) <= 7) && ((columnIndex + 1) >= 0) && ((columnIndex + 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 2] [columnIndex + 1] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 2) + "," + (columnIndex + 1));
if (((rowIndex - 1) >= 0) && ((rowIndex - 1) <= 7) && ((columnIndex + 2) >= 0) && ((columnIndex + 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex + 2] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex + 2));
if (((rowIndex + 1) >= 0) && ((rowIndex + 1) <= 7) && ((columnIndex + 2) >= 0) && ((columnIndex + 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex + 2] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex + 2));
if (((rowIndex + 2) >= 0) && ((rowIndex + 2) <= 7) && ((columnIndex + 1) >= 0) && ((columnIndex + 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 2] [columnIndex + 1] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 2) + "," + (columnIndex + 1));
if (((rowIndex + 2) >= 0) && ((rowIndex + 2) <= 7) && ((columnIndex - 1) >= 0) && ((columnIndex - 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 2] [columnIndex - 1] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 2) + "," + (columnIndex - 1));
if (((rowIndex + 1) >= 0) && ((rowIndex + 1) <= 7) && ((columnIndex - 2) >= 0) && ((columnIndex - 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex - 2] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex - 2));
if (((rowIndex - 1) >= 0) && ((rowIndex - 1) <= 7) && ((columnIndex - 2) >= 0) && ((columnIndex - 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex - 2] == 0) effectiveApplication . ownMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex - 2));
}
// ANALYZE THE OPPONENT-KNIGHT MOVES.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . knightCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
// START AT THE TOP-TOP-LEFT AND ROTATE CLOCKWISE.
if (((rowIndex - 2) >= 0) && ((rowIndex - 2) <= 7) && ((columnIndex - 1) >= 0) && ((columnIndex - 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 2] [columnIndex - 1] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 2) + "," + (columnIndex - 1));
if (((rowIndex - 2) >= 0) && ((rowIndex - 2) <= 7) && ((columnIndex + 1) >= 0) && ((columnIndex + 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 2] [columnIndex + 1] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 2) + "," + (columnIndex + 1));
if (((rowIndex - 1) >= 0) && ((rowIndex - 1) <= 7) && ((columnIndex + 2) >= 0) && ((columnIndex + 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex + 2] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex + 2));
if (((rowIndex + 1) >= 0) && ((rowIndex + 1) <= 7) && ((columnIndex + 2) >= 0) && ((columnIndex + 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex + 2] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex + 2));
if (((rowIndex + 2) >= 0) && ((rowIndex + 2) <= 7) && ((columnIndex + 1) >= 0) && ((columnIndex + 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 2] [columnIndex + 1] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 2) + "," + (columnIndex + 1));
if (((rowIndex + 2) >= 0) && ((rowIndex + 2) <= 7) && ((columnIndex - 1) >= 0) && ((columnIndex - 1) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 2] [columnIndex - 1] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 2) + "," + (columnIndex - 1));
if (((rowIndex + 1) >= 0) && ((rowIndex + 1) <= 7) && ((columnIndex - 2) >= 0) && ((columnIndex - 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex + 1] [columnIndex - 2] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex + 1) + "," + (columnIndex - 2));
if (((rowIndex - 1) >= 0) && ((rowIndex - 1) <= 7) && ((columnIndex - 2) >= 0) && ((columnIndex - 2) <= 7)) if (effectiveApplication . state . ownershipPredicates [rowIndex - 1] [columnIndex - 2] == 1) effectiveApplication . opponentMoves . push ((rowIndex) + "," + (columnIndex) + "," + (rowIndex - 1) + "," + (columnIndex - 2));
}
// ANALYZE THE OWN-BISHOP MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . bishopCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, + 1, 8);
}
// ANALYZE THE OPPONENT-BISHOP MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . bishopCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, + 1, 8);
}
// ANALYZE THE OWN-ROOK MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . rookCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, + 1, 8);
}
// ANALYZE THE OPPONENT-ROOK MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . rookCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, + 1, 8);
}
// ANALYZE THE OWN-QUEEN MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . queenCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, + 1, 8);
}
// ANALYZE THE OPPONENT-QUEEN MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . queenCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, + 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, 0, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, - 1, 8);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, + 1, 8);
}
// ANALYZE THE OWN-KING MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . kingCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
{
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, + 1, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, + 1, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, - 1, 0, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, + 1, 0, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . ownMoves, rowIndex, columnIndex, 0, + 1, 1);
}
// ANALYZE THE OPPONENT-KING MOVES.
// SCAN EACH OF THE FOUR DIAGONALS ... STOP WHEN THE EDGE OF THE BOARD OR ANOTHER PIECE IS REACHED.
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . kingCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
{
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, + 1, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, + 1, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, - 1, 0, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, + 1, 0, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, - 1, 1);
this . scan (effectiveApplication, effectiveApplication . opponentMoves, rowIndex, columnIndex, 0, + 1, 1);
}
}
// SCAN FOR CHECK.
effectiveApplication . ownCheckPredicate = 0;
effectiveApplication . opponentCheckPredicate = 0;
effectiveApplication . ownQueensideCheckPredicate = 0;
effectiveApplication . ownKingsideCheckPredicate = 0;
for (var index = 0; index < effectiveApplication . opponentMoves . length; index = index + 1)
{
var rowIndex;
var columnIndex;
rowIndex = (effectiveApplication . opponentMoves [index] . split (",")) [2];
columnIndex = (effectiveApplication . opponentMoves [index] . split (",")) [3];
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . kingCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 1)
effectiveApplication . ownCheckPredicate = 1;
// if ((this . playerRank == "primary") && (rowIndex == 7) && (columnIndex == 1)) effectiveApplication . ownQueensideCheckPredicate = 1;
if ((this . playerRank == "primary") && (rowIndex == 7) && (columnIndex == 2)) effectiveApplication . ownQueensideCheckPredicate = 1;
if ((this . playerRank == "primary") && (rowIndex == 7) && (columnIndex == 3)) effectiveApplication . ownQueensideCheckPredicate = 1;
if ((this . playerRank == "primary") && (rowIndex == 7) && (columnIndex == 5)) effectiveApplication . ownKingsideCheckPredicate = 1;
if ((this . playerRank == "primary") && (rowIndex == 7) && (columnIndex == 6)) effectiveApplication . ownKingsideCheckPredicate = 1;
// if ((this . playerRank == "secondary") && (rowIndex == 7) && (columnIndex == 1)) effectiveApplication . ownKingsideCheckPredicate = 1;
if ((this . playerRank == "secondary") && (rowIndex == 7) && (columnIndex == 2)) effectiveApplication . ownKingsideCheckPredicate = 1;
if ((this . playerRank == "secondary") && (rowIndex == 7) && (columnIndex == 3)) effectiveApplication . ownKingsideCheckPredicate = 1;
if ((this . playerRank == "secondary") && (rowIndex == 7) && (columnIndex == 5)) effectiveApplication . ownQueensideCheckPredicate = 1;
if ((this . playerRank == "secondary") && (rowIndex == 7) && (columnIndex == 6)) effectiveApplication . ownQueensideCheckPredicate = 1;
}
for (var index = 0; index < effectiveApplication . ownMoves . length; index = index + 1)
{
var rowIndex;
var columnIndex;
rowIndex = (effectiveApplication . ownMoves [index] . split (",")) [2];
columnIndex = (effectiveApplication . ownMoves [index] . split (",")) [3];
if (effectiveApplication . state . pieceCodes [rowIndex] [columnIndex] == effectiveApplication . state . kingCode)
if (effectiveApplication . state . ownershipPredicates [rowIndex] [columnIndex] == 0)
effectiveApplication . opponentCheckPredicate = 1;
}
effectiveApplication . ownKingsidePathClosedPredicate = 1;
effectiveApplication . ownQueensidePathClosedPredicate = 1;
if (this . playerRank == "primary")
if (effectiveApplication . state . pieceCodes [7] [1] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [2] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [3] == effectiveApplication . state . emptyCode)
effectiveApplication . ownQueensidePathClosedPredicate = 0;
if (this . playerRank == "primary")
if (effectiveApplication . state . pieceCodes [7] [5] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [6] == effectiveApplication . state . emptyCode)
effectiveApplication . ownKingsidePathClosedPredicate = 0;
if (this . playerRank == "secondary")
if (effectiveApplication . state . pieceCodes [7] [1] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [2] == effectiveApplication . state . emptyCode)
effectiveApplication . ownKingsidePathClosedPredicate = 0;
if (this . playerRank == "secondary")
if (effectiveApplication . state . pieceCodes [7] [4] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [5] == effectiveApplication . state . emptyCode)
if (effectiveApplication . state . pieceCodes [7] [6] == effectiveApplication . state . emptyCode)
effectiveApplication . ownQueensidePathClosedPredicate = 0;
};
this . scan = function (effectiveApplication, moves, fromRowIndex, fromColumnIndex, rowScalar, columnScalar, distance)
{
var done;
done = 0;
for (var distanceIndex = 1; distanceIndex < (distance + 1); distanceIndex = distanceIndex + 1)
{
var toRowIndex = fromRowIndex + distanceIndex * rowScalar;
var toColumnIndex = fromColumnIndex + distanceIndex * columnScalar;
if (done == 0)
if (toRowIndex >= 0)
if (toRowIndex <= 7)
if (toColumnIndex >= 0)
if (toColumnIndex <= 7)
{
if ((toRowIndex >= 0) && (toRowIndex <= 7) && (toColumnIndex >= 0) && (toColumnIndex <= 7))
{
if (effectiveApplication . state . pieceCodes [toRowIndex] [toColumnIndex] == effectiveApplication . state . emptyCode)
{
moves . push ((fromRowIndex) + "," + (fromColumnIndex) + "," + (toRowIndex) + "," + (toColumnIndex));
}
else
{
if (effectiveApplication . state . ownershipPredicates [toRowIndex] [toColumnIndex] == (moves == effectiveApplication . ownMoves ? 0 : 1))
moves . push ((fromRowIndex) + "," + (fromColumnIndex) + "," + (toRowIndex) + "," + (toColumnIndex));
done = 1;
}
}
else
{
done = 1;
}
}
}
};
this . capture = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex)
{
var matchCode = this . state . possession [fromRowIndex] [fromColumnIndex];
var rowStep = 0;
var columnStep = 0;
if (fromRowIndex < toRowIndex) rowStep = 1;
if (fromRowIndex > toRowIndex) rowStep = - 1;
if (fromColumnIndex < toColumnIndex) columnStep = 1;
if (fromColumnIndex > toColumnIndex) columnStep = - 1;
var rowIndex = fromRowIndex + rowStep;
var columnIndex = fromColumnIndex + columnStep;
var index = 0;
var initialValue = 0;
var rowLimit = fromRowIndex < toRowIndex ? toRowIndex - fromRowIndex - 1 : fromRowIndex - toRowIndex - 1;
var columnLimit = fromColumnIndex < toColumnIndex ? toColumnIndex - fromColumnIndex - 1 : fromColumnIndex - toColumnIndex - 1;
var limit = rowLimit > columnLimit ? rowLimit : columnLimit;
for (index = initialValue; index < limit; index = index + 1)
{
this . state . possession [rowIndex] [columnIndex] = matchCode;
rowIndex = rowIndex + rowStep;
columnIndex = columnIndex + columnStep;
}
};
this . render = function ()
{
for (var rowIndex = 0; rowIndex < 8; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < 8; columnIndex = columnIndex + 1)
{
var piece;
piece = this . pieces [rowIndex] [columnIndex];
piece . getNode () . style . display = "none";
if (this . state . pieceCodes [rowIndex] [columnIndex] != this . state . emptyCode)
{
var typeCode;
var typeText;
typeCode = this . state . pieceCodes [rowIndex] [columnIndex];
if (typeCode == this . state . pawnCode ) typeText = "pawn";
if (typeCode == this . state . rookCode ) typeText = "rook";
if (typeCode == this . state . knightCode ) typeText = "knight";
if (typeCode == this . state . bishopCode ) typeText = "bishop";
if (typeCode == this . state . queenCode ) typeText = "queen";
if (typeCode == this . state . kingCode ) typeText = "king";
piece . setType (typeText);
piece . getNode () . style . display = "block";
piece . setCoordinates (rowIndex * piece . getHeight () + rowIndex, columnIndex * piece . getWidth () + columnIndex);
piece . setOwnPredicate (this . state . ownershipPredicates [rowIndex] [columnIndex]);
}
}
};
this . connectEventCues = function ()
{
this . board . glassNode . that = this;
this . board . glassNode . onmousedown = this . cursorDownDelegate;
this . board . glassNode . onmousemove = this . cursorMoveDelegate;
// this . board . glassNode . onmouseup = this . cursorUpDelegate;
}
this . disconnectEventCues = function ()
{
this . board . glassNode . onmousedown = null;
this . board . glassNode . onmousemove = null;
this . board . glassNode . onmouseup = null;
}
this . cursorDownDelegate = function (cursorEvent) {application . cursorDown (cursorEvent);};
this . cursorMoveDelegate = function (cursorEvent) {application . cursorMove (cursorEvent);};
this . cursorUpDelegate = function (cursorEvent) {application . cursorUp (cursorEvent);};
this . cursorDown = function (cursorEvent)
{
var fromRowIndex = Math . floor (documentObjectModel . getLayerY (cursorEvent) / 51);
var fromColumnIndex = Math . floor (documentObjectModel . getLayerX (cursorEvent) / 51);
if (fromRowIndex >= 0)
if (fromRowIndex < this . board . rowCount)
if (fromColumnIndex >= 0)
if (fromColumnIndex < this . board . columnCount)
{
var targetedCheckerPiece = this . pieces [fromRowIndex] [fromColumnIndex];
if (targetedCheckerPiece != null)
if (targetedCheckerPiece . getOwnPredicate () == 1)
if ((this . selectedCheckerPiece == null) || (this . selectedCheckerPiece == targetedCheckerPiece))
{
this . selectedCheckerPiece = targetedCheckerPiece;
this . selectedCheckerPiece . setCoordinates (documentObjectModel . getLayerY (cursorEvent) - Math . floor (this . selectedCheckerPiece . getHeight () / 2), documentObjectModel . getLayerX (cursorEvent) - Math . floor (this . selectedCheckerPiece . getWidth () / 2));
this . selectedCheckerPiece . elevate ();
this . selectedCheckerPiece . fromRowIndex = fromRowIndex;
this . selectedCheckerPiece . fromColumnIndex = fromColumnIndex;
this . board . glassNode . onmousemove = this . cursorMoveDelegate;
this . board . glassNode . onmouseup = this . cursorUpDelegate;
}
}
cursorEvent . stopPropagation ();
cursorEvent . preventDefault ();
return;
};
this . cursorMove = function (cursorEvent)
{
if (this . selectedCheckerPiece)
this . selectedCheckerPiece . setCoordinates (documentObjectModel . getLayerY (cursorEvent) - Math . floor (this . selectedCheckerPiece . getHeight () / 2), documentObjectModel . getLayerX (cursorEvent) - Math . floor (this . selectedCheckerPiece . getWidth () / 2));
cursorEvent . stopPropagation ();
cursorEvent . preventDefault ();
return;
};
this . cursorUp = function (cursorEvent)
{
this . board . glassNode . onmousemove = null;
if (this . selectedCheckerPiece)
{
this . selectedCheckerPiece . ground ();
var toRowIndex = Math . floor (documentObjectModel . getLayerY (cursorEvent) / 51);
var toColumnIndex = Math . floor (documentObjectModel . getLayerX (cursorEvent) / 51);
var fromRowIndex = this . selectedCheckerPiece . fromRowIndex;
var fromColumnIndex = this . selectedCheckerPiece . fromColumnIndex;
if (toRowIndex >= 0)
if (toRowIndex < this . board . rowCount)
if (toColumnIndex >= 0)
if (toColumnIndex < this . board . columnCount)
{
if (this . getLegalMovePredicate (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex))
{
if (this . state . pieceCodes [fromRowIndex] [fromColumnIndex] == this . state . kingCode)
this . ownKingMovedPredicate = 1;
if ((fromRowIndex == 7) && (fromColumnIndex == 0) && (this . playerRank == "primary"))
this . ownQueensideRookMovedPredicate = 1;
if ((fromRowIndex == 7) && (fromColumnIndex == 0) && (this . playerRank == "secondary"))
this . ownKingsideRookMovedPredicate = 1;
if ((fromRowIndex == 7) && (fromColumnIndex == 7) && (this . playerRank == "primary"))
this . ownKingsideRookMovedPredicate = 1;
if ((fromRowIndex == 7) && (fromColumnIndex == 7) && (this . playerRank == "secondary"))
this . ownQueensideRookMovedPredicate = 1;
this . state . move (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex, this . enPassantPosition);
this . render ();
if ((this . state . pieceCodes [toRowIndex] [toColumnIndex] == this . state . pawnCode) && (toRowIndex == 0))
{
this . promotionMove = new Array (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex);
this . panel . showPromotionPrompt (this . promoteDelegate);
}
else
{
this . setMove (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex, null);
}
}
else if (this . getIllegalMovePredicate (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex))
{
this . panel . setMessage ("high", "that move is not allowed during this turn");
this . board . glassNode . onmousemove = null;
this . board . glassNode . onmouseup = null;
}
else if ((fromRowIndex == toRowIndex) && (fromColumnIndex == toColumnIndex))
{
this . board . glassNode . onmousemove = null;
this . board . glassNode . onmouseup = null;
}
else
{
this . panel . setMessage ("high", "that move is not allowed");
this . board . glassNode . onmousemove = null;
this . board . glassNode . onmouseup = null;
}
this . selectedCheckerPiece = null;
}
}
this . render ();
cursorEvent . stopPropagation ();
cursorEvent . preventDefault ();
return;
};
this . promoteDelegate = function (typeText)
{
application . promote (typeText);
};
this . promote = function (typeText)
{
if (typeText == "rook" ) this . state . pieceCodes [this . promotionMove [2]] [this . promotionMove [3]] = this . state . rookCode;
if (typeText == "knight") this . state . pieceCodes [this . promotionMove [2]] [this . promotionMove [3]] = this . state . knightCode;
if (typeText == "bishop") this . state . pieceCodes [this . promotionMove [2]] [this . promotionMove [3]] = this . state . bishopCode;
if (typeText == "queen" ) this . state . pieceCodes [this . promotionMove [2]] [this . promotionMove [3]] = this . state . queenCode;
this . render ();
this . setMove (this . promotionMove [0], this . promotionMove [1], this . promotionMove [2], this . promotionMove [3], typeText);
};
this . getLegalMovePredicate = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex)
{
for (var index = 0; index < this . legalMoves . length; index = index + 1)
if (this . legalMoves [index] == (fromRowIndex + "," + fromColumnIndex + "," + toRowIndex + "," + toColumnIndex))
return (1);
return (0);
};
this . getIllegalMovePredicate = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex)
{
for (var index = 0; index < this . illegalMoves . length; index = index + 1)
if (this . illegalMoves [index] == (fromRowIndex + "," + fromColumnIndex + "," + toRowIndex + "," + toColumnIndex))
return (1);
return (0);
};
this . showMarkers = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex)
{
if ((fromRowIndex % 2) == (fromColumnIndex % 2))
{
this . offMarkerNodes [0] . style . top = (fromRowIndex * 51) + "px";
this . offMarkerNodes [0] . style . left = (fromColumnIndex * 51) + "px";
this . offMarkerNodes [0] . style . display = "block";
}
else
{
this . onMarkerNodes [0] . style . top = (fromRowIndex * 51) + "px";
this . onMarkerNodes [0] . style . left = (fromColumnIndex * 51) + "px";
this . onMarkerNodes [0] . style . display = "block";
}
if ((toRowIndex % 2) == (toColumnIndex % 2))
{
this . offMarkerNodes [1] . style . top = (toRowIndex * 51) + "px";
this . offMarkerNodes [1] . style . left = (toColumnIndex * 51) + "px";
this . offMarkerNodes [1] . style . display = "block";
}
else
{
this . onMarkerNodes [1] . style . top = (toRowIndex * 51) + "px";
this . onMarkerNodes [1] . style . left = (toColumnIndex * 51) + "px";
this . onMarkerNodes [1] . style . display = "block";
}
};
this . showAuxiliaryMarkers = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex)
{
if ((fromRowIndex % 2) == (fromColumnIndex % 2))
{
this . offMarkerNodes [2] . style . top = (fromRowIndex * 51) + "px";
this . offMarkerNodes [2] . style . left = (fromColumnIndex * 51) + "px";
this . offMarkerNodes [2] . style . display = "block";
}
else
{
this . onMarkerNodes [2] . style . top = (fromRowIndex * 51) + "px";
this . onMarkerNodes [2] . style . left = (fromColumnIndex * 51) + "px";
this . onMarkerNodes [2] . style . display = "block";
}
if ((toRowIndex % 2) == (toColumnIndex % 2))
{
this . offMarkerNodes [3] . style . top = (toRowIndex * 51) + "px";
this . offMarkerNodes [3] . style . left = (toColumnIndex * 51) + "px";
this . offMarkerNodes [3] . style . display = "block";
}
else
{
this . onMarkerNodes [3] . style . top = (toRowIndex * 51) + "px";
this . onMarkerNodes [3] . style . left = (toColumnIndex * 51) + "px";
this . onMarkerNodes [3] . style . display = "block";
}
};
this . hideMarkers = function ()
{
this . offMarkerNodes [0] . style . display = "none";
this . offMarkerNodes [1] . style . display = "none";
this . offMarkerNodes [2] . style . display = "none";
this . offMarkerNodes [3] . style . display = "none";
this . onMarkerNodes [0] . style . display = "none";
this . onMarkerNodes [1] . style . display = "none";
this . onMarkerNodes [2] . style . display = "none";
this . onMarkerNodes [3] . style . display = "none";
};
this . getRefreshCode = function ()
{
var date = new Date ();
var refreshCode = "";
refreshCode = refreshCode + this . getPaddedText (date . getUTCFullYear () , "0", 4);
refreshCode = refreshCode + this . getPaddedText (date . getUTCMonth () , "0", 2);
refreshCode = refreshCode + this . getPaddedText (date . getUTCDay () , "0", 2);
refreshCode = refreshCode + this . getPaddedText (date . getUTCHours () , "0", 2);
refreshCode = refreshCode + this . getPaddedText (date . getUTCMinutes () , "0", 2);
refreshCode = refreshCode + this . getPaddedText (date . getUTCSeconds () , "0", 2);
refreshCode = refreshCode + this . getPaddedText (date . getUTCMilliseconds () , "0", 3);
return (refreshCode);
};
this . getPaddedText = function (text, padCharacter, characterCount)
{
text = text . toString ();
while (text . length < characterCount)
text = padCharacter + "" + text;
return (text);
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . State = function ()
{
this . emptyCode = null;
this . kingCode = null;
this . queenCode = null;
this . rookCode = null;
this . bishopCode = null;
this . knightCode = null;
this . pawnCode = null;
this . pieceCodes = null;
this . ownershipPredicates = null;
this . ownPin = null;
this . opponentPin = null;
this . initialize = function ()
{
this . emptyCode = 0;
this . kingCode = 1;
this . queenCode = 2;
this . rookCode = 3;
this . bishopCode = 4;
this . knightCode = 5;
this . pawnCode = 6;
this . occupancy = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
this . possession = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
this . pieceCodes = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
this . ownershipPredicates = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
this . ownPin = null;
this . opponentPin = null;
this . rowCount = 8;
this . columnCount = 8;
};
this . finalize = function ()
{
this . pieceCodes = null;
this . ownershipPredicates = null;
this . ownPin = null;
this . opponentPin = null;
};
this . reset = function ()
{
var rowIndex;
var columnIndex;
for (rowIndex = 0; rowIndex < this . rowCount; rowIndex = rowIndex + 1)
for (columnIndex = 0; columnIndex < this . columnCount; columnIndex = columnIndex + 1)
{
this . pieceCodes [rowIndex] [columnIndex] = this . emptyCode;
this . ownershipPredicates [rowIndex] [columnIndex] = ((rowIndex == 6) || (rowIndex == 7)) ? 1 : 0;
}
this . pieceCodes [0] [0] = this . rookCode;
this . pieceCodes [0] [1] = this . knightCode;
this . pieceCodes [0] [2] = this . bishopCode;
this . pieceCodes [0] [3] = this . queenCode;
this . pieceCodes [0] [4] = this . kingCode;
this . pieceCodes [0] [5] = this . bishopCode;
this . pieceCodes [0] [6] = this . knightCode;
this . pieceCodes [0] [7] = this . rookCode;
this . pieceCodes [1] [0] = this . pawnCode;
this . pieceCodes [1] [1] = this . pawnCode;
this . pieceCodes [1] [2] = this . pawnCode;
this . pieceCodes [1] [3] = this . pawnCode;
this . pieceCodes [1] [4] = this . pawnCode;
this . pieceCodes [1] [5] = this . pawnCode;
this . pieceCodes [1] [6] = this . pawnCode;
this . pieceCodes [1] [7] = this . pawnCode;
this . pieceCodes [6] [0] = this . pawnCode;
this . pieceCodes [6] [1] = this . pawnCode;
this . pieceCodes [6] [2] = this . pawnCode;
this . pieceCodes [6] [3] = this . pawnCode;
this . pieceCodes [6] [4] = this . pawnCode;
this . pieceCodes [6] [5] = this . pawnCode;
this . pieceCodes [6] [6] = this . pawnCode;
this . pieceCodes [6] [7] = this . pawnCode;
this . pieceCodes [7] [0] = this . rookCode;
this . pieceCodes [7] [1] = this . knightCode;
this . pieceCodes [7] [2] = this . bishopCode;
this . pieceCodes [7] [3] = this . queenCode;
this . pieceCodes [7] [4] = this . kingCode;
this . pieceCodes [7] [5] = this . bishopCode;
this . pieceCodes [7] [6] = this . knightCode;
this . pieceCodes [7] [7] = this . rookCode;
};
this . copy = function (state)
{
var rowIndex;
var columnIndex;
for (rowIndex = 0; rowIndex < this . rowCount; rowIndex = rowIndex + 1)
for (columnIndex = 0; columnIndex < this . columnCount; columnIndex = columnIndex + 1)
{
this . pieceCodes [rowIndex] [columnIndex] = state . pieceCodes [rowIndex] [columnIndex];
this . ownershipPredicates [rowIndex] [columnIndex] = state . ownershipPredicates [rowIndex] [columnIndex];
}
}
this . move = function (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex, enPassantPosition)
{
this . pieceCodes [toRowIndex] [toColumnIndex] = this . pieceCodes [fromRowIndex] [fromColumnIndex];
this . pieceCodes [fromRowIndex] [fromColumnIndex] = this . emptyCode;
this . ownershipPredicates [toRowIndex] [toColumnIndex] = this . ownershipPredicates [fromRowIndex] [fromColumnIndex];
this . ownershipPredicates [fromRowIndex] [fromColumnIndex] = 0;
if (enPassantPosition != null)
if (this . pieceCodes [toRowIndex] [toColumnIndex] == this . pawnCode)
if (toRowIndex == (enPassantPosition [0] - 1))
if (toColumnIndex == (enPassantPosition [1]))
this . pieceCodes [enPassantPosition [0]] [enPassantPosition [1]] = this . emptyCode;
};
this . reverse = function ()
{
for (rowIndex = 0; rowIndex < (this . rowCount / 2); rowIndex = rowIndex + 1)
for (columnIndex = 0; columnIndex < this . columnCount; columnIndex = columnIndex + 1)
{
var pieceCode = this . pieceCodes [rowIndex] [columnIndex];
this . pieceCodes [rowIndex] [columnIndex] = this . pieceCodes [this . rowCount - 1 - rowIndex] [this . columnCount - 1 - columnIndex];
this . pieceCodes [this . rowCount - 1 - rowIndex] [this . columnCount - 1 - columnIndex] = pieceCode;
}
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . Panel = function ()
{
this . height = 409;
this . width = 175;
// STATE
this . boardState = null; // AN ARRAY OF CODES THAT DESCRIBES WHAT OCCUPIES EACH SQUARE ON THE BOARD.
this . ownPin = null; // A MULTI-DIGIT PIN THAT IS USED TO IDENTIFY {THIS PLAYER, THIS BOARD INSTANCE}.
this . opponentPin = null; // A MULTI-DIGIT PIN THAT IS USED TO IDENTIFY {THAT PLAYER, THIS BOARD INSTANCE}.
this . currentPlayerPin = null; // A COPY OF THE PIN THAT IS OWNED BY THE PLAYER WHO'S TURN IT IS.
this . setOpponentPinDelegate = null;
this . setSizeDelegate = null;
this . okayButtonInstructions = null;
// INTERFACE
this . title = null;
this . node = null;
this . titleDivision = null;
this . checkDivision = null;
this . messageDivision = null;
this . opponentPlayerTypeFormDivision = null;
this . okayButtonDivision = null;
this . pinFormDivision = null;
this . pinDivision = null;
this . capturedDivision = null;
this . ownPinArea = null;
this . opponentPinArea = null;
this . playerDivision = null;
this . playerImage = null;
this . promotionFormDivision = null;
this . castleFormDivision = null;
this . sizeDivision = null;
this . lowMessageColor = null;
this . mediumMessageColor = null;
this . highMessageColor = null;
this . castleQueensideOption = null;
this . castleKingsideOption = null;
this . initialize = function ()
{
this . node = documentObjectModel . createNode ("div");
this . titleDivision = documentObjectModel . createNode ("div");
this . checkDivision = documentObjectModel . createNode ("div");
this . messageDivision = documentObjectModel . createNode ("div");
this . opponentPlayerTypeFormDivision = documentObjectModel . createNode ("div");
this . okayButtonDivision = documentObjectModel . createNode ("div");
this . pinFormDivision = documentObjectModel . createNode ("div");
this . pinDivision = documentObjectModel . createNode ("div");
this . capturedDivision = documentObjectModel . createNode ("div");
this . ownPinArea = documentObjectModel . createNode ("input");
this . opponentPinArea = documentObjectModel . createNode ("input");
this . playerDivision = documentObjectModel . createNode ("div");
this . playerImage = documentObjectModel . createNode ("img");
this . promotionFormDivision = documentObjectModel . createNode ("div");
this . castleFormDivision = documentObjectModel . createNode ("div");
this . sizeDivision = documentObjectModel . createNode ("div");
this . lowMessageColor = "#7c7b51";
this . mediumMessageColor = "#495537";
this . highMessageColor = "#553737";
this . title = "Chess";
this . node . id = "ChessPanel";
this . node . style . position = "absolute";
this . node . style . top = "0px";
this . node . style . left = "0px";
this . node . style . height = (51 * 8 - 1) + "px";
this . node . style . width = "175px";
this . node . style . backgroundColor = "#444444";
this . node . style . border = "#000000 2px solid";
this . node . style . overflow = "hidden";
this . titleDivision . id = "ChessPanel_TitleDivision";
this . titleDivision . style . position = "relative";
this . titleDivision . style . color = "#ffffff";
this . titleDivision . style . backgroundColor = "#222222";
this . titleDivision . style . borderBottom = "#333333 0px solid";
this . titleDivision . style . padding = "8px";
this . titleDivision . style . textAlign = "center";
this . checkDivision . id = "ChessPanel_checkDivision";
this . checkDivision . style . position = "relative";
this . checkDivision . style . color = "#ffffff";
this . checkDivision . style . backgroundColor = this . lowMessageColor;
this . checkDivision . style . borderBottom = "#333333 1px solid";
this . checkDivision . style . padding = "8px";
this . checkDivision . style . textAlign = "center";
this . checkDivision . style . display = "none";
this . messageDivision . id = "ChessPanel_MessageDivision";
this . messageDivision . style . position = "relative";
this . messageDivision . style . color = "#ffffff";
this . messageDivision . style . backgroundColor = this . lowMessageColor;
this . messageDivision . style . borderBottom = "#333333 1px solid";
this . messageDivision . style . padding = "8px";
this . messageDivision . style . textAlign = "center";
this . opponentPlayerTypeFormDivision . id = "ReversiPanel_OpponentPlayerTypeFormDivision";
this . opponentPlayerTypeFormDivision . style . position = "relative";
this . opponentPlayerTypeFormDivision . style . color = "#ffffff";
this . opponentPlayerTypeFormDivision . style . backgroundColor = "#555555";
this . opponentPlayerTypeFormDivision . style . borderBottom = "#333333 1px solid";
this . opponentPlayerTypeFormDivision . style . padding = "8px";
this . opponentPlayerTypeFormDivision . style . textAlign = "center";
this . opponentPlayerTypeFormDivision . style . display = "none";
this . okayButtonDivision . id = "ChessPanel_OkayButtonDivision";
this . okayButtonDivision . that = this;
this . okayButtonDivision . style . position = "relative";
this . okayButtonDivision . style . cursor = "pointer";
this . okayButtonDivision . style . color = "#ffffff";
this . okayButtonDivision . style . backgroundColor = "#555555";
this . okayButtonDivision . style . borderBottom = "#333333 1px solid";
this . okayButtonDivision . style . padding = "8px";
this . okayButtonDivision . style . textAlign = "center";
this . okayButtonDivision . style . display = "none";
var makaylaOption = documentObjectModel . createNode ("img");
var friendOption = documentObjectModel . createNode ("img");
makaylaOption . that = this;
makaylaOption . style . cursor = "pointer";
makaylaOption . opponentPlayerType = "virtual";
makaylaOption . src = "/games/chess/Chess/pictures/button.makayla.jpg";
makaylaOption . onclick = function () {this . that . setOpponentPlayerTypeDelegate (this . opponentPlayerType);}
friendOption . that = this;
friendOption . style . cursor = "pointer";
friendOption . opponentPlayerType = "actual";
friendOption . src = "/games/chess/Chess/pictures/button.friend.jpg";
friendOption . onclick = function () {this . that . setOpponentPlayerTypeDelegate (this . opponentPlayerType);}
documentObjectModel . appendNode (this . opponentPlayerTypeFormDivision, makaylaOption);
documentObjectModel . appendNode (this . opponentPlayerTypeFormDivision, documentObjectModel . createNode ("br"));
documentObjectModel . appendNode (this . opponentPlayerTypeFormDivision, friendOption);
this . pinFormDivision . id = "ChessPanel_pinFormDivision";
this . pinFormDivision . style . position = "relative";
this . pinFormDivision . style . color = "#ffffff";
this . pinFormDivision . style . backgroundColor = "#555555";
this . pinFormDivision . style . borderBottom = "#333333 1px solid";
this . pinFormDivision . style . padding = "8px";
this . pinFormDivision . style . textAlign = "center";
this . pinFormDivision . style . display = "none";
this . ownPinArea . id = "ChessPanel_ownPinArea";
this . ownPinArea . style . position = "relative";
this . ownPinArea . style . width = "50px";
this . ownPinArea . style . color = "#ffffff";
this . ownPinArea . style . backgroundColor = "#555555";
this . ownPinArea . style . border = "#000000 1px solid";
this . ownPinArea . style . padding = "2px";
this . ownPinArea . style . textAlign = "center";
this . ownPinArea . value = this . ownPin;
this . opponentPinArea . id = "ChessPanel_opponentPinArea";
this . opponentPinArea . that = this;
this . opponentPinArea . style . position = "relative";
this . opponentPinArea . style . width = "50px";
this . opponentPinArea . style . color = "#000000";
this . opponentPinArea . style . backgroundColor = "#ffffff";
this . opponentPinArea . style . border = "#000000 1px solid";
this . opponentPinArea . style . padding = "2px";
this . opponentPinArea . style . textAlign = "center";
this . opponentPinArea . value = "";
this . ownPinArea . onkeypress = function (keyEvent)
{
var keyCode = context . getKeyCode (keyEvent);
return (false);
};
this . opponentPinArea . onkeypress = function (keyEvent)
{
var keyCode = documentObjectModel . getKeyCode (keyEvent);
// APPROVE CERTAIN CHARACTERS FIRST, REJECT OTHER CHARACTERS LATER.
if (keyCode == 8) return (true); // BACKSPACE
if (keyCode == 48) return (true); // 0
if (keyCode == 49) return (true); // 1
if (keyCode == 50) return (true); // 2
if (keyCode == 51) return (true); // 3
if (keyCode == 52) return (true); // 4
if (keyCode == 53) return (true); // 5
if (keyCode == 54) return (true); // 6
if (keyCode == 55) return (true); // 7
if (keyCode == 56) return (true); // 8
if (keyCode == 57) return (true); // 9
// AT THIS POINT, WE KNOW THE CHARACTER IS NOT ALLOWED, SO DISCARD IT.
return (false);
};
this . opponentPinArea . onkeyup = function ()
{
if (this . value == this . that . ownPinArea . value) this . value = "";
if (this . value . length > 4) this . value = this . value . substring (0, 4);
if (this . value . length == 4)
{
this . that . setOpponentPinDelegate (this . value);
}
};
this . okayButtonDivision . onclick = function ()
{
this . that . hideOkayButton ();
};
this . pinDivision . id = "ChessPanel_PinDivision";
this . pinDivision . style . position = "relative";
this . pinDivision . style . color = "#ffffff";
this . pinDivision . style . backgroundColor = "#555555";
this . pinDivision . style . borderBottom = "#333333 1px solid";
this . pinDivision . style . padding = "8px";
this . pinDivision . style . textAlign = "center";
this . pinDivision . style . display = "none";
this . capturedDivision . id = "ChessPanel_capturedDivision";
this . capturedDivision . style . position = "relative";
this . capturedDivision . style . color = "#aaaaaa";
this . capturedDivision . style . backgroundColor = "#444444";
this . capturedDivision . style . borderBottom = "#444444 1px solid";
this . capturedDivision . style . padding = "8px";
this . capturedDivision . style . textAlign = "center";
this . capturedDivision . style . display = "none";
this . playerDivision . id = "ChessPanel_PlayerDivision";
this . playerDivision . style . position = "relative";
this . playerDivision . style . color = "#ffffff";
this . playerDivision . style . backgroundColor = "#444444";
this . playerDivision . style . borderBottom = "#444444 1px solid";
this . playerDivision . style . padding = "8px";
this . playerDivision . style . textAlign = "center";
this . playerDivision . style . display = "block";
var javascriptText;
javascriptText = "";
javascriptText = javascriptText + "javascript: ";
javascriptText = javascriptText + "messageBox . initialize (); ";
javascriptText = javascriptText + "messageBox . addType (\"Events\"); ";
javascriptText = javascriptText + "messageBox . addType (\"Notes\"); ";
javascriptText = javascriptText + "messageBox . addType (\"Warnings\"); ";
javascriptText = javascriptText + "messageBox . addType (\"Errors\"); ";
this . playerImage . that = this;
this . playerImage . src = "/games/chess/Chess/pictures/piece.primary.pawn." + (navigator . userAgent . indexOf ("Firefox") != - 1 ? "png" : "gif");
this . playerImage . style . display = "block";
this . playerImage . style . marginLeft = "auto";
this . playerImage . style . marginRight = "auto";
this . playerImage . style . height = "50px";
this . playerImage . style . width = "50px";
this . playerImage . setAttribute ("onclick", javascriptText);
this . promotionFormDivision . id = "ChessPanel_PromotionFormDivision";
this . promotionFormDivision . style . position = "relative";
this . promotionFormDivision . style . color = "#ffffff";
this . promotionFormDivision . style . backgroundColor = "#555555";
this . promotionFormDivision . style . borderBottom = "#333333 1px solid";
this . promotionFormDivision . style . padding = "8px";
this . promotionFormDivision . style . textAlign = "center";
this . promotionFormDivision . style . display = "none";
this . castleFormDivision . id = "ChessPanel_CastleFormDivision";
this . castleFormDivision . style . position = "relative";
this . castleFormDivision . style . color = "#ffffff";
this . castleFormDivision . style . backgroundColor = "#555555";
this . castleFormDivision . style . borderBottom = "#333333 1px solid";
this . castleFormDivision . style . padding = "8px";
this . castleFormDivision . style . textAlign = "center";
this . castleFormDivision . style . display = "none";
this . sizeDivision . id = "ChessPanel_SizeDivision";
this . sizeDivision . style . position = "relative";
this . sizeDivision . style . color = "#ffffff";
this . sizeDivision . style . backgroundColor = "#555555";
this . sizeDivision . style . borderBottom = "#333333 1px solid";
this . sizeDivision . style . padding = "8px";
this . sizeDivision . style . textAlign = "center";
this . sizeDivision . style . display = "none";
// APPEND THE MAJOR COMPONENTS TO THE PANEL.
documentObjectModel . appendNode (this . getNode (), this . titleDivision);
documentObjectModel . appendNode (this . getNode (), this . checkDivision);
documentObjectModel . appendNode (this . getNode (), this . messageDivision);
documentObjectModel . appendNode (this . getNode (), this . okayButtonDivision);
documentObjectModel . appendNode (this . getNode (), this . opponentPlayerTypeFormDivision);
documentObjectModel . appendNode (this . getNode (), this . promotionFormDivision);
documentObjectModel . appendNode (this . getNode (), this . castleFormDivision);
documentObjectModel . appendNode (this . getNode (), this . sizeDivision);
documentObjectModel . appendNode (this . getNode (), this . pinFormDivision);
documentObjectModel . appendNode (this . getNode (), this . pinDivision);
documentObjectModel . appendNode (this . getNode (), this . playerDivision);
documentObjectModel . appendNode (this . getNode (), this . capturedDivision);
documentObjectModel . appendNode (this . playerDivision, this . playerImage);
// APPEND THE MINOR COMPONENTS TO THE MAJOR COMPONENTS.
var rookOption = documentObjectModel . createNode ("img");
var knightOption = documentObjectModel . createNode ("img");
var bishopOption = documentObjectModel . createNode ("img");
var queenOption = documentObjectModel . createNode ("img");
documentObjectModel . appendNode (this . promotionFormDivision, rookOption);
documentObjectModel . appendNode (this . promotionFormDivision, documentObjectModel . createNode ("br"));
documentObjectModel . appendNode (this . promotionFormDivision, knightOption);
documentObjectModel . appendNode (this . promotionFormDivision, documentObjectModel . createNode ("br"));
documentObjectModel . appendNode (this . promotionFormDivision, bishopOption);
documentObjectModel . appendNode (this . promotionFormDivision, documentObjectModel . createNode ("br"));
documentObjectModel . appendNode (this . promotionFormDivision, queenOption);
rookOption . that = this;
rookOption . style . cursor = "pointer";
rookOption . typeText = "rook";
rookOption . src = "/games/chess/Chess/pictures/button.rook.jpg";
rookOption . onclick = function () {this . that . promotionFormDivision . style . display = "none"; this . that . promoteDelegate (this . typeText);}
knightOption . that = this;
knightOption . style . cursor = "pointer";
knightOption . typeText = "knight";
knightOption . src = "/games/chess/Chess/pictures/button.knight.jpg";
knightOption . onclick = function () {this . that . promotionFormDivision . style . display = "none"; this . that . promoteDelegate (this . typeText);}
bishopOption . that = this;
bishopOption . style . cursor = "pointer";
bishopOption . typeText = "bishop";
bishopOption . src = "/games/chess/Chess/pictures/button.bishop.jpg";
bishopOption . onclick = function () {this . that . promotionFormDivision . style . display = "none"; this . that . promoteDelegate (this . typeText);}
queenOption . that = this;
queenOption . style . cursor = "pointer";
queenOption . typeText = "queen";
queenOption . src = "/games/chess/Chess/pictures/button.queen.jpg";
queenOption . onclick = function () {this . that . promotionFormDivision . style . display = "none"; this . that . promoteDelegate (this . typeText);}
var castledivision0 = documentObjectModel . createNode ("div");
var castledivision1 = documentObjectModel . createNode ("div");
this . castleQueensideOption = documentObjectModel . createNode ("img");
this . castleKingsideOption = documentObjectModel . createNode ("img");
this . castleQueensideOption . setAttribute ("align", "center");
this . castleKingsideOption . setAttribute ("align", "center");
this . castleQueensideOption . that = this;
this . castleQueensideOption . style . cursor = "pointer";
this . castleQueensideOption . sideName = "queen";
this . castleQueensideOption . src = "/games/chess/Chess/pictures/button.castlequeenside.jpg";
this . castleQueensideOption . onclick = function () {this . that . castleDelegate (this . sideName);}
this . castleQueensideOption . style . display = "block";
this . castleQueensideOption . style . marginLeft = "auto";
this . castleQueensideOption . style . marginRight = "auto";
this . castleKingsideOption . that = this;
this . castleKingsideOption . style . cursor = "pointer";
this . castleKingsideOption . sideName = "king";
this . castleKingsideOption . src = "/games/chess/Chess/pictures/button.castlekingside.jpg";
this . castleKingsideOption . onclick = function () {this . that . castleDelegate (this . sideName);}
this . castleKingsideOption . style . display = "block";
this . castleKingsideOption . style . marginLeft = "auto";
this . castleKingsideOption . style . marginRight = "auto";
documentObjectModel . appendNode (castledivision0, this . castleQueensideOption);
documentObjectModel . appendNode (castledivision1, this . castleKingsideOption);
documentObjectModel . appendNode (this . castleFormDivision, castledivision0);
documentObjectModel . appendNode (this . castleFormDivision, castledivision1);
documentObjectModel . appendNode (this . titleDivision, documentObjectModel . createTextNode (this . title));
var okayOption;
okayOption = documentObjectModel . createNode ("img");
okayOption . that = this;
okayOption . style . cursor = "pointer";
okayOption . typeText = "okay";
okayOption . src = "/games/chess/Chess/pictures/button.okay.jpg";
documentObjectModel . appendNode (this . okayButtonDivision, okayOption);
documentObjectModel . appendNode (this . pinFormDivision, this . ownPinArea);
documentObjectModel . appendNode (this . pinFormDivision, documentObjectModel . createTextNode (" "));
documentObjectModel . appendNode (this . pinFormDivision, this . opponentPinArea);
this . setMessage ("low", "Initializing");
};
this . showCheckMessage = function (player)
{
var message;
if (player == "both")
message = "You and your opponent are both in check.";
else if (player == "own")
message = "You are in check.";
else
message = "Your opponent is in check.";
this . checkDivision . style . backgroundColor = this . highMessageColor;
documentObjectModel . clearNodes (this . checkDivision);
documentObjectModel . appendNode (this . checkDivision, documentObjectModel . createTextNode (message));
this . checkDivision . style . display = "block";
};
this . showPlayerMessage = function (player)
{
var message;
var extension;
if (navigator . userAgent . indexOf ("Firefox") != - 1)
extension = "png";
else
extension = "gif";
if (player == "primary")
this . playerImage . src = "/games/chess/Chess/pictures/piece.primary.pawn." + extension;
else
this . playerImage . src = "/games/chess/Chess/pictures/piece.secondary.pawn." + extension;
this . playerDivision . style . display = "block";
};
this . hideCheckMessage = function ()
{
this . checkDivision . style . display = "none";
};
this . setMessage = function (level, message)
{
if (level == "low") this . messageDivision . style . backgroundColor = this . lowMessageColor;
if (level == "medium") this . messageDivision . style . backgroundColor = this . mediumMessageColor;
if (level == "high") this . messageDivision . style . backgroundColor = this . highMessageColor;
documentObjectModel . clearNodes (this . messageDivision);
documentObjectModel . appendNode (this . messageDivision, documentObjectModel . createTextNode (message));
};
this . displayOpponentPlayerTypePrompt = function (delegate)
{
this . setOpponentPlayerTypeDelegate = delegate;
this . setMessage ("medium", "Who would you like to challenge?");
this . opponentPlayerTypeFormDivision . style . display = "block";
this . setOpponentPlayerTypeDelegate ("actual");
return;
};
this . hideOpponentPlayerTypePrompt = function ()
{
this . opponentPlayerTypeFormDivision . style . display = "none";
return;
};
this . displayPinPrompt = function (ownPin, delegate)
{
this . ownPin = ownPin;
this . ownPinArea . value = ownPin;
this . setOpponentPinDelegate = delegate;
this . setMessage ("medium", "Please exchange pins with your opponent.");
this . pinFormDivision . style . display = "block";
};
this . showPins = function (ownPin, opponentPin)
{
this . pinFormDivision . style . display = "none";
this . pinDivision . style . display = "block";
documentObjectModel . appendNode (this . pinDivision, documentObjectModel . createTextNode (ownPin + " : " + opponentPin));
};
this . showCaptures = function (captures)
{
if (captures == null) return;
if (captures . length == 0) return;
var capturedTitle;
capturedTitle = documentObjectModel . createNode ("span");
capturedTitle . style . color = "#ffffff";
documentObjectModel . appendNode (capturedTitle, documentObjectModel . createTextNode ("Captured"));
documentObjectModel . clearNodes (this . capturedDivision);
documentObjectModel . appendNode (this . capturedDivision, capturedTitle);
documentObjectModel . appendNode (this . capturedDivision, documentObjectModel . createNode ("br"));
for (var index = 0; index < captures . length; index = index + 1)
{
documentObjectModel . appendNode (this . capturedDivision, documentObjectModel . createTextNode (captures [index]));
documentObjectModel . appendNode (this . capturedDivision, documentObjectModel . createNode ("br"));
}
this . capturedDivision . style . display = "block";
};
this . hideCaptures = function ()
{
this . capturedDivision . style . display = "none";
};
this . showPromotionPrompt = function (delegate)
{
this . promoteDelegate = delegate;
this . setMessage ("medium", "Which piece should the pawn be promoted to?");
this . promotionFormDivision . style . display = "block";
};
this . showCastlePrompt = function (delegate, queensidePredicate, kingsidePredicate)
{
this . castleDelegate = delegate;
this . castleQueensideOption . style . display = (queensidePredicate == 1 ? "block" : "none");
this . castleKingsideOption . style . display = (kingsidePredicate == 1 ? "block" : "none");
this . castleFormDivision . style . display = "block";
};
this . hideCastlePrompt = function ()
{
this . castleFormDivision . style . display = "none";
};
this . showBoardSize = function (boardSize)
{
this . promotionFormDivision . style . display = "none";
this . sizeDivision . style . display = "block";
documentObjectModel . appendNode (this . sizeDivision, documentObjectModel . createTextNode (boardSize + " Board"));
};
this . showOkayButton = function (okayButtonInstructions)
{
this . okayButtonInstructions = okayButtonInstructions;
this . okayButtonDivision . style . display = "block";
};
this . hideOkayButton = function ()
{
this . okayButtonDivision . style . display = "none";
setTimeout (this . okayButtonInstructions, 10);
};
this . getNode = function ()
{
return (this . node);
};
this . render = function (renderNode)
{
documentObjectModel . appendNode (renderNode, this . getNode ());
};
this . setRowPixelCount = function (rowPixelCount)
{
this . node . style . height = (rowPixelCount) + "px";
};
this . setBoardSize = function (heightSquareCount, widthSquareCount)
{
for (var rowIndex = 0; rowIndex < heightSquareCount; rowIndex = rowIndex + 1)
{
this . boardState [rowIndex] = new Array ();
}
for (var rowIndex = 0; rowIndex < heightSquareCount; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < widthSquareCount; columnIndex = columnIndex + 1)
{
this . boardState [rowIndex] [columnIndex] = new Array ();
}
};
}
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . Board = function ()
{
this . node = null;
this . glassNode = null;
this . rowCount = null;
this . columnCount = null;
this . squareHeight = null;
this . squareWidth = null;
this . height = null;
this . width = null;
this . squareNodes = null;
this . squareColor0 = null;
this . squareColor1 = null;
this . initialize = function ()
{
this . node = documentObjectModel . createNode ("div");
this . glassNode = documentObjectModel . createNode ("div");
this . squareNodes = new Array (new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array (), new Array ());
this . rowCount = 8;
this . columnCount = 8;
this . squareHeight = 50;
this . squareWidth = 50;
this . height = (this . squareHeight + 1) * this . rowCount - 1;
this . width = (this . squareWidth + 1) * this . columnCount - 1;
this . squareColor0 = "#434343";
this . squareColor1 = "#3f3f3f";
this . render ();
};
this . finalize = function ()
{
this . node = null;
this . height = null;
this . width = null;
this . rowCount = null;
this . columnCount = null;
};
this . getNode = function ()
{
return (this . node);
};
this . render = function ()
{
this . node . style . position = "absolute";
this . node . style . top = "0px";
this . node . style . left = "200px";
this . node . style . height = (this . height) + "px";
this . node . style . width = (this . width) + "px";
this . node . style . backgroundColor = "#353535";
this . node . style . border = "#000000 2px solid";
this . node . style . overflow = "hidden";
this . node . style . zIndex = "0";
this . glassNode . style . position = "absolute";
this . glassNode . style . top = "0px";
this . glassNode . style . left = "0px";
this . glassNode . style . height = (this . height) + "px";
this . glassNode . style . width = (this . width) + "px";
this . glassNode . style . zIndex = "4";
if (0) // The following is not needed when using XHTML.
if (navigator . userAgent . indexOf ("MSIE") != - 1)
{
this . height = (this . squareHeight + 1) * this . rowCount - 1 + 4;
this . width = (this . squareWidth + 1) * this . columnCount - 1 + 4;
this . node . style . height = (this . height) + "px";
this . node . style . width = (this . width) + "px";
this . glassNode . style . height = (this . height) + "px";
this . glassNode . style . width = (this . width) + "px";
}
for (var rowIndex = 0; rowIndex < 12; rowIndex = rowIndex + 1)
for (var columnIndex = 0; columnIndex < 12; columnIndex = columnIndex + 1)
{
var node;
node = documentObjectModel . createNode ("div");
node . style . position = "absolute";
node . style . top = (rowIndex * (this . squareHeight + 1)) + "px";
node . style . left = (columnIndex * (this . squareWidth + 1)) + "px";
node . style . height = "50px";
node . style . width = "50px";
node . style . backgroundColor = ((rowIndex % 2) == (columnIndex % 2)) ? this . squareColor0 : this . squareColor1;
this . squareNodes [rowIndex] [columnIndex] = node;
documentObjectModel . appendNode (this . node, node);
}
documentObjectModel . appendNode (this . node, this . glassNode);
};
this . setBoardSize = function (boardSize)
{
if (boardSize == "default" ) this . rowCount = 8;
if (boardSize == "Standard" ) this . rowCount = 8;
if (boardSize == "Small" ) this . rowCount = 8;
if (boardSize == "Medium" ) this . rowCount = 10;
if (boardSize == "Large" ) this . rowCount = 12;
if (boardSize == "default" ) this . columnCount = 8;
if (boardSize == "Standard" ) this . columnCount = 8;
if (boardSize == "Small" ) this . columnCount = 8;
if (boardSize == "Medium" ) this . columnCount = 10;
if (boardSize == "Large" ) this . columnCount = 12;
if (navigator . userAgent . indexOf ("MSIE") != - 1)
{
this . height = (this . squareHeight + 1) * this . rowCount - 1 + 4;
this . width = (this . squareWidth + 1) * this . columnCount - 1 + 4;
}
else
{
this . height = (this . squareHeight + 1) * this . rowCount - 1;
this . width = (this . squareWidth + 1) * this . columnCount - 1;
}
this . node . style . height = (this . height) + "px";
this . node . style . width = (this . width) + "px";
this . glassNode . style . height = (this . height) + "px";
this . glassNode . style . width = (this . width) + "px";
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . Piece = function ()
{
this . node = null;
this . ownPredicate = null;
this . reversedPredicate = null;
this . height = null;
this . width = null;
this . initialize = function ()
{
this . type = "pawn";
this . node = documentObjectModel . createNode ("div");
this . ownPredicate = 0;
this . reversedPredicate = 0;
this . height = 50;
this . width = 50;
this . node . style . position = "absolute";
this . node . style . top = "0px";
this . node . style . left = "0px";
this . node . style . height = (this . height) + "px";
this . node . style . width = (this . width) + "px";
this . node . style . display = "none";
this . node . style . zIndex = "2";
this . render ();
};
this . finalize = function ()
{
this . node = null;
this . ownPredicate = null;
this . reversedPredicate = null;
this . height = null;
this . width = null;
};
this . elevate = function ()
{
this . node . style . zIndex = "3";
};
this . ground = function ()
{
this . node . style . zIndex = "2";
};
this . getHeight = function ()
{
return (this . height);
};
this . getWidth = function ()
{
return (this . width);
};
this . getNode = function ()
{
return (this . node);
};
this . setCoordinates = function (rowCoordinate, columnCoordinate)
{
this . node . style . top = (rowCoordinate) + "px";
this . node . style . left = (columnCoordinate) + "px";
};
this . setType = function (type)
{
this . type = type;
this . render ();
};
this . getType = function ()
{
return (this . type);
};
this . setOwnPredicate = function (predicate)
{
this . ownPredicate = predicate;
this . render ();
};
this . getOwnPredicate = function (predicate)
{
return (this . ownPredicate);
};
this . reverse = function ()
{
this . reversedPredicate = 1;
this . render ();
};
this . render = function ()
{
var extension;
if (navigator . userAgent . indexOf ("Firefox") != - 1)
extension = "png";
else
extension = "gif";
if (! this . reversedPredicate)
{
if (this . ownPredicate)
this . node . style . backgroundImage = "url(/games/chess/Chess/pictures/piece.primary." + this . type + "." + extension + ")";
else
this . node . style . backgroundImage = "url(/games/chess/Chess/pictures/piece.secondary." + this . type + "." + extension + ")";
}
else
{
if (this . ownPredicate)
this . node . style . backgroundImage = "url(/games/chess/Chess/pictures/piece.secondary." + this . type + "." + extension + ")";
else
this . node . style . backgroundImage = "url(/games/chess/Chess/pictures/piece.primary." + this . type + "." + extension + ")";
}
};
};
// MICHAEL MCELROY, MICHAELMCELROY.NET
// COPYRIGHT, 2006, ALL RIGHTS RESERVED
if (typeof (Chess) == "undefined") Chess = new Object ();
Chess . Kayla = function ()
{
this . initialize = function ()
{
application . getMove_Override = application . getMove;
application . getMove = this . play;
return;
};
this . play = function (event)
{
application . opponentMove = kayla . getPreferredMove ();
application . getMove_Override ();
return (false);
};
this . getPreferredMove = function ()
{
var preferredMove = null;
var moves = null;
var offensiveMoves = null;
var defensiveMoves = null;
var safeMoves = null;
application . trimOpponentMoves ();
moves = application . legalOpponentMoves;
if ((moves . length == 0) && (application . opponentCheckPredicate == 1))
{
preferredMove = "checkmate" + application . opponentPin;
}
else if ((moves . length == 0) && (application . opponentCheckPredicate == 0))
{
preferredMove = "stalemate" + application . opponentPin;
}
else
{
preferredMove = this . getRankingMove (moves) + ",queen";
}
return (preferredMove);
};
this . getRankingMove = function (moves)
{
var rankingMove = null;
var rank = null;
var test = null; // A simple object used as a container for the test state.
var moveIndex = null;
var ownMoveIndex = null;
var opponentMoveIndex = null;
var boardScore = null;
var rowIndex = null;
var columnIndex = null;
var fromRowIndex = null;
var fromColumnIndex = null;
var toRowIndex = null;
var toColumnIndex = null;
rankingMove = null;
rank = null;
test = new Object ();
test . state = new Chess . State ();
test . state . initialize ();
for (moveIndex = 0; moveIndex < moves . length; moveIndex = moveIndex + 1)
{
test . state . copy (application . state);
fromRowIndex = (moves [moveIndex] . split (",")) [0];
fromColumnIndex = (moves [moveIndex] . split (",")) [1];
toRowIndex = (moves [moveIndex] . split (",")) [2];
toColumnIndex = (moves [moveIndex] . split (",")) [3];
// Calculate the board's score.
// board score = sum of piece scores + sum of own piece values - sum of opponent piece values
// piece score = 0
// + sum of piece values of pieces it can capture
// - sum of (identity value * how many opponent pieces can capture it)
boardScore = 0;
// Increase the score if this move captures a piece.
if (test . state . ownershipPredicates [toRowIndex] [toColumnIndex] == 1) // Remember that Kayla is the opponent.
{
switch (test . state . pieceCodes [toRowIndex] [toColumnIndex])
{
case test . state . kingCode: boardScore = boardScore + 9 * 4; break;
case test . state . queenCode: boardScore = boardScore + 9 * 2; break;
case test . state . rookCode: boardScore = boardScore + 5 * 2; break;
case test . state . bishopCode: boardScore = boardScore + 3; break;
case test . state . knightCode: boardScore = boardScore + 3; break;
case test . state . pawnCode: boardScore = boardScore + 1; break;
}
}
test . state . move (fromRowIndex, fromColumnIndex, toRowIndex, toColumnIndex);
application . evaluate (test);
// Increase the score for each of Kayla's pieces.
// Decrease the score for each of the opponent's pieces (remember that Kayla is the opponent).
for (rowIndex = 0; rowIndex < application . board . rowCount; rowIndex = rowIndex + 1)
{
for (columnIndex = 0; columnIndex < application . board . columnCount; columnIndex = columnIndex + 1)
{
if (test . state . ownershipPredicates [rowIndex] [columnIndex] == 0) // Remember that Kayla is the opponent.
{
switch (test . state . pieceCodes [rowIndex] [columnIndex])
{
case test . state . queenCode: boardScore = boardScore + 9; break;
case test . state . rookCode: boardScore = boardScore + 5; break;
case test . state . bishopCode: boardScore = boardScore + 3; break;
case test . state . knightCode: boardScore = boardScore + 100 + 3; break;
case test . state . pawnCode: boardScore = boardScore + 1; break;
}
}
else
{
switch (test . state . pieceCodes [rowIndex] [columnIndex])
{
case test . state . queenCode: boardScore = boardScore - 9; break;
case test . state . rookCode: boardScore = boardScore - 5; break;
case test . state . bishopCode: boardScore = boardScore - 3; break;
case test . state . knightCode: boardScore = boardScore - 3; break;
case test . state . pawnCode: boardScore = boardScore - 1; break;
}
}
}
}
// Check for any moves where Kayla can capture a piece.
// Remember that Kayla is the opponent.
for (opponentMoveIndex = 0; opponentMoveIndex < test . opponentMoves . length; opponentMoveIndex = opponentMoveIndex + 1)
{
fromRowIndex = (test . opponentMoves [opponentMoveIndex] . split (",")) [0];
fromColumnIndex = (test . opponentMoves [opponentMoveIndex] . split (",")) [1];
toRowIndex = (test . opponentMoves [opponentMoveIndex] . split (",")) [2];
toColumnIndex = (test . opponentMoves [opponentMoveIndex] . split (",")) [3];
if (test . state . ownershipPredicates [toRowIndex] [toColumnIndex] == 1) // Remember that Kayla is the opponent.
{
// Empty squares will fall through the switch.
switch (test . state . pieceCodes [toRowIndex] [toColumnIndex])
{
case test . state . queenCode: boardScore = boardScore + 9; break;
case test . state . rookCode: boardScore = boardScore + 5; break;
case test . state . bishopCode: boardScore = boardScore + 3; break;
case test . state . knightCode: boardScore = boardScore + 3; break;
case test . state . pawnCode: boardScore = boardScore + 1; break;
}
}
}
// Check for any moves where the opponent can capture a piece.
// Remember that Kayla is the opponent.
for (ownMoveIndex = 0; ownMoveIndex < test . ownMoves . length; ownMoveIndex = ownMoveIndex + 1)
{
fromRowIndex = (test . ownMoves [ownMoveIndex] . split (",")) [0];
fromColumnIndex = (test . ownMoves [ownMoveIndex] . split (",")) [1];
toRowIndex = (test . ownMoves [ownMoveIndex] . split (",")) [2];
toColumnIndex = (test . ownMoves [ownMoveIndex] . split (",")) [3];
if (test . state . ownershipPredicates [toRowIndex] [toColumnIndex] == 0) // Remember that Kayla is the opponent.
{
// Empty squares will fall through the switch.
switch (test . state . pieceCodes [toRowIndex] [toColumnIndex])
{
case test . state . queenCode: boardScore = boardScore - 9; break;
case test . state . rookCode: boardScore = boardScore - 5; break;
case test . state . bishopCode: boardScore = boardScore - 3; break;
case test . state . knightCode: boardScore = boardScore - 100 - 3; break;
case test . state . pawnCode: boardScore = boardScore - 1; break;
}
}
}
if (rank != "checkmate")
{
// The random part mixes up the equal moves, so Kayla does not necessarily try the same move every time.
if ((rank == null) || (boardScore == "checkmate") || (rank < boardScore) || ((rank == boardScore) && (Math . floor (Math . random () * 3) == 0)))
{
rank = boardScore;
rankingMove = moves [moveIndex];
}
}
}
messageBox . addMessage ("notes", "preferred move = " + rankingMove + "\n" + "rank = " + rank);
return (rankingMove);
};
};
// ARRANGE TO HAVE THE APPLICATION INITIATED AS SOON AS THE DOCUMENT LOADS.
var setupChess = function ()
{
showWaitMessage ();
setTimeout ("initializeChess ();", 500);
};
var initializeChess = function ()
{
messageBox = new Development . MessageBox ();
messageBox . initiate ();
documentObjectModel = new Network . DocumentObjectModel ();
documentObjectModel . initialize ();
socket = new Network . Socket ();
socket . initialize ();
internetClient = new Network . InternetClient ();
internetClient . initialize ();
application = new Chess . Application ();
application . initialize ();
};
var showWaitMessage = function ()
{
var documentObjectModel;
documentObjectModel = new Network . DocumentObjectModel ();
documentObjectModel . initialize ();
var division = documentObjectModel . createNode ("div");
var span = documentObjectModel . createNode ("span");
var link = documentObjectModel . createNode ("a");
division . style . color = "#545f36";
division . style . textAlign = "center";
span . style . fontSize = "18px";
span . style . display = "block";
span . style . width = "100%";
span . style . color = "#3e4625";
link . style . color = "#545f36";
link . target = "_blank";
link . href = "http://www.mozilla.com/firefox";
documentObjectModel . appendNode (documentObjectModel . getNode ("game"), division);
documentObjectModel . appendNode (division, span);
documentObjectModel . appendNode (division, documentObjectModel . createNode ("br"));
if (navigator . userAgent . indexOf ("MSIE") != - 1)
{
documentObjectModel . appendNode (division, documentObjectModel . createTextNode ("Internet Explorer requires additional time to initialize the game."));
// documentObjectModel . appendNode (division, documentObjectModel . createTextNode ("Internet Explorer requires extra time to initialize the game."));
// documentObjectModel . appendNode (division, documentObjectModel . createNode ("br"));
// documentObjectModel . appendNode (division, documentObjectModel . createTextNode ("Internet Explorer also requires extra time to start the game, so please be patient."));
// documentObjectModel . appendNode (division, documentObjectModel . createNode ("br"));
// documentObjectModel . appendNode (division, documentObjectModel . createTextNode ("Please consider using "));
// documentObjectModel . appendNode (division, link);
// documentObjectModel . appendNode (division, documentObjectModel . createTextNode ("."));
// documentObjectModel . appendNode (division, documentObjectModel . createNode ("br"));
}
documentObjectModel . appendNode (span, documentObjectModel . createTextNode ("loading ..."));
// documentObjectModel . appendNode (link, documentObjectModel . createTextNode ("Firefox"));
};
var hideWaitMessage = function ()
{
var documentObjectModel;
documentObjectModel = new Network . DocumentObjectModel ();
documentObjectModel . initialize ();
documentObjectModel . clearNodes (documentObjectModel . getNode ("game"));
};