10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• Reset the game variables to the beginning conditions. Because the players<br />

have been keeping track of who has moved, which moves were selected, and<br />

which player was declared the winner, all the variables keeping track of<br />

these different states have to be cleared.<br />

Each operation will be cast as a method and then arranged into an order that constitutes<br />

the template method. Following the Hollywood Principle (see Chapter 9), the<br />

Referee instance will make calls on the Player objects.<br />

Now that we have a referee, we need to look at the relationship between the players<br />

and the referee. Figure 13-2 is an object diagram for a two-player game with a referee:<br />

Figure 13-2. Referee and players<br />

Just like a non-virtual game, the single referee makes decisions about the game out<strong>com</strong>e<br />

for two or more players. At this point, the game is immaterial—it could be any<br />

game. Looking at the object diagram, it’s not difficult to imagine two or more players<br />

interacting on a single host (<strong>com</strong>puter). In fact, if the goal were to create a game<br />

where one of the players is the <strong>com</strong>puter and the other’s a user interacting through a<br />

UI, our job would be done. In a game of Rock, Paper, Scissors, a random move generator<br />

could easily be one of the two players. However, because we want to play the<br />

game over the Internet, we have a few more steps.<br />

The Internet, Proxies, and Players<br />

As soon as we introduce remote players over the Internet, everything changes. Each<br />

player makes a move, that move is sent over the Internet, and the referee decides the<br />

out<strong>com</strong>e, displays it, and then cleans everything up. With serial turn taking, the<br />

problem may not be as daunting because after each move, both players can see the<br />

other’s move. The referee can wait until the first win condition is met and then send<br />

messages to all players. However, this can get messy because you have to decide<br />

where the referee’s going to reside. The referee could be placed on the host with the<br />

first player to start. The second player would be referenced through a proxy.<br />

Another solution would be to place the referee on its own server, and, while workable,<br />

this requires that the moves travel over the Internet twice. Also, depending on<br />

the server, the basic game design may have to be changed to ac<strong>com</strong>modate what the<br />

referee looks like.<br />

472 | Chapter 13: Symmetric Proxy Pattern<br />

Referee<br />

Player 1 Player 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!