CPSC 427a: Object-Oriented Programming

Michael J. Fischer

Lecture 13
October 21, 2010

subsection in toc subsection in toc subsection in toc subsection in toc

Demo: Stopwatch

Realtime measurements StopWatch 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 the stopwatch has been running.

(See demo.)

HirezTime class

HirezTime is a wrapper class for the system-specific functions to read a high-resolution clock. 12-StopWatch (Linux/Unix/Darwin) Function gettimeofday() returns the clock in a struct timeval, which consists of two long ints representing seconds and microseconds. The granularity (resolution) of the clock is hardware-dependent. 12-StopWatch-hirez (Linux only) Function clock_gettime() returns the clock in a struct timespec, which consists of two long ints representing seconds and nanoseconds. The granularity (resolution) of the clock is hardware-dependent and can be obtained with the clock_getres() function.

HirezTime class structure

Class HirezTime hides the details of the underlying time representation and provides a simple interface for reading, computing, and printing times and time intervals.

HirezTime objects are intended to be copied rather than pointed at.

StopWatch class

StopWatch contains five member variables to record

An operator extension defines a cast for reading the cumulative time from a stop watch:

   operator HirezTime() const { return cumSpan; }

Thus,

   StopWatch sw;

   cout << sw;

will print cumSpan using HirezTime::print().

Demo: Hangman Game

Game Rules

Hangman game Well-known letter-guessing game.

Start with a hidden puzzle word.

Player guesses a letter.

Player wins when puzzle word is uncovered.

Player loses after 7 bad guesses

Code Design

Overall design Game 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).

Use cases

Two levels.

  1. Play one round of Hangman on a puzzle word.
  2. Repeated play

Code structure: Model

Model

  1. Alphabet used to represent letters left.
  2. HangWord used to represent puzzle word and bad guesses.
  3. Both are derived from BaseWord
  4. Common elements are a word and a visibility mask.
  5. Variable elements:
  6. Class Board data members store model state.

Code structure: Viewer and controller

Viewer Contained in class Board.

Controller Contained in class Board.

Class Game

Class Game is a top-level MVC design.

Storage Management

Storage management Two 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 to strings.

Natural way to each string is to use new to allocate a character buffer of the appropriate length.

Design issues:

String store

A StringStore provide an alternative way to store words.

Instead of using new once for each string, allocate a big char array and copy strings into it.

When no longer needed, ~StringStore() deletes entire array.

Advantages and disadvantages:

Refactored Game

Refactored hangman game Demo 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 the viewer/controller.

We’ll examine each of these in detail.

Flex arrays

A FlexArray is a growable array of elements of type T.

Whenever the array is full, private method grow() is called to increase the storage allocation.

grow() allocates a new array of double the size of the original and copies the data from the original into it (using memcpy()).

Note: After grow(), array is 1/2 full.

By doubling the size, the amortized time is O(n) for n items.

Flex array implementation issues

Element type: A general-purpose FlexArray should allow arrays of arbitrary element type T.

If only one type is needed, we can instantiate T using typedef.

Example: typedef int T; defines T as synonym for int.

C++ templates allow for multiple instantiations.

Class types: If T is a class type, then its default constructor and destructor are called whenever the array grows.

They must both be designed so that this does not violate the intended semantics.

This problem does not occur with numeric or pointer flexarrays.

String store limitation

Can’t use FlexArray to implement StringStore since pointers to strings would change after grow().

Instead, when one StringStore fills up, start another.

Only really want another storage pool, not another StringStore object.

Eacn new Pool is linked to the previous one, enabling all pools to be deleted by ~StringStore().