ActionScript 3.0 Design Patterns

(Chris Devlin) #1

480 | Chapter 13: Symmetric Proxy Pattern


the comment line (//Abstract methods) indicating abstract methods is a similar


reminder that the abstract methods are not real abstract methods, because they’re


not supported in ActionScript 3.0.


Methods


Of the three methods in theRefereeclass, the first needs to be very flexible because it


will return the winner of the game. The real implementation of the method will lie in


any subclass that describes a game. ThedoWinnermethod is most likely to be part of


a subclass specifying the rules of the game.


function doWinner(p1Move:String,p2Move:String):String
{
return winner;
}

It includes parameters for moves by both players. In the context ofRPS, where the


game has only a single move by each player, the moves represent the endgame condi-


tions. In other games, though, theRefereemay need to call for moves every round to


accumulate information about win conditions.


The next method is designed to display outcomes, requesting both a reference to a


text field and a string.


function displayResults(displayWindow:DynamicText,outcome:String):void
{
displayWindow.setMove(outcome);
}

In the context ofRPSand most games, this method has two different roles. On one


hand, as part of the template method, it displays who has won. However, it can also


be used to display information independent of the template method. Keeping in


mind that moves inRPSare simultaneous, neither player can see the other player’s


move until both have made their moves. So this method can also be used to display


other information such as the opponent’s move any time it’s appropriate to do so.


The third method in theRefereeclass is to reset all of the values to the start condi-


tions—setting up the chess pieces in their original positions, so to speak. This


method has a housekeeping character, but it’s essential if you’re going to play the


game more than once without reloading it.


function resetGame(movecheck:Array):void
{
for (var r:uint =0; r< movecheck.length; r++)
{
movecheck[r]=false;
}
}

By keeping track of the moves in an array, resetting a game is made both easier and


more flexible. With only two moves, the function could be written to reset two

Free download pdf