----------------- Part 6.1 ----------------- Code Sample 6.1.1 " function attemptMovePiece(%x, %y) { // check if the move is legal %legal = ClientCheckerBoard.isLegalMove($clientSelectedX, $clientSelectedY, %x, %y); // here we check if we are moving to the same position, if so we want to reset our piece if(%legal $= "same") { // we grab the tile's position %pos = ClientCheckerBoard.getTilePosition($clientSelectedX, $clientSelectedY); // we set the layer of the checker and its eyes $clientSelectedPiece.setLayer($layers::checkerLayer); // dismount the checker from the moust object $clientSelectedPiece.dismount(); // reset the checker's position to the original position $clientSelectedPiece.setPosition(%pos); // set selected to false $clientSelected = false; } else if(%legal) { // request a move from the server commandToServer('attemptMovePiece', %x, %y); } } " Code Sample 6.1.2 " function serverCmdAttemptMovePiece(%client, %x, %y) { // pass the attempted move values on to the server function serverAttemptMovePiece(%client, %x, %y); } " Code Sample 6.1.3 " function serverAttemptMovePiece(%client, %x, %y) { // grab whether the move is legal %legal = ServerCheckerBoard.isLegalMove($playersTurn.selectedX, $playersTurn.selectedY, %x, %y); // init jumped to false %jumped = false; // if we recieve more than just a 0 or 1 for true or false then thats the position of a jumped piece if(getWord(%legal, 1) !$= "") { // dived up the legal response since this is now a piece location that was jumped %jumpedX = getWord(%legal, 0); %jumpedY = getWord(%legal, 1); // grab the jumped piece %jumpedPiece = ServerCheckerBoard.getPiece(%jumpedX, %jumpedY); // grab the other player %otherPlayer = getOtherPlayer(%client); // remove the piece ServerCheckerBoard.removePiece(%jumpedX, %jumpedY); // tell the client's to remove the piece commandToClient(%client, 'destroyPiece', %jumpedX, %jumpedY); commandToClient(%otherPlayer, 'destroyPiece', %jumpedX, %jumpedY); // decrement the proper count if(%jumpedPiece == $red || %jumpedPiece == $redKing) { $redCount--; } else { $blueCount--; } // check if this means the game is over serverEndGameCheck(); // togle jumped to true %jumped = true; } // check if the move is legal if(%legal) { // grab the type of piece that is selected %type = ServerCheckerBoard.getPiece($playersTurn.selectedX, $playersTurn.selectedY); // set the proper piece settings ServerCheckerBoard.setPiece($playersTurn.selectedX, $playersTurn.selectedY, $none); ServerCheckerBoard.setPiece(%x, %y, %type); // grab the other player %otherPlayer = getOtherPlayer(%client); // tell the client's to move the piece since this was a legal move commandToClient(%otherPlayer, 'moveCheckerPiece', %type, $playersTurn.selectedX, $playersTurn.selectedY, %x, %y); commandToClient(%client, 'movePieceResponse', %x, %y, $yes); // reset the player's selection $playersTurn.selectedX = ""; $playersTurn.selectedY = ""; $playersTurn.hasSelected = false; // if this wasn't a double jump then we just swap turns swapTurns(); } } " Code Sample 6.1.4 " function clientCmdMovePieceResponse(%x, %y, %answer) { // here is the server's response to use trying to move a piece, // if its a no then we tell the client, if yes then we move it if(!%answer) { MessageBoxOK("You cannot move...", "You cannot move to the selected spot!", ""); } else { clientMoveSelectedPiece(%x, %y); } } " Code Sample 6.1.5 " function clientMoveSelectedPiece(%x, %y) { // grab the tile's position from logical to world %pos = ClientCheckerBoard.getTilePosition(%x, %y); // dismount the selection from the mouse and set its position to // the new spot $clientSelectedPiece.dismount(); $clientSelectedPiece.setPosition(%pos); // we set the layer of the checker and its eye's $clientSelectedPiece.setLayer($layers::checkerLayer); // grab the image and reset it, also set the new image %piece = ClientCheckerBoard.images[$clientSelectedX,$clientSelectedY]; ClientCheckerBoard.images[$clientSelectedX,$clientSelectedY] = ""; ClientCheckerBoard.images[%x,%y] = %piece; // grab the object's type %type = ClientCheckerBoard.getPiece($clientSelectedX, $clientSelectedY); // set the pieces position ClientCheckerBoard.setPiece($clientSelectedX, $clientSelectedY, $none); ClientCheckerBoard.setPiece(%x, %y, %type); // reset the client's selection $clientSelectedX = ""; $clientSelectedY = ""; $clientSelected = false; } " Code Sample 6.1.6 " function clientCmdMoveCheckerPiece(%type, %fromX, %fromY, %x, %y) { // this moves the piece directly to a new location clientMovePiece(%type, %fromX, %fromY, %x, %y); } function clientCmdDestroyPiece(%x, %y) { // this destroy's a checker piece clientDestroyPiece(%x, %y); } " Code Sample 6.1.7 " function clientMovePiece(%type, %fromX, %fromY, %x, %y) { // grab the tile's position from logical to world %pos = ClientCheckerBoard.getTilePosition(%x, %y); // get the piece's image %piece = ClientCheckerBoard.images[%fromX, %fromY]; // set the position of our piece %piece.setPosition(%pos); // reset the image in the previous slot and set the new one ClientCheckerBoard.images[$clientSelectedX,$clientSelectedY] = ""; ClientCheckerBoard.images[%x,%y] = %piece; // reset the piece in the previous slot and set the new one ClientCheckerBoard.setPiece(%fromX, %fromY, $none); ClientCheckerBoard.setPiece(%x, %y, %type); } " Code Sample 6.1.8 " function clientDestroyPiece(%x, %y) { // grab the team and the image data of the piece // to be destroyed %team = ClientCheckerBoard.getPiece(%x, %y); %image = ClientCheckerBoard.images[%x, %y]; // check which team the piece is if(%team == $red) { // remove the piece from the team's container ClientCheckerBoard.redPieces.remove(%image); } else if(%team == $blue) { // remove the piece from the team's container ClientCheckerBoard.bluePieces.remove(%image); } // remove the piece image from the image data ClientCheckerBoard.images[%x, %y] = ""; // remove the piece from the board data ClientCheckerBoard.removePiece(%x, %y); %image.safeDelete(); } " Code Sample 6.1.9 " function swapTurns() { // get the other player %player = getOtherPlayer($playersTurn); // set the turn to the other player setTurn(%player); } "