Unformatted text preview:

OutlineRuntime TesterDemo: Hangman GameGame RulesCode DesignStorage ManagementOutline Tester HangmanCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 15October 25, 2011CPSC 427a, Lecture 15 1/26Outline Tester HangmanRuntime TesterDemo: Hangman GameGame RulesCode DesignStorage ManagementCPSC 427a, Lecture 15 2/26Outline Tester HangmanRuntime TesterCPSC 427a, Lecture 15 3/26Outline Tester HangmanModularizing timing testsThe 14-StopWatch demo presented last time illustrated the use ofthe StopWatch class to obtain the running time of some simpletest programs.All of the test code was thrown together in a monolithic main()function.The code PS2-timing, which will be the basis of problem set 4,puts some structure on the test code.It also illustrates some C++ features that we have not used muchup to this point.CPSC 427a, Lecture 15 4/26Outline Tester HangmanStructure of class TesterSome design goals for class Tester:1. Put the subject code to be timed into a separate function.2. Separate the measurement code from the code block beingtested.3. Create a test manager runAllTests() that can be called bymain() to carry out all tests, and delegate printing to it.4. Provide a means for main() to control the parametersgoverning the test – in this case, the number of iterations andthe seed for the random number generator.CPSC 427a, Lecture 15 5/26Outline Tester HangmanObjective 1Tester has two subject member functions:Itest1() exercises an array list.Itest2() exercises a linked list.CPSC 427a, Lecture 15 6/26Outline Tester HangmanObjective 2Tester has a function measureRuntime() that takes a memberfunction as its argument and returns a HirezTime result.This requires the use of member function pointers, which will beexplained shortly.CPSC 427a, Lecture 15 7/26Outline Tester HangmanObjective 3Tests are managed by runAllTests() and runOneTest().Printing is handled by those two functions.CPSC 427a, Lecture 15 8/26Outline Tester HangmanObjective 4Optional parameters are passed to Tester through its constructorrather than being frozen in the Tester class.CPSC 427a, Lecture 15 9/26Outline Tester HangmanMember function pointersC++ supports pointers to class member functions through somenew constructs:1. The type declaration syntax is extended with the ::* syntax.2. The & operator is extended to apply to qualified memberfunction names and return member function pointers.3. Two new operators .* and ->* are introduced to followmember function pointers and permit the referenced functionsto be called.CPSC 427a, Lecture 15 10/26Outline Tester HangmanDeclaring member function pointersThe type of a member function must include the type of theimplicit argument as well as the explicit argument types and returntype. This is done by placing ::* between the type of the explicitargument and the member function name.Example:double (MyClass::*myPtr)(int)declares myPtr to be a pointer to a member function of classMyClass that takes an explicit int argument and returns a doubleIf the implicit argument is const, the declaration becomesdouble (MyClass::*myPtr) const (int)CPSC 427a, Lecture 15 11/26Outline Tester HangmanUsing typedef with member function pointersMany people find the member function pointer declaration syntaxto be cumbersome and confusing.I (and many others) highly recommend using typedef to give asimple descriptive name to the function pointer type.This new type name is then used to declare function pointerparameters and function pointer variables.Example:typedef double (MyClass::*FunctionPtr)(int);double someFunction(FunctionPtr f, int n) {...}FunctionPtr myPtr;CPSC 427a, Lecture 15 12/26Outline Tester HangmanCreating member function pointersFunction pointers are created using the & operator.Example:class MyClass {public:double myFun( int n ) {return (double)n/3.0;}};...myPtr = &MyClass::myFun;CPSC 427a, Lecture 15 13/26Outline Tester HangmanUsing member function pointersMember function pointers are followed using one of the two C++binary operators .* or ->*.Example:1. (obj.*myPtr)(17);2. (objp->*myPtr)(17);Assuming myPtr currently points to member function myFun, thesecalls are equivalent to:1. obj.myFun(17);2. (objp->myFun)(17);CPSC 427a, Lecture 15 14/26Outline Tester HangmanDemo: Hangman GameCPSC 427a, Lecture 15 15/26Outline Tester HangmanGame RulesGame RulesCPSC 427a, Lecture 15 16/26Outline Tester 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, Lecture 15 17/26Outline Tester HangmanCode DesignCode DesignCPSC 427a, Lecture 15 18/26Outline Tester 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, Lecture 15 19/26Outline Tester 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, Lecture 15 20/26Outline Tester 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, Lecture 15 21/26Outline Tester 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, Lecture 15 22/26Outline Tester HangmanCode DesignClass GameClass Game is a top-level MVC design.IModel contains alphabet, remaining vocabulary, and win/losscounters.IViewer


View Full Document

Yale CPSC 427 - Lecture 15

Download Lecture 15
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 Lecture 15 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 Lecture 15 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?