chapter 6 chapter 6 case study: game2d with method design

25
Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Upload: eleanor-bond

Post on 30-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Chapter 6Chapter 6

Case Study: Game2D with Method Design

Page 2: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Objectives of Game2DObjectives of Game2D

Simple 2 player game To be played over the Internet The game board consists of a two-

dimensional grid Dots randomly appear on the grid, and

alternate between two colors (red and blue) Each player is assigned a color, either red or

blue

Page 3: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

More Objectives of Game2DMore Objectives of Game2D

The objective of the game is for each player to cause dots of his/her color to disappear by clicking on the dots

A player loses if two neighboring dots of his/her color appears on the gird

Page 4: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Supported Activity List Supported Activity List

Allow registration of two players for each game Display a simple table (grid) of initially empty cells When two players have been registered and one of

them clicks the start button, the game begins Randomly display dots in the cells at regular

intervals. The dots alternate in color between red and blue

Allow the player to adjust the interval at which dots are displayed

Page 5: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Supported Activity List Supported Activity List ContinuedContinued

Assign one of two colors (red or blue) to each player so that each player is concerned with dots of that color only

Respond to mouse clicks. If either player clicks on a dot, the dot disappears in response to that click

Keep track of points (the score). Each time a player clicks on a dot of his or her color. He or she earns a point

If two dots of the same color appear next to each other, the player corresponding to that color loses. Notify each player of their win or loss

Page 6: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Human-Computer InterfaceHuman-Computer Interface

Page 7: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Requirements AnalysisRequirements Analysis

List of nouns Player Game Internet Table Cell Start Game button Dot Interval

Color Mouse Mouse click Point (score) Loser

Page 8: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Analysis of List of NounsAnalysis of List of Nouns

Retained information

Needed services

Multiple attributes

Common attributes

Common operations

Essential requirements

Page 9: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Primary ClassesPrimary Classes

Player

Game

Cell

Dot

Page 10: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Use Case DevelopmentUse Case Development

Client communicates player sign-on Client communicates player action (mouse

click) Server communicates that dot is removed

from cell Server communicates that dot is set in cell Server communicates that game is won/lost

Page 11: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Use Case: Client Use Case: Client Communicates Player ActionCommunicates Player Action

Main flow of events:This use case begins when the player clicks the left mouse button in the applet window. The goal of each player is to erase as many dots of his or her associated color as possible. In other words, the blue player wishes to erase blue dots, while the red player wishes to erase red dots. A player erases a dot by pressing the left mouse button over a cell containing that dot. When the mouse button is clicked, the event is trapped and the coordinates of the mouse event are translated into a specific reference to a Cell object.

Page 12: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Use Case: Client Use Case: Client Communicates Player Action Communicates Player Action (continued)(continued)

Main flow of events (continued):

The Cell object is queried to determine if a Dot is present in that Cell and what its color is. If no Dot exists in this Cell, the use case ends. If a Dot matching the player's color is present, the player receives a point. If a Dot of any color is present, the Dot is removed from the Cell and the information to remove the Dot from the Cell is transmitted to the server. The use case then ends.

Page 13: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Use Case: Client Use Case: Client Communicates Player Action Communicates Player Action (continued)(continued)

Exceptional flow of events:

If a player clicks the mouse on an area of the applet where no Cell object is rendered, the use case ends with no information being transmitted to the server.

(see deliverable 6.11 for associated scenario)

Page 14: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Refined Class ListRefined Class List

Player Game2DServer Game Timer Cell Dot

Page 15: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

An Initial Class An Initial Class Diagram(Partial)Diagram(Partial)

GameServer Timer

Game Player

Dot Cell

starts prompts

communicates with

contains

contains

contains

(see deliverable 6.16 for complete diagram)

Page 16: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Use Case Diagram for Game2DUse Case Diagram for Game2D

Signon

removeDot

setDot

Game Won/Lost

Action

Player Internet

Page 17: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

State Machine for Game2D State Machine for Game2D (Partial)(Partial)

Game2D

Communicate Game status

Get name Select color

(see deliverable 6.18 for complete diagram)

Wait for other players

Wait for dots

connectplayer #2 connects game initiation

Page 18: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Revised Class Diagram for Revised Class Diagram for Game2D (Partial)Game2D (Partial)

Game2DInitiator

Game2DServer

Timer

prompts

prompts

starts

unsetDot Game2DServerListener

Game2DClient

Communicates with

Game2DClientListener

unsetDotCommunicates with

(see deliverable 6.19 for complete diagram)

Page 19: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Game2D: Interaction Diagram Game2D: Interaction Diagram (Partial)(Partial)

:Game2DServer

:Game2DServerListener

unsetDot(row,col)

{messages}

setColor(color)setDot(row,col,color)unsetDot(row,col)win()lose()

:Game2DClient

:Game2DClientListener

(see deliverable 6.20 for complete diagram)

Page 20: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Game2D: Object Diagram Game2D: Object Diagram (Partial) (Partial)

:Game2DClientListenerAppletSocket

:Game2DClientgrid Cell[][]Socket color = 1

grid[0][0]:CellX=0y=0width=15height=15ApplethasDot=falsecolor= 0

(see deliverable 6.22 for complete diagram)

Page 21: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Game2D: ReuseGame2D: ReuseCell

boolean hasDot;int color;

checkDot(int color)setDot(int color)unsetDot(int color)

Graphical Cell

Applet app;int x, yint width, height;

setDot(int color)unsetDot(int color)draw()

Page 22: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Game2D: Class Skeleton Game2D: Class Skeleton (Partial)(Partial)

public class Game2DClient extend Applet implements Mouselistener, ActionListener{ // Class semantics and roles // This class embodies the GUI which the player interacts // Instance variables: private Graphical_Cell grid[][]; // 2D array of cells: game state private int color; // the player’s color 1 = blue, 2 = red private Socket sock; // Socket connection to game server private PrintWriter out; // output stream built over socket connection

// Nonstatic Methods public void paint(Graphics g) // Override Applet method to render the array of graphic cells public void setDot(int I, int j, int c)

// send message to graphical cell object at row i, column j array grid // set color to Color.blue, if c = 1 otherwise set it to Color.red // precondition: 2D array of graphical cells has been allocated and c

// c has value of 1 or 2. Postcondition : either a red or blue dot appears // in the graphic cell grid[I][j] .

(see deliverable 6.23 for complete skeleton)

Page 23: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Method DesignMethod Design

Techniques for specifying algorithms Nassi-Shneiderman charts

Flowcharts

Pseudocode

Decision tables

Page 24: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Decision Table and Decision Table and Psuedocode ExamplePsuedocode Example

pick random number between 0 and MAX-1set dot in cell grid[I][j] to color c

grid[i+1] [j] has dot color c player c loses

grid[i-1] [j] has dot color c

grid[i] [j+1] has dot color c

grid[i] [j-1] has dot color c

player c loses

player c loses

player c loses

Page 25: Chapter 6 Chapter 6 Case Study: Game2D with Method Design

Creating Quality MethodsCreating Quality Methods

Cohesive

Loosely coupled

Encapsulated

Reusable