DOC PREVIEW
Yale CPSC 427 - Object-Oriented Programming
School name Yale University
Pages 24

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineDemo: StopwatchDemo: Hangman GameGame RulesCode DesignStorage ManagementRefactored GameOutline Stopwatch HangmanCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 13October 21, 2010CPSC 427a 1/24Outline Stopwatch HangmanDemo: StopwatchDemo: Hangman GameGame RulesCode DesignStorage ManagementRefactored GameCPSC 427a 2/24Outline Stopwatch HangmanDemo: StopwatchCPSC 427a 3/24Outline Stopwatch HangmanRealtime measurementsStopWatch is a class for measuring realtime performance of code.It emulates a stopwatch with 3 buttons: reset, start, and stop.At any time, the watch displays the cumulative time that thestopwatch has been running.(See demo.)CPSC 427a 4/24Outline Stopwatch HangmanHirezTime classHirezTime is a wrapper class for the system-specific functions toread a high-resolution clock.12-StopWatch (Linux/Unix/Darwin) Function gettimeofday()returns the clock in a struct timeval, whichconsists of two long ints representing seconds andmicroseconds. The granularity (resolution) of theclock is hardware-dependent.12-StopWatch-hirez (Linux only) Function clock gettime()returns the clock in a struct timespec, whichconsists of two long ints representing seconds andnanoseconds. The granularity (resolution) of theclock is hardware-dependent and can be obtainedwith the clock getres() function.CPSC 427a 5/24Outline Stopwatch HangmanHirezTime class structureClass HirezTime hides the details of the underlying timerepresentation and provides a simple interface for reading,computing, and printing times and time intervals.HirezTime objects are intended to be copied rather than pointedat.IHirezTime is derived from timeval or timespec.IIt uses protected derivation to hide the underlyingrepresentation.IIt presents two interfaces to the world:1. The normal public interface treats HirezTime as an opaqueobject.2. A class derived from it can access the fields of the underlyingtimespec/timeval.CPSC 427a 6/24Outline Stopwatch HangmanStopWatch classStopWatch contains five member variables to recordIWhether the watch is running or not.IThe cumulative run time to point when last stopped.IThe most recent start and stop times.An operator extension defines a cast for reading the cumulativetime from a stop watch:operator HirezTime() const { return cumSpan; }Thus,StopWatch sw;cout << sw;will print cumSpan using HirezTime::print().CPSC 427a 7/24Outline Stopwatch HangmanDemo: Hangman GameCPSC 427a 8/24Outline Stopwatch HangmanGame RulesGame RulesCPSC 427a 9/24Outline Stopwatch HangmanGame RulesHangman gameWell-known letter-guessing game.Start with a hidden puzzle word.Player guesses a letter.IIf letter appears in puzzle word, matching letters areuncovered.IIf letter does not appear, it is shown in list of bad guesses.Player wins when puzzle word is uncovered.Player loses after 7 bad guessesCPSC 427a 10/24Outline Stopwatch HangmanCode DesignCode DesignCPSC 427a 11/24Outline Stopwatch HangmanCode DesignOverall designGame elements:1. Puzzle word and letters found so far.2. Bad guesses word.3. Alphabet and letters left.4. Vocabulary.5. Game board display (viewer).6. Game play (controller).CPSC 427a 12/24Outline Stopwatch HangmanCode DesignUse casesTwo levels.1. Play one round of Hangman on a puzzle word.IGet input letter from user.IClassify input as good, bad, redundant, or not allowed.IInform user and show updated board.IAnnounce termination and win/loss.2. Repeated playIChoose unused word from vocabulary.IPlay Hangman with that word.ITally and announce win/loss.IAsk user whether to play again.CPSC 427a 13/24Outline Stopwatch HangmanCode DesignCode structure: ModelModel1. Alphabet used to represent letters left.2. HangWord used to represent puzzle word and bad guesses.3. Both are derived from BaseWord4. Common elements are a word and a visibility mask.5. Variable elements:IHow to print masked word.IOperations needed: find and try6. Class Board data members store model state.CPSC 427a 14/24Outline Stopwatch HangmanCode DesignCode structure: Viewer and controllerViewer Contained in class Board.IBoard::print() prints the puzzle, letters left, and badguesses.IBoard::move() prints guess, outcome, and next board.IBoard::play() prints the win/loss message.Controller Contained in class Board.IBoard::play() carries out turns and determines gametermination.IBoard::move() prompts users for character and carries outturn.IBoard::guess() updates the model.CPSC 427a 15/24Outline Stopwatch HangmanCode DesignClass GameClass Game is a top-level MVC design.IModel contains alphabet, remaining vocabulary, and win/losscounters.IViewer is embedded in Game::play().IController is in Game::playRound() and Game::play().CPSC 427a 16/24Outline Stopwatch HangmanStorage ManagementStorage ManagementCPSC 427a 17/24Outline Stopwatch HangmanStorage ManagementStorage managementTwo storage management issues in Hangman:1. How to store the vocabulary list?2. How to store the words in the vocabulary?Natural solutions are to store vocabulary as an array of pointers tostrings.Natural way to each string is to use new to allocate a characterbuffer of the appropriate length.Design issues:IHow big should the vocabulary array be?IWho owns the strings and takes responsibility for cleanupwhen they are no longer needed?CPSC 427a 18/24Outline Stopwatch HangmanStorage ManagementString storeA StringStore provide an alternative way to store words.Instead of using new once for each string, allocate a big char arrayand copy strings into it.When no longer needed, ~StringStore() deletes entire array.Advantages and disadvantages:IMuch more efficient—(each new consumes minimum of 32bytes on modern machines).ISimpler storage management—ownership of storage remainswith StringStore.IDownside: Can’t reclaim storage from individual strings untilthe end.IHow big should the char array be?CPSC 427a 19/24Outline Stopwatch HangmanRefactored GameRefactored GameCPSC 427a 20/24Outline Stopwatch HangmanRefactored GameRefactored hangman gameDemo 11-Hangman-full extends 10-Hangman in three respects:1. It removes the fixed limitation on the vocabulary size.2. It removes the fixed limitation on the string store size.3. It more clearly separates the model of Board from theviewer/controller.We’ll examine each of these in detail.CPSC 427a 21/24Outline Stopwatch HangmanRefactored GameFlex arraysA FlexArray is a growable array of elements of type T.Whenever the array is full, private method grow() is called toincrease the storage allocation.grow() allocates a new array of


View Full Document

Yale CPSC 427 - Object-Oriented Programming

Download Object-Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?