Five Dice Game - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Five Dice Game

Description:

Complete a game of five card dice that allows one human player to play a Yahtzee ... own car, taking a taxi, an airport shuttle, a city bus, or a limousine service. ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Five Dice Game


1
Five Dice Game
  • An Example Project with
  • Strategy, Observer, Composite, MVC
  • Human Player v. Computer Player
  • Selectable views
  • Menus
  • Drawing
  • Animation

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
User Stories
  • The view indicates the current player
  • The player can view the current state of the
    opponents
  • The human player is given a chance to reroll or
    pass
  • without a modal dialog--JOptionPane (Rick's pet
    peeve)
  • There are two different computer strategies for
    re-rolling
  • Can swap between two views of the dice game
  • Demo FiveDiceGame

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(currentPlayer.doYouWantToReroll(currentPlayer,
  • getPlayers()))
  • currentPlayer.rollAllDice()
  • setChanged()
  • // Tell the view which player to highlight
  • // How could I refactor this?
  • notifyObservers(new Integer(currentPlayerInde
    x))

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

9
  • // File BeginnerStrategy.java
  • public class BeginnerStrategy implements
    ReRollStrategy
  • private static Random generator
  • public BeginnerStrategy()
  • generator new Random()
  • // Beginners reroll 50 of the time, arguments
    ignored
  • public boolean doYouWantToReroll(Player
    thisPLayer
  • ListltPlayergt
    players)
  • return (generator.nextInt(2) 0)

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,
  • ListltPlayergt
    players)
  • Player ref null
  • for( int j 0 j lt players.size() j )
  • ref 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
Allow human and Computer players every timer beat
without modal boxes
  • 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
  • else // must be a computer player
  • // Defer to the computer player strategy,
  • // one of which examines other players
  • if (currentPlayer.doYouWantToReroll(currentPlaye
    r,

  • 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