Design Patterns for Games Stephen Wong Dung Nguyen Rice University.

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

CS12230 Introduction to Programming Tying things together.
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Tic Tac Toe Game Design Using OOP
Introduction to Programming Using simple games to convey introductory concepts MERLOT International Conference 2004 Tracey Jensen Assistant Professor,
CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu.
Model View Controller Development architecture. MVC Model: the classes encapsulating the functionality of your app View: what the user sees and interfaces.
PLANNING THE TIC TAC TOE GAME BY NEEL DAVE. TIC TAC TOE INSTRUCTIONS Tic Tac Toe Instructions The basic concept of Tic Tac Toe 1.This is a game for two.
Tic Tac Toe Architecture CSE 5290 – Artificial Intelligence 06/13/2011 Christopher Hepler.
Games CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Façade Design Pattern (1) –A structural design pattern.
1 Teaching Programming with Sudoku Bill Sanders for Axel T. Schreiner Killer Examples Workshop at OOPSLA’09.
1 Adversary Search Ref: Chapter 5. 2 Games & A.I. Easy to measure success Easy to represent states Small number of operators Comparison against humans.
Minimax.
1 Othello Game Programs (human to human). 2 (defconstant all-directs '( )) ALL-DIRECTS (defconstant empty 0) EMPTY (defconstant.
Lecture 6: Game Playing Heshaam Faili University of Tehran Two-player games Minmax search algorithm Alpha-Beta pruning Games with chance.
Adversarial Search CS311 David Kauchak Spring 2013 Some material borrowed from : Sara Owsley Sood and others.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
Game Playing. Towards Intelligence? Many researchers attacked “intelligent behavior” by looking to strategy games involving deep thought. Many researchers.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
Design Patterns for Sorting Teaching something old in a new light Dung “Zung” Nguyen Stephen Wong Rice University.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Marine Biology Simulation Part II: Assignment, Milestone 1 Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
Chapter 9 Putting together a complete system. This chapter discusses n Designing a complete system. n Overview of the design and implementation process.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Design Patterns for Games Proceedings of the 33 rd SIGCSE technical symposium on Computer Science Education Melisa Tyira SE510.
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
CS12230 Introduction to Programming Extra example– More on responsibilities 1.
Adversarial Search and Game Playing Russell and Norvig: Chapter 6 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2004/home.htm Prof: Dekang.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Understanding AI of 2 Player Games. Motivation Not much experience in AI (first AI project) and no specific interests/passion that I wanted to explore.
1 Decisions in games Minimax algorithm  -  algorithm Tic-Tac-Toe game Decisions in games Minimax algorithm  -  algorithm Tic-Tac-Toe game.
Adversarial Search and Game-Playing
ADVERSARIAL GAME SEARCH: Min-Max Search
Template Method Pattern Iterator Pattern
Lecture 5 of Computer Science II
Alpha-Beta Pruning.
Defining Rules and Levels of Rules (Chs. 11 & 12)
Adversarial Search and Game Playing (Where making good decisions requires respecting your opponent) R&N: Chap. 6.
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Adversarial Search.
Design Patterns for Games
Review.
Chapter 5 - Functions Outline 5.1 Introduction
Design Patterns for Games
Chapter 12 Recursion (methods calling themselves)
Game playing.
Chapter 6 : Game Search 게임 탐색 (Adversarial Search)
Design Patterns for Games
Marine Biology Simulation Part II: Assignment, Milestone 1
Design Patterns for Games
Adversarial Search and Game Playing Examples
Minimax strategies, alpha beta pruning
Data Structures and Algorithms
Tic-Tac-Toe Game Engine
Adversarial Search Game Theory.
Derek : No Artificial Flavors.
Unit II Game Playing.
Presentation transcript:

Design Patterns for Games Stephen Wong Dung Nguyen Rice University

Let’s Play a Game  Sun’s Tic-Tac-Toe Sun’s Tic-Tac-Toe  What can we learn from this? 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? XX O O Controller Model Rules Players Strategies View Buttons, etc. Display outputs adapter Install adapters

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 Decouple these components! Abstract these components!

The Rules of the Game IBoardModel IBoardStatusVisitor makeMove(player, row, col), chkMoveCmd) IUndoMove, bdStatusVstr) ICheckMoveCmd validMoveCase() invalidMoveCase() player1WonCase(…) player0WonCase(…) noWinnerCase(…) drawCase(…) IUndoMove apply(…) commands host/visitor Object execute(bdStatusVstr, param) // other methods

