/* This implements a board and pieces for 9-man morris. The board is 11 columns by 7 rows */ package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.ui.Keyboard; public class morris extends Sprite { private var squares:Array; // contents of the board private var pieces:Array; // an array of symbols, representing the pieces private var pieceHeld: int; // the piece you're holding public function morris() { var i, pn:int; squares = new Array(77); // 7 rows * 11 columns setUpBoard(); // set up the board to start pieceHeld = -1; // set up the symbols; they will be the game pieces pieces = new Array(18); for (i=0; i<9; ++i) { pieces[i] = new wtPiece(); // pieces 0-8 are white pieces[i+9] = new bkPiece(); // 9-17 are black } // put the symbols on the stage, but make them act like they're on a board for (i=0; i<77; ++i) { // look at all the squares on the board pn = squares[i]; // is there a piece here? if (pn!=-1) { pieces[pn].x = boxX(i); // move the symbol to here pieces[pn].y = boxY(i); addChild(pieces[pn]); } } // listen for mouse clicks and mouse moves stage.addEventListener(MouseEvent.CLICK, mouseClickHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } // this function sorts the pieces for a new game by // setting the numbers in "squares" private function setUpBoard() { var i:int; // first, clear the board for (i=0; i<77; ++i) { squares[i] = -1; } // put 7 pieces on the left and rightmost columns for (i=0; i<7; ++i) { squares[i*11] = i; // pieces 0-6 (white) squares[i*11+10] = i+9; // 9-15 (black) } // put on 2 more-- it's 9-man morris, right? for (i=0; i<2; ++i) { squares[i*11+1] = i+7; // pieces 7, 8 squares[i*11+9] = i+16; // 16, 17 } } // given location on screen, return which square it's in, or -1 // this number is an index into the "squares" array private function inBox(xIn:int, yIn:int):int { var dx, dy, res:int; //trace("inBox results input:"); //trace(xIn, yIn); //trace("on board"); dx = int(xIn); dy = int(400-yIn); //trace("flipped coords"); //trace(dx, dy); dx = int(Math.floor(dx/50)); dy = int(Math.floor(dy/50))-1; res = dy*11+dx; //trace("box coords and result"); //trace(dx, dy, res); return (res); } // these functions help us drop pieces right onto the center of the board // given a box number, return the x coord of the center of the box private function boxX(which:int): int { return ((which%11)*50)+25; } // same, but for y. private function boxY(which:int): int { return 350-(int(Math.floor(which/11))*50)-25; } // when the user clicks, find out which box got clicked in // then see whether there's a piece there. // if you're carrying a piece, put it in the box // if there was a piece there, pick it up private function mouseClickHandler(event:MouseEvent):void { var foundPiece, boxNum:int; // find the box you clicked on boxNum =inBox(event.stageX, event.stageY); // trace("in box number", boxNum); // trace(boxNum); foundPiece = -1; if (boxNum!=-1) { // remember the piece in the box foundPiece = squares[boxNum]; // trace("foundPiece, pieceHeld:", foundPiece, pieceHeld); // if you're carrying a piece, put it in the box squares[boxNum] = pieceHeld; if (pieceHeld!=-1) { pieces[pieceHeld].x = boxX(boxNum); pieces[pieceHeld].y = boxY(boxNum); // trace("piece dropped"); } // if there was a piece in the box, pick it up pieceHeld = foundPiece; //if (foundPiece!=-1) { // trace("piece picked up"); //} } } // 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; } } } }