/* This implements a chessboard. The pieces are held in an array, one array entry for each piece, 32 pieces. Each piece has a symbol in the library. The board is 8x14-- an 8x8 chessboad, with 3 extra columns on each side for captured pieces. There is an array that stores which piece is in a particular square. Pick up pieces by clicking on them Put them down by clicking. No attempt to know the rules of chess movement. To do: network connectivity */ package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.ui.Keyboard; public class chessboard extends Sprite { private var squares:Array; // contents of each square private var pieces:Array; // the set of pieces private var pieceHeld: int; // the piece you're holding private const numColumns = 14; // size of the board private const numRows = 8; private const numPieces = 16; // number of pieces each side gets private const squareSize = 45; // size of a square in pixels private const halfSquareSize = 23; // size of a square in pixels public function chessboard() { var i, pn:int; // "squares" says what is in what square on the board squares = new Array(numRows*numColumns); fillsquares(); pieceHeld = -1; // the pieces are treated all the same // except for just below. // this sets up the pieces to look like chessmen // The names of the symbols are set in the library pieces = new Array(32); pieces[0] = new wtRook(); pieces[1] = new wtKnight(); pieces[2] = new wtBishop(); pieces[3] = new wtQueen(); pieces[4] = new wtKing(); pieces[5] = new wtBishop(); pieces[6] = new wtKnight(); pieces[7] = new wtRook(); pieces[16] = new bkRook(); pieces[17] = new bkKnight(); pieces[18] = new bkBishop(); pieces[19] = new bkQueen(); pieces[20] = new bkKing(); pieces[21] = new bkBishop(); pieces[22] = new bkKnight(); pieces[23] = new bkRook(); for (i=0; i<8; ++i) { pieces[i+8] = new wtPawn(); pieces[i+24] = new bkPawn(); } // put the pieces right in the middle of the squares for (i=0; i<(numRows*numColumns); ++i) { pn = squares[i]; // is there a piece here? if (pn!=-1) { pieces[pn].x = squareX(i); // move the symbol to here pieces[pn].y = squareY(i); addChild(pieces[pn]); } } // listen for mouse clicks and mouse moves stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); } // this function sorts the pieces for a new game by // setting the numbers in "squares" // if you don't understand it, consult the rules of chess. private function fillsquares() { var i:int; // fill the array with -1s => all empty for (i=0; i<(numRows*numColumns); ++i) { squares[i] = -1; } // the rows are numbered 0-7, with the 7th row on top // for (i=0; i<8; ++i) { squares[i+(numColumns*7)+3] = i; // white back row squares[i+(numColumns*6)+3] = i+8; // white pawns squares[i+(numColumns*1)+3] = i+24; // black pawns squares[i+(numColumns*0)+3] = i+16; // black back row } } // given location on screen, return which square it's in, or -1 // this number is an index into the "squares" array private function insquare(xIn:int, yIn:int):int { var dx, dy, res:int; //trace("insquare results input:"); //trace(xIn, yIn); if (xIn<13.0) return -1; // off to the left if (yIn<17.0) return -1; // below if (xIn>648.0) return -1; // off to the right if (yIn>375.0) return -1; // above //trace("on board"); dx = int(xIn-13); dy = int(yIn-17); //trace("flipped coords"); //trace(dx, dy); dx = int(Math.floor(dx/squareSize)); dy = int(Math.floor(dy/squareSize)); res = dy*numColumns+dx; //trace("square coords and result"); //trace(dx, dy, res); return (res); // "res" contains the square you clicked on } // given a square number, return the x coord of the center of the square private function squareX(which:int): int { return (39+((which%numColumns)*squareSize)); } private function squareY(which:int): int { return int(40+(Math.floor(which/numColumns)*squareSize)); } // when the user clicks, find out which square got clicked in // then see whether there's a piece there. // if you're carrying a piece, put it in the square // if there was a piece there, pick it up // carrying *and* there's a peice? drop one, pick up the other private function mouseDownHandler(event:MouseEvent):void { var foundPiece, squareNum:int; // find the square you clicked on squareNum =insquare(event.stageX, event.stageY); // trace("in square number", squareNum); // set "foundPiece" to be the piece in the square you clicked on if (squareNum!=-1) { foundPiece = squares[squareNum]; } else { foundPiece=-1; // or -1 if it's empty } // trace("foundPiece, pieceHeld:", foundPiece, pieceHeld); if ((pieceHeld==-1) && (foundPiece==-1)) { // have nothing, clicked on an empty square-- do nothing } else if ((pieceHeld==-1) && (foundPiece!=-1)) { // carrying nothing, clicked on a piece pieceHeld = foundPiece; // pick it up squares[squareNum] = -1; // make the square empty } else if ((pieceHeld!=-1) && (foundPiece==-1)) { // if you're carrying a piece, and the square was empty, put it in the square squares[squareNum] = pieceHeld; pieces[pieceHeld].x = squareX(squareNum); pieces[pieceHeld].y = squareY(squareNum); pieceHeld = -1; // you are now carrying nothing } else if ((pieceHeld!=-1) && (foundPiece!=-1)) { // if you're carrying a piece, and the square was empty, trade squares[squareNum] = pieceHeld; pieces[pieceHeld].x = squareX(squareNum); pieces[pieceHeld].y = squareY(squareNum); pieceHeld = foundPiece; } } // when the mouse moves, and you're holding a piece, // move the piece to follow the mouse private function mouseMoveHandler(event:MouseEvent):void { if (pieceHeld!=-1) { pieces[pieceHeld].x = event.stageX; pieces[pieceHeld].y = event.stageY; } } } }