State Diagram Invalid Move State Valid Move State Non-Terminal State (no winner yet) Terminal States Player #0 Wins Draw Game Player #1 Wins

ABoardState state State Design Pattern TicTacToeBoard Player1WonPlayer0WonDrawGameATerminalStateNonTerminalState IBoardModel execute(IBoardStatusVisitor v, …) v.player0WonCase(…) state.execute(v, …) v.player1WonCase(…) v.drawCase(…) v.noWinnerCase(…)

Playing the Game ComputerPlayer takeTurn(…) INextMoveStrategy Point getNextMove(…) RandomAlphaBetaMinMax The next move process is decoupled from the rules of the game!

Facade APlayer takeTurn(…) IBoardModel makeMove(…) TurnControlIView IRequestor

What the player sees: public interface IRequestor { public abstract void setTokenAt( int row, int col, int player, IRejectCommand rejectCommand); } public interface IRejectCommand { public abstract void execute(); }

Player Factories IView GameModel Set of APlayer factories Selected APlayer factories Only displays the toString() of the factories. The factories are treated as Objects! private interface IMakePlayer { public APlayer create(int playerNo); }

The Set of APlayer Factories public Vector getPlayers() { Vector v = new Vector(); v.addElement(new IMakePlayer() { public APlayer create(int playerNo) { return new HumanPlayer(requestor, playerNo, turnAdmin); } public String toString() { return "Human player"; } }); return v; } Anonymous APlayer factory IView only cares about this! Factory method

Min-Max Principle V(s) =  For terminal state  +1, if s is a winning state for that player  0, if s is a draw state  -1, if s is a losing state for that player  For non-terminal state  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. The best next move for a given player, m, is determined from max{V(c) | c  S} where S is the set of available moves for that player. How max is computed is a variant. Application of a process over a set!

Mapping and Lambda  Math/FP: Map(, S) = { (x) | x  S}  Express our algorithm in terms of mapping, not iteration: –min(…)  map(, min-accum) –max(…)  map(, max-accum) Both accumulators are abstractly equivalent!  Backtracking is automatically handled by mapping.

IBoardLambda Mapping Abstraction IBoardModel makeMove(player, row, col), chkMoveCmd) IUndoMove, bdStatusVstr) command Object execute(bdStatusVstr, param) void map(player, lambda, param) boolean apply(board, param, row, col, cell-val) void noApply(board, param) Controls continuation of mapping Called when there are no valid moves. Called on all valid moves.

Min-Max Abstraction INextMoveStrategy MinMaxaccFac:IAccFactory AAccum makeAcc(player) minMaxEval:IBoardLambda boolean apply(…) void noApply(…) Point getNextMove(model, player) AAccum acc = accFac.makeAcc(player); model.getBoardModel().map(minMaxEval, acc); return acc.getMove();

private IBoardLambda minMaxEval = new IBoardLambda() { public boolean apply(board, acc, row, col, cell-value) { IUndoMove undo = host.makeMove( row, col, acc.getPlayer(), validMvVstr, new IBoardStatusVisitor() { player0WonCase(...) {…} player1WonCase(…) {…} drawCase(…) {…} noWinnerCase(…) { undo.apply(validUndo); return acc.isNotDone(); } Update accumulator Update accumulator Update accumulator AAccumulator nextAcc = acc.makeOpposite(); host.map(nextAcc.getPlayer(), minMaxEval, nextAcc); acc.updateBest(row, col, nextAcc.getVal()); return null; } }); Try a test move. Undo the move. Declarative style programming! to be mapped over the available states. Called by map on each valid (row, col) What to do in each situation Stop mapping?

Alpha-Beta Pruning public class AlphaAcc extends MaxAcc { public AAccumulator makeOpposite() { return new BetaAcc() { { this.modelPlayer = AlphaAcc.this.modelPlayer; } public boolean isNotDone() { return AlphaAcc.this.getVal() < this.getVal(); } }; } } Just a new accumulator! Override the creation of the next level’s accumulator Accumulator for the opposite player as an anonymous inner class. Stop mapping if pruning condition is met. Abstraction isolates the essence and provides extensibility Inner class gives the scoping we need!

Player Management Event-loop for turn-taking Call-back techniques for asynchronous processing Abstract players

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 Design patterns express abstractions

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:  Patterns for Decoupling Data Structures and Algorithms: