class ChessBoard { var grid:Array, board:MovieClip, pieceIdentifier:String, tileWidth:Number; var currentMove:Number, lastNonDrawMove:Number, whiteKing:ChessPiece, blackKing:ChessPiece, piecesAlive:Number; var whitePieces:Array, blackPieces:Array, boardStates:Array, history:Array, historyText:String, latestHistoryAddition:String; var rowMap:Array = new Array("1", "2", "3", "4", "5", "6", "7", "8"); var columnMap:Array = new Array("h", "g", "f", "e", "d", "c", "b", "a"); var depth:Number = 100; var castling:Boolean = true; var castlingInfo:Array; var enpassant:Boolean = true; var enpassantTile:Tile; var promotePawn:Boolean; var boardReverse:Boolean = false; var pieceID:Number = 0; function ChessBoard() { //constructor } function initialize() { //Does everything needed to create and initialize the board in memory //set the current move to 0 since no moves have yet been made currentMove = 0; //As pieces are captured we decrement this number. If it reaches 2 and the game is not otherwise over, then we must end the game piecesAlive = 32; //Every move has a unique numeric value (1, 2, 3, 4, ect). Every time a pawn is moved or a piece is captured we // set lastNonDrawMove to the current move number. If 50 moves go by with no pawn moved or piece captured, then the game is over lastNonDrawMove = 0; //The state of a board is where every single piece is. If the same state is repeated 3 times in 1 game, then the game is over. boardStates = new Array(); //We are keeping track of the move history history = new Array(); historyText = ""; //build the board in memory without pieces. a 2D array of Tile class instances. 8X8 buildGrid(); //place the white pieces on the board placePieces("white"); //place the black pieces on the board placePieces("black"); //every piece threatens tiles. every tile knows what pieces threaten it. this is called "hotness". // we store this information, but it must be first be determined for the board layout initializeHotness(); } function getWhitePieces() { return whitePieces; } function getBlackPieces() { return blackPieces; } function traceHotness() { //for debugging purposes //this traces the current state of the board as far as threats go. it does not show where pieces are, just // what tiles are threatened, and by what color var str:String = ""; for (var j = 0; j<8; ++j) { var row:String = ""; for (var i = 0; i<8; ++i) { var tmp_tile:Tile = getTile(i, j); if (tmp_tile.isWhiteHot) { row += "W"; } else { row += "_"; } if (tmp_tile.isBlackHot) { row += "B"; } else { row += "_"; } row += "|\t"; } str += row+"\n"; str += "--------------------------------\n"; } trace(str); } function setReverse(newVal:Boolean) { //This should be called before initialize() //it affects how the board is displayed boardReverse = newVal; } function initializeHotness() { for (var j = 0; j<8; ++j) { for (var i = 0; i<8; ++i) { var currTile:Tile = getTile(i, j); expandHotness(currTile); } } } function expandHotness(currTile:Tile) { var currPiece:ChessPiece = currTile.getPiece(); currPiece.clearTileThreats(); var start_x:Number = currTile.getColumn(); var start_y:Number = currTile.getRow(); var type:String = currPiece.getType(); var color:String = currPiece.getColor(); var sign:Number; if (color == "white") { sign = 1; } else { sign = -1; } if (type == "pawn") { //pawns threaten 2 tiles var tile_a:Tile = getTile(start_x+1, start_y+sign*1); var tile_b:Tile = getTile(start_x-1, start_y+sign*1); tile_a.addThreatPiece(currPiece); tile_b.addThreatPiece(currPiece); currPiece.addTileThreat(tile_a); currPiece.addTileThreat(tile_b); // added 12-30-03 var tile_c:Tile = getTile(start_x, start_y+sign*1); tile_c.addThreatPiece(currPiece); currPiece.addTileThreat(tile_c); if (currPiece.isFirstMove) { var tile_d:Tile = getTile(start_x-1, start_y+sign*2); tile_d.addThreatPiece(currPiece); currPiece.addTileThreat(tile_d); } // end } else if (type == "king") { //top left var h_x:Number = start_x-1; var h_y:Number = start_y-1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //top var h_x:Number = start_x; var h_y:Number = start_y-1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //top right var h_x:Number = start_x+1; var h_y:Number = start_y-1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //right var h_x:Number = start_x+1; var h_y:Number = start_y; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //bottom right var h_x:Number = start_x+1; var h_y:Number = start_y+1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //bottom var h_x:Number = start_x; var h_y:Number = start_y+1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //bottom left var h_x:Number = start_x-1; var h_y:Number = start_y+1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //left var h_x:Number = start_x-1; var h_y:Number = start_y; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); } else if (type == "knight") { //11 o'clock var h_x:Number = start_x-1; var h_y:Number = start_y-2; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //1 o'clock var h_x:Number = start_x+1; var h_y:Number = start_y-2; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //2 o'clock var h_x:Number = start_x+2; var h_y:Number = start_y-1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //4 o'clock var h_x:Number = start_x+2; var h_y:Number = start_y+1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //5 o'clock var h_x:Number = start_x+1; var h_y:Number = start_y+2; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //7 o'clock var h_x:Number = start_x-1; var h_y:Number = start_y+2; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //9 o'clock var h_x:Number = start_x-2; var h_y:Number = start_y+1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); //10 o'clock var h_x:Number = start_x-2; var h_y:Number = start_y-1; var tmp_tile:Tile = getTile(h_x, h_y); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); } else if (type == "queen" || type == "rook" || type == "bishop") { var hor_vert:Boolean = false; var diag:Boolean = false; if (type == "queen") { hor_vert = true; diag = true; } else if (type == "rook") { hor_vert = true; } else if (type == "bishop") { diag = true; } if (hor_vert) { //vertical var column:Number = start_x; //go from piece to the bottom of the board if (start_y != 7) { for (var i = start_y+1; i<8; ++i) { var row:Number = i; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } } } //go from piece to top of board if (start_y != 0) { for (var i = start_y-1; i>-1; --i) { var row:Number = i; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } } } //horizontal var row:Number = start_y; //go from piece to the bottom of the board if (start_x != 7) { for (var i = start_x+1; i<8; ++i) { var column:Number = i; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } } } //go from piece to top of board if (start_x != 0) { for (var i = start_x-1; i>-1; --i) { var column:Number = i; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } } } } if (diag) { //go from piece to the bottom right of board if (start_x != 7 && start_y != 7) { var j:Number = start_y+1; for (var i = start_x+1; i<8; ++i) { var column:Number = i; var row:Number = j; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } ++j; } } //go from piece to the bottom left of board if (start_x != 0 && start_y != 7) { var j:Number = start_y+1; for (var i = start_x-1; i>-1; --i) { var column:Number = i; var row:Number = j; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } ++j; } } //go from piece to the top left of board if (start_x != 0 && start_y != 0) { var j:Number = start_y-1; for (var i = start_x-1; i>-1; --i) { var column:Number = i; var row:Number = j; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } --j; } } //go from piece to the top right of board if (start_x != 7 && start_y != 0) { var j:Number = start_y-1; for (var i = start_x+1; i<8; ++i) { var column:Number = i; var row:Number = j; var tmp_tile:Tile = getTile(column, row); tmp_tile.addThreatPiece(currPiece); currPiece.addTileThreat(tmp_tile); if (tmp_tile.isFilled) { break; } --j; } } } } } function refreshTile(tmp_tile:Tile) { //This function takes a tile and loops through all of the pieces that are threaning it and tells them // all to re-populate their threats var whiteThreats:Array = tmp_tile.getWhiteThreats(); var arr:Array = whiteThreats; for (var i = 0; i1) { //if there is more than 1 pieces threatening the king, and the king cannot move, then it is an automatic checkmate //if the king *can* move, then it is not a checkmate checkCondition1 = true; } else if (threats.length == 1) { checkCondition1 = true; checkCondition2 = true; } if (checkCondition1) { var t_x:Number = kingTile.getColumn(); var t_y:Number = kingTile.getRow(); var coordsToCheck:Array = new Array({x:t_x, y:t_y-1}, {x:t_x+1, y:t_y-1}, {x:t_x+1, y:t_y}, {x:t_x+1, y:t_y+1}, {x:t_x, y:t_y+1}, {x:t_x-1, y:t_y+1}, {x:t_x-1, y:t_y}, {x:t_x-1, y:t_y-1}); var freeSpaceExists:Boolean = false; for (var i = 0; i=0 && x<8 && y>=0 && y<8) { var tmp_tile:Tile = getTile(x, y); var hot:Boolean; if (color == "black") { hot = tmp_tile.isWhiteHot && tmp_tile.pawnCheck("white"); } else if (color == "white") { hot = tmp_tile.isBlackHot && tmp_tile.pawnCheck("black"); } if (!hot && !tmp_tile.isFilled) { //there is no checkmate //there is an empty space that is not being threatened where the king can move freeSpaceExists = true; break; } else if (!hot && tmp_tile.isFilled && tmp_tile.getPiece().getColor() != color) { //there is no checkmate //there is an enemy that the king can capture, and that tile is not threatned freeSpaceExists = true; break; } } } if (freeSpaceExists) { //The king has a free spot to which he can move //So no checkmate is possible condition1Result = false; } else { //The king cannot move anywhere condition1Result = true; } } if (checkCondition2 && condition1Result) { //This condition is called when the king cannot move anywhere and he is only being threatened by 1 piece var threatPiece:ChessPiece = threats[0]; var threatPieceTile:Tile = threatPiece.getTile(); var tileThreats_tmp:Array = threatPiece.getTileThreats(); //first, check to see if we can capture the threatPiece var attemptCapture:Boolean = false; var attemptBlock:Boolean = true; if (threatPiece.getType() == "knight") { attemptBlock = false; } var goodThreats_tmp:Array; var goodThreats:Array = new Array(); if (color == "white") { if (threatPieceTile.isWhiteHot && tmp_tile.pawnCheck("white")) { attemptCapture = true; goodThreats_tmp = threatPieceTile.getWhiteThreats(); } } else if (color == "black") { if (threatPieceTile.isBlackHot && tmp_tile.pawnCheck("black")) { attemptCapture = true; goodThreats_tmp = threatPieceTile.getBlackThreats(); } } if (attemptCapture) { //the offending piece can be captured. But let's see if it can be done without putting ourselves in check //performValidatedMove for (var i = 0; i-1; --i) { if (boardStates[i].value == str) { num = ++boardStates[i].occurences; existsYet = true; break; } } if (!existsYet) { var ob:Object = new Object(); ob.occurences = 1; ob.value = str; boardStates.push(ob); } else { if (num>=3) { returnVal = true; } } return returnVal; } function checkForStall():Boolean { var returnVal:Boolean = false; if (currentMove-lastNonDrawMove>=50) { returnVal = true; } return returnVal; } function addToHistory(starting_tile:Tile, ending_tile:Tile, placedOpponentInCheck:Boolean) { var color:String = ending_tile.getPiece().getColor(); var start:String = columnMap[starting_tile.getColumn()]+rowMap[starting_tile.getRow()]; var end:String = columnMap[ending_tile.getColumn()]+rowMap[ending_tile.getRow()]; var str:String = start+" - "+end; var baseOb:Object; var moveNum:Number; if (color == "white") { moveNum = Math.floor(currentMove/2); baseOb = new Object(); baseOb.data = new Object(); baseOb.data.white = new Object(); baseOb.data.white.start = start; baseOb.data.white.end = end; if (placedOpponentInCheck) { str += "+"; } baseOb.label = str; history[moveNum] = baseOb; latestHistoryAddition = str; historyText += latestHistoryAddition; } else { moveNum = Math.floor((currentMove-1)/2); baseOb = history[moveNum]; baseOb.data.black = new Object(); baseOb.data.black.start = start; baseOb.data.black.end = end; if (placedOpponentInCheck) { str += "+"; } str = " "+str; baseOb.label += str; latestHistoryAddition = str+"
"; historyText += latestHistoryAddition; } } function getHistory():String { return historyText; } function getLatestHistoryAddition():String { return latestHistoryAddition; } function move(starting_tile:Tile, ending_tile:Tile, pawnPromotion:Boolean, newType:String):Object { var t = getTimer(); var chessPiece:ChessPiece = starting_tile.getPiece(); var color:String = chessPiece.getColor(); var result:Object = new Object(); var checkMate:Boolean = false; var staleMate:Boolean = false; var draw:Boolean = false; var stall:Boolean = false; var insufficient:Boolean = false; var promote:Boolean = false; var pawnToPromote:ChessPiece; var castled:Boolean = false; var castleInfo:Tile; var enpassanted:Boolean = false; var enpassantedPieceID:String; //------------------------------------ //start validation //This just checks to see if the move can happen. No visual changes occur. Piece is not moved in memory. var validate_basic_move:Boolean; if (pawnPromotion != true) { validate_basic_move = validateBasicMove(starting_tile, ending_tile); } else { validate_basic_move = false; } var valid:Boolean; if (validate_basic_move) { //perform move in memory valid = performValidatedMove(starting_tile, ending_tile, false); trace("---valid---") trace(valid) if (castling) { //The "castling" move was just invoked performValidatedMove(castlingInfo[1][0], castlingInfo[1][1], false); castled = true; castleInfo = castlingInfo[1][1]; } if (enpassant && valid) { var tmp_piece:ChessPiece = enpassantTile.getPiece(); tmp_piece.clearTileThreats(); enpassanted = true; enpassantedPieceID = enpassantTile.getPiece().getID(); enpassantTile.capturePiece(); enpassantTile.removePiece(); } //if valid == false, then the player that moved placed his own king in check if (!valid) { //reverse the move if person put his own king in check performValidatedMove(ending_tile, starting_tile, true); } } if (pawnPromotion) { chessPiece.changeType(newType); promotePawn = false; valid = true; expandHotness(starting_tile); } //end validation //------------------------------------ if (valid) { //The move was 100% valid. It has been moved in memory if (!pawnPromotion) { ++currentMove; } var capturedPieceID:String = ending_tile.getCapturedPieceID(); var pieceCaptured:Boolean = ending_tile.removeCapturedPiece(); if (chessPiece.getType() == "pawn" || pieceCaptured) { lastNonDrawMove = currentMove; } if (promotePawn) { promote = true; pawnToPromote = ending_tile.getPiece(); } if (pieceCaptured) { --piecesAlive; if (piecesAlive == 2) { insufficient = true; } } if (enpassant) { enpassantTile.removeCapturedPiece(); } //traceHotness(); chessPiece.setLastMove(currentMove); var placedOpponentInCheck:Boolean = false; if (color == "white" && blackKing.getTile().isWhiteHot && blackKing.getTile().pawnCheck("white")) { //due to the move, the opponent is now in check. determine if it is a check mate placedOpponentInCheck = true; checkMate = checkForCheckmate("black"); } else if (color == "black" && whiteKing.getTile().isBlackHot && whiteKing.getTile().pawnCheck("black")) { //due to the move, the opponent is now in check. determine if it is a check mate placedOpponentInCheck = true; checkMate = checkForCheckmate("white"); } //Render it to screen if (castling) { //Caslting was used, so render the chess piece var castlePiece:ChessPiece = castlingInfo[1][1].getPiece(); var endCastleTile:Tile = castlingInfo[1][1]; castlePiece.setPosition(endCastleTile.getColumn(), endCastleTile.getRow()); } if (!checkMate && !placedOpponentInCheck) { var tmp_color = color == "white" ? "black" : "white"; staleMate = checkForStaleMate(tmp_color); draw = generateState(); stall = checkForStall(); } initializeHotness(); chessPiece.setPosition(ending_tile.getColumn(), ending_tile.getRow()); addToHistory(starting_tile, ending_tile, placedOpponentInCheck); } else { chessPiece.setPosition(starting_tile.getColumn(), starting_tile.getRow()); } result.success = valid; result.history = history; result.checkMate = checkMate; result.staleMate = staleMate; result.draw = draw; result.stall = stall; result.insufficient = insufficient; result.promote = promote; result.pieceCaptured = pieceCaptured; result.capturedPieceID = capturedPieceID; result.castled = castled; result.enpassanted = enpassanted; result.color = color; if (castled) { result.castleInfo = castleInfo; } if (enpassanted) { result.enpassantedPieceID = enpassantedPieceID; } if (promote) { result.pawnToPromote = pawnToPromote; } return result; } function validateBasicMove(starting_tile, ending_tile):Boolean { if (starting_tile == ending_tile) { return false; } castling = false; enpassant = false; promotePawn = false; var chessPiece:ChessPiece = starting_tile.getPiece(); var enemyPiece:ChessPiece = ending_tile.getPiece(); var containsPiece:Boolean = ending_tile.isFilled; var color:String = chessPiece.getColor(); var enemyColor:String = enemyPiece.getColor(); var type:String = chessPiece.getType(); var returnVal:Boolean = false; var start_x:Number = starting_tile.getColumn(); var start_y:Number = starting_tile.getRow(); var end_x:Number = ending_tile.getColumn(); var end_y:Number = ending_tile.getRow(); var containsEnemy:Boolean = containsPiece && enemyColor != color; var containsSameColor:Boolean = containsPiece && enemyColor == color; if (type == "pawn") { var firstMove:Boolean = chessPiece.isFirstMove; var sign:Number; if (color == "white") { sign = 1; } else if (color == "black") { sign = -1; } var next_y:Number = start_y+sign; var nextTile:Tile = getTile(start_x, next_y); if (start_x == end_x && (end_y-start_y == sign*1 || (end_y-start_y == sign*2 && firstMove && !nextTile.isFilled)) && !containsPiece) { //going straight ahead returnVal = true; } else if (Math.abs(start_x-end_x) == 1 && end_y-start_y == sign*1 && containsEnemy) { //going diagonal to capture a piece returnVal = true; } else if (Math.abs(start_x-end_x) == 1 && end_y-start_y == sign*1 && !containsPiece) { //using enpassant var en_x:Number = end_x; var en_y:Number = start_y; var enTile:Tile = getTile(en_x, en_y); var enPiece:ChessPiece = enTile.getPiece(); enpassantTile = enTile; if (enPiece.getType() == "pawn" && currentMove == enPiece.getLastMove() && ((en_y == 3 && sign == -1) || (en_y == 4 && sign == 1))) { returnVal = true; enpassant = true; } } if (returnVal && ((color == "white" && end_y == 7) || (color == "black" && end_y == 0))) { promotePawn = true; } } else if (type == "king") { var firstMove:Boolean = chessPiece.isFirstMove; if ((Math.abs(start_y-end_y) == 1 && start_x-end_x == 0) || (Math.abs(start_x-end_x) == 1 && (Math.abs(start_y-end_y) == 1 || start_y-end_y == 0))) { if (!containsSameColor) { returnVal = true; } } else if (firstMove && start_y == end_y) { //check for castling //need to make sure king does not move through check! if (end_x == 1) { var rookTile:Tile = getTile(0, start_y); var rook:ChessPiece = rookTile.getPiece(); var tile1:Tile = getTile(2, start_y); var tile2:Tile = getTile(1, start_y); var tileCondition:Boolean; if (color == "white") { tileCondition = !tile1.isFilled && !(tile1.isBlackHot && tile1.pawnCheck("black")) && !tile2.isFilled && !(tile2.isBlackHot && tile2.pawnCheck("black")); } else if (color == "black") { tileCondition = !tile1.isFilled && !(tile1.isWhiteHot && tile1.pawnCheck("white")) && !tile2.isFilled && !(tile2.isWhiteHot && tile2.pawnCheck("white")); } if (rook.isFirstMove && tileCondition) { castling = true; var rook_dest:Tile = getTile(2, start_y); castlingInfo = [[starting_tile, ending_tile], [rookTile, rook_dest]]; returnVal = true; } } else if (end_x == 5) { var rookTile:Tile = getTile(7, start_y); var rook:ChessPiece = rookTile.getPiece(); var tile1:Tile = getTile(6, start_y); var tile2:Tile = getTile(5, start_y); var tileCondition:Boolean; if (color == "white") { tileCondition = !tile1.isFilled && !(tile1.isBlackHot && tile1.pawnCheck("black")) && !tile2.isFilled && !(tile2.isBlackHot && tile1.pawnCheck("black")); } else if (color == "black") { tileCondition = !tile1.isFilled && !(tile1.isWhiteHot && tile1.pawnCheck("white")) && !tile2.isFilled && !(tile2.isWhiteHot && tile2.pawnCheck("white")); } if (rook.isFirstMove && tileCondition) { castling = true; var rook_dest:Tile = getTile(4, start_y); castlingInfo = [[starting_tile, ending_tile], [rookTile, rook_dest]]; returnVal = true; } } } } else if (type == "knight") { if ((Math.abs(start_x-end_x) == 1 && Math.abs(start_y-end_y) == 2) || (Math.abs(start_x-end_x) == 2 && Math.abs(start_y-end_y) == 1)) { if (!containsSameColor) { returnVal = true; } } } else if (type == "queen" || type == "rook" || type == "bishop") { var hor_vert:Boolean = false; var diag:Boolean = false; if (type == "queen") { hor_vert = true; diag = true; } else if (type == "rook") { hor_vert = true; } else if (type == "bishop") { diag = true; } if (start_x == end_x && hor_vert) { //vertical only move var sign:Number; if (end_y-start_y>0) { sign = 1; } else { sign = -1; } for (var i = start_y+sign; i-end_y != sign; i += sign) { var currTile:Tile = getTile(start_x, i); var currPiece:ChessPiece = currTile.getPiece(); var filled:Boolean = currTile.isFilled; if (filled && i != end_y) { //a spot along the way is filled with the same color break; } else if (filled && i == end_y && currPiece.getColor() != color) { //the destination is filled with the same color returnVal = true; break; } else if (i == end_y && !filled) { //clear path returnVal = true; } } } else if (start_y == end_y && hor_vert) { //horizontal only move var sign:Number; if (end_x-start_x>0) { sign = 1; } else { sign = -1; } for (var i = start_x+sign; i-end_x != sign; i += sign) { var currTile:Tile = getTile(i, start_y); var currPiece:ChessPiece = currTile.getPiece(); var filled:Boolean = currTile.isFilled; if (filled && i != end_x) { //a spot along the way is filled with the same color break; } else if (filled && i == end_x && currPiece.getColor() != color) { //the destination is filled with the same color returnVal = true; break; } else if (i == end_x && !filled) { //clear path returnVal = true; } } } else if (Math.abs(start_y-end_y) == Math.abs(start_x-end_x) && diag) { //diagonal only move var sign_x:Number; var sign_y:Number; if (end_x-start_x>0) { sign_x = 1; } else { sign_x = -1; } if (end_y-start_y>0) { sign_y = 1; } else { sign_y = -1; } var j:Number = start_y+sign_y; for (var i = start_x+sign_x; i-end_x != sign_x; i += sign_x) { var currTile:Tile = getTile(i, j); var currPiece:ChessPiece = currTile.getPiece(); var filled:Boolean = currTile.isFilled; if (filled && i != end_x) { //a spot along the way is filled with the same color break; } else if (filled && i == end_x && currPiece.getColor() != color) { //the destination is filled with the same color returnVal = true; break; } else if (i == end_x && !filled) { //clear path returnVal = true; } j += sign_y; } } } return returnVal; } function setBoard(theClip:MovieClip) { board = theClip; } function setTileWidth(theWidth:Number) { tileWidth = theWidth; } function setPieceIdentifier(theID:String) { pieceIdentifier = theID; } function newClip() { ++depth; var newName:String = "piece"+depth.toString(); var clip:MovieClip = board.attachMovie(pieceIdentifier, newName, depth); return clip; } function placePieces(col:String) { var row:Number; var pieceArray:Array; if (col == "white") { row = 1; whitePieces = new Array(); pieceArray = whitePieces; } else { row = 6; blackPieces = new Array(); pieceArray = blackPieces; } //------------- //pawns for (var i = 0; i<8; ++i) { var column:Number = i; ++depth; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("pawn", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); } //------------- if (col == "white") { row = 0; } else { row = 7; } //rooks var column:Number = 0; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("rook", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); var column:Number = 7; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("rook", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); //------------- //knights var column:Number = 1; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("knight", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); var column:Number = 6; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("knight", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); //------------- //bishops var column:Number = 2; var tempPiece:ChessPiece = new ChessPiece("bishop", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); var clip:MovieClip = newClip(); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); var column:Number = 5; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("bishop", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); //------------- //queen var column:Number = 4; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("queen", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); //------------- //king var column:Number = 3; var clip:MovieClip = newClip(); var tempPiece:ChessPiece = new ChessPiece("king", col); tempPiece.setID(++pieceID); tempPiece.setReverse(boardReverse); tempPiece.setClip(clip); tempPiece.setTileWidth(tileWidth); tempPiece.setPosition(column, row); getTile(column, row).addPiece(tempPiece); pieceArray.push(tempPiece); if (col == "white") { whiteKing = tempPiece; } else if (col == "black") { blackKing = tempPiece; } } function getTile(column:Number, row:Number):Tile { return grid[column][row]; } function buildGrid() { grid = new Array(); for (var i = 0; i<8; ++i) { for (var j = 0; j<8; ++j) { if (j == 0) { grid[i] = new Array(); } var temp:Tile = new Tile(i, j); grid[i][j] = temp; } } } }