Design Patterns for Games
Stephen Wong
Dung Nguyen
Rice University

Let’s Play a Game
Sun’s Tic-Tac-Toe
What can we learn from this?
Arrays
For-loops
Nested Conditionals
What aren’t we learning?
Delineation of concepts from implementation
Abstraction
Design

Something Different…
It’s not really about TicTacToe…
It’s a vehicle to teach something BIGGER.
Abstraction
Design Process
Fundamental Principles

What’s in a Game?

Game Model
Rules of the Game
Board configurations
Legal moves
Next Move Strategies
Random
Min-Max
Etc.
Player Management
Turn taking
Win/lose/draw notification
Fault handling

Game Model Classes

State Diagram

State Design Pattern

The Rules of the Game

Playing the Game

Facade

What the player sees:

Player Factories

The Set of APlayer Factories

Min-Max Principle

Mapping and Lambda
Math/FP:   Map(l, S) = {l(x) | x Î S}
Express our algorithm in terms
of mapping, not iteration:
min(…) à map(l, min-accum)
max(…) à map(l, max-accum)

Abstract Accumulators

Mapping Abstraction

Min-Max Abstraction

Slide 20

Alpha-Beta Pruning

Player Management

Design Patterns In Action
MVC separates model from view
Commands and Visitors isolate rules from behaviors
State pattern models game behavior
Calculating the next move is a Strategy

Concepts in Action
Abstract functions – lambda’s
Higher order functions – Mapping
Declarative programming
Invariant: Min-Max Principle
Variant:
Full depth-first search
Alpha-beta pruning

More Information…
Design Pattern for Games:
http://www.exciton.cs.rice.edu/research/SIGCSE02
Patterns for Decoupling Data Structures and Algorithms:
http://www.exciton.cs.rice.edu/research/SIGCSE02