Five Dice Game Rick Mercer - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Five Dice Game Rick Mercer

Description:

Have a menu to swap view and start new games ... Problem: You want to encapsulate a family of algorithms and make them interchangeable. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 18
Provided by: mer3
Category:
Tags: dice | five | game | mercer | rick

less

Transcript and Presenter's Notes

Title: Five Dice Game Rick Mercer


1
Five Dice GameRick Mercer
  • An Example with
  • Strategy, Observer, Composite, MVC Human Player
    v. computer player Selectable views
  • Drawing
  • Animation
  • For the Risk and Pacman teams

2
Specification
  • Complete a game of five card dice that allows one
    human player to play a Yahtzee-like game of dice
    against two other computer players. The game
    rolls five dice for each player to see has the
    best initial roll. The best five dice wins using
    this ranking system
  • FIVEOFAKIND, FOUROFAKIND, STRAIGHT (1,
    2, 3, 4, 5), FULLHOUSE (3 of a kind plus a
    pair), THREEOFKIND, TWOPAIR, PAIR
  • Each player is then given a chance to reroll
  • The highest hand after the second roll is
    declared the winner (sort of like 5 card draw
    poker)

3
Particulars
  • There must be two views of the dice game
  • The computer players must have a different
    strategies for re-rolling an the human player has
    her own
  • Have a menu to swap view and start new games
  • Allow the human player to choose without a modal
    dialog
  • Make the current player obvious

4
Review Strategy Design Pattern
  • Name Strategy (a.k.a Policy)
  • Problem You want to encapsulate a family of
    algorithms and make them interchangeable.
    Strategy lets the the algorithm vary
    independently from the clients that use it (GoF)
  • Solution Create an abstract class (or interface)
    and extend (or implement) it in numerous ways.
    Each subclass (or interface) defines the same
    method names in different ways

5
Strategy Pattern (non-computer)
  • Strategy http//www.agcs.com/supportv2/techpapers/
    patterns/papers/patexamples.htm
  • A Strategy defines a set of algorithms that can
    be used interchangeably. Modes of transportation
    to an airport is an example of a Strategy.
    Several options exist, such as driving one's own
    car, taking a taxi, an airport shuttle, a city
    bus, or a limousine service. For some airports,
    subways and helicopters are also available as a
    mode of transportation to the airport. Any of
    these modes of transportation will get a traveler
    to the airport, and they can be used
    interchangeably. The traveler must chose the
    Strategy based on tradeoffs between cost,
    convenience, and time.

6
Can plug in a strategy
7
Strategy Pattern
  • In Five Dice Game, design a strategy so different
    computer players can have different strategies
  • Using the pattern from inside the model
  • if(((ComputerPlayer)currentPlayer).doYouWantToRero
    ll(currentPlayer, getPlayers()))
  • currentPlayer.rollAllDice()
  • setChanged()
  • notifyObservers(new Integer(currentPlayerInde
    x))

8
Strategy with an interface
  • // File ReRollStrategy.java
  • import java.util.List
  • public interface ReRollStrategy
  • public boolean doYouWantToReroll(Player
    thisPlayer,
  • List
    players)

9
  • // File BeginnerStrategy.java
  • import java.util.
  • public class BeginnerStrategy implements
    ReRollStrategy
  • private static Random generator
  • public BeginnerStrategy()
  • generator new Random()
  • // Beginners reroll 50 of the time, argument
    ignored
  • public boolean doYouWantToReroll(Player
    thisPLayer
  • List players)
  • if(generator.nextInt(2) 0)
  • return true
  • else
  • return false

10
  • // File IntermediateStrategy.java
  • public class IntermediateStrategy
  • implements
    ReRollStrategy
  • // ReRoll when a higher hand is found in any
    player
  • // Argument is used here
  • public boolean doYouWantToReroll(Player
    thisPlayer, List players)
  • Player ref null
  • for( int j 0 j lt players.size() j )
  • ref (Player)players.get(j)
  • if(HandRanker.firstArgBigger(ref.getDiceArra
    y(),
  • thisPlayer.getDiceArray()) gt 0)
  • return true
  • return false

11
Player
  • Each "computer" player has a setStrategy method
  • The human player's decision will be decided by
    the user's selection at the GUI
  • Here is the code that is in GameModel

12
Executes every timer beat
  • if (currentPlayer instanceof HumanPlayer)
  • // Handle human user differently
  • myTimer.stop()
  • passButton.setEnabled(true)
  • rerollButton.setEnabled(true)
  • // advanceToNextPlayer message should be sent
  • // when user has clicked reroll or pass buttons
  • else // must be a computer player

13
  • // Defer to the computer player strategy,
  • // one of which examines other players
  • if (((ComputerPlayer)
  • currentPlayer).doYouWantToReroll(currentPlay
    er,

  • getPlayers()))
  • currentPlayer.rollAllDice()
  • setChanged()
  • notifyObservers(new Integer(currentPlayerIndex
    ))
  • currentPlayerIndex (currentPlayerIndex 1)
  • getNumberOfPlayers()
  • tick
  • myTimer.start()
  • // end of handle timer tick

14
Model
  • The model has Player, GameModel, ImageCollection,
    HandRanker, a Strategy pattern

15
(No Transcript)
16
View
  • There are two views, both extend JPanel and
    implement Observer

17
Controller
  • In this case, one class that extends JFrame and
    listens to its own menu selections
Write a Comment
User Comments (0)
About PowerShow.com