Design Patterns for Games Proceedings of the 33 rd SIGCSE technical symposium on Computer Science Education Melisa Tyira SE510
Presentation Topics Design Patterns Overview Design Patterns Overview Intended Purpose of this article Intended Purpose of this article Design Patterns discussed in this article Design Patterns discussed in this article Generalizations Generalizations The Model The Model The Strategy Pattern The Strategy Pattern The State Pattern The State Pattern The Visitor Pattern The Visitor Pattern Min-Max Principle Min-Max Principle Model View Controller Pattern Model View Controller Pattern Tic-Tac-Toe Example Tic-Tac-Toe Example Conclusion Conclusion Questions Questions
Design Patterns Overview Definition: describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution over and over, without ever doing it the same way twice.
Four Essential Elements of a Design Pattern Pattern Name - increases our design vocabulary Pattern Name - increases our design vocabulary Problem – describes when to apply the pattern Problem – describes when to apply the pattern Solution – describes the elements that make up the design Solution – describes the elements that make up the design Consequences – the results and trade-offs of applying the pattern Consequences – the results and trade-offs of applying the pattern
Purpose of Design Patterns Creational – concern the process of object creation Creational – concern the process of object creation Structural – deal with the composition of classes or objects Structural – deal with the composition of classes or objects Behavioral – characterize the ways in which classes or objects interact and distribute responsibility Behavioral – characterize the ways in which classes or objects interact and distribute responsibility
Intended Purpose of Article This article’s intent is to describe the apparent benefits of using design patterns to create two player games that can be easily modified, upgraded, or adapted. While using design patterns to create a design at an abstract level, code can be easily generated and reused.
Design Patterns Discussed in this Article Model View Controller Pattern – the game model, the user interface and the controller between the two Model View Controller Pattern – the game model, the user interface and the controller between the two State Pattern – state of the game board State Pattern – state of the game board Visitor Pattern – provides methods that correspond to various states of the game board Visitor Pattern – provides methods that correspond to various states of the game board Strategy Pattern – implements the min-max algorithm Strategy Pattern – implements the min-max algorithm Command Pattern – communication between the model and the view Command Pattern – communication between the model and the view
Generalizations Two-dimensional game boards Two-dimensional game boards Two Players Two Players Rules of the game define the legal board configurations plus the winning and the draw configurations Rules of the game define the legal board configurations plus the winning and the draw configurations
The Model IBoardModel – interface that abstractly “knows” how to transition from one legal board configuration to another and will reject a request for an illegal move. Each representation represents different types of games INextMoveStrategy – an interface that contains the algorithms used to compute the next move from a board configuration
IBoardModelINextMoveStrategy GameModel
The Strategy Pattern Described by the relationship between GameModel and INextMoveStrategy Described by the relationship between GameModel and INextMoveStrategy Separation of the rules of the game (IBoardModel) and the strategy to make a move (INextMoveStrategy) provides flexibility to reuse the same strategy for different types of games Separation of the rules of the game (IBoardModel) and the strategy to make a move (INextMoveStrategy) provides flexibility to reuse the same strategy for different types of games
The State Pattern Draw Game Player 1 Wins Player 0 Wins Terminal States Non-Terminal StateNo Winner Yet Valid Move State Invalid Move State
The state pattern is used to model both concrete states and superstates The state pattern is used to model both concrete states and superstates Superstates are depicted as abstract superclasses of the concrete states they contain Superstates are depicted as abstract superclasses of the concrete states they contain States and Superstates are contained in an interface, IBoardState States and Superstates are contained in an interface, IBoardState The State Pattern (Cont.)
The Visitor Pattern Used to provide state-dependent abstract behaviors Used to provide state-dependent abstract behaviors The interface, IBoardStatusVisitor contains methods corresponding to the various states of the game board and the game board provides the means to execute the visitors The interface, IBoardStatusVisitor contains methods corresponding to the various states of the game board and the game board provides the means to execute the visitors Only the method associated with the current state of the board is called when the visitor is accepted Only the method associated with the current state of the board is called when the visitor is accepted
Logic (State Pattern and Visitor Pattern) Algorithm to determine the state is executed Visitor’s appropriate method is called Virtual State Virtual State – the visitor’s method is called that corresponds to the state that was determined, without setting the system into a static concrete state
Min-Max Principle V(s) computes the value of a state s V(s) computes the value of a state s V(s) = 1, s is a winning state for that player 1, s is a winning state for that player 0, s is a draw state 0, s is a draw state -1, s is a losing state for that player -1, s is a losing state for that player No Winner Yet: No Winner Yet: max[V(c) | c is a child valid move state of s] if that player moves next max[V(c) | c is a child valid move state of s] if that player moves next min[V(c) | c is a child valid move state of s] if the other player moves next min[V(c) | c is a child valid move state of s] if the other player moves next
Model View Controller Pattern Model Three public methods Must be able to accept a request to make a move by a player to a particular location on the board Must be able to accept a request to make a move by a player to a particular location on the board Must reset to an initial condition Must reset to an initial condition Must be told which player it represents Must be told which player it representsView Four public methods Must be able to be notified of a valid move Must be able to be notified of a valid move Must be told that a specific player has won Must be told that a specific player has won Must be told that the game ended in a draw Must be told that the game ended in a draw Must reset to an initial condition Must reset to an initial condition Needs mechanism for communicating a rejected move
Tic-Tac-Toe Example Game Model – two player board game two player board game does not contain code specific to Tic-Tac-Toe does not contain code specific to Tic-Tac-Toe Moderates the interactions between the board (IBoardModel) and the strategy (INextMoveStrategy) Moderates the interactions between the board (IBoardModel) and the strategy (INextMoveStrategy) States – Terminal Superstate – ATerminalState Terminal Superstate – ATerminalState Concrete States – Player0WonState, Player1WonState, DrawState Concrete States – Player0WonState, Player1WonState, DrawState Virtual States – Invalid Move and Valid Move Virtual States – Invalid Move and Valid Move Visitors - Handle communication to and from the GameModel and the IBoardModel Handle communication to and from the GameModel and the IBoardModel Visitors Visitors IBoardStatusVisitor IBoardStatusVisitor ICheckMoveVisitor ICheckMoveVisitor IBoardLambda IBoardLambda
Conclusion Design patterns are used to achieve proper abstractions and decoupling Design patterns are used to achieve proper abstractions and decoupling The system design can be easily modified, upgraded or adapted The system design can be easily modified, upgraded or adapted Code for one game can easily be changed to handle another game Code for one game can easily be changed to handle another game Single use, “throw away” code is not created as a result of this process Single use, “throw away” code is not created as a result of this process REUSE
Nguyen, D. and Wong, S. “Design Patterns For Games”, Proceedings of the 33 rd SIGCSE technical symposium on Computer Science Education, Volume 34, Issue 1, March 2002, Pages 126 – 130.
Questions