Coursework 2

IN3013/INM173 - Object-Oriented Programming in C++

This coursework involves defining and using a template class. The application is to extend a simple text-based solitaire game. It is due on Tuesday 2nd March, and counts for 10% of the marks for the module.

The starting point

Copy the file pegs.cc into a new directory and compile it. If you run the program, it will display the output

          1 2 3 4 5 6 7 8 9
        1       * * *       
        2       * * *       
        3       * * *       
        4 * * * * * * * * * 
        5 * * * * - * * * * 
        6 * * * * * * * * * 
        7       * * *       
        8       * * *       
        9       * * *
and wait for input. This represents a classic solitaire game played with pegs in a board. An asterisk (*) indicates a peg, while a hyphen (-) indicates an empty hole. There are 9 rows and columns, each numbered 1-9.

A move consists of moving a peg over an adjacent peg to a vacant hole on the other side. This continues until no move is possible. For example, in the initial configuration above, there are 4 possible moves:

To select the first move, type
        r 5 3
(move right from row 5, column 3) and the program will respond by printing the new configuration
          1 2 3 4 5 6 7 8 9
        1       * * *       
        2       * * *       
        3       * * *       
        4 * * * * * * * * * 
        5 * * - - * * * * * 
        6 * * * * * * * * * 
        7       * * *       
        8       * * *       
        9       * * *
Each command consists of a direction (l, r, u or d) and a row and column number of the peg to move. The program will carry out the move if it is legal, and object if it is not.

Adding a history

You are to add to this program the ability to back out of moves, and to redo them.

  1. To do this, you will need a data structure to represent the history. Histories are a very general notion, useful in a wide variety of applications, e.g. a history of changed in an editor, or a history of URLs in a web browser.

    For an intuition into this structure, check the way the history works in your favourite web browser. Abstractly, a history is a sequence of items, with a current position in this sequence. Undo and redo involves moving the current position back and forwards. Adding an item discards everything forwards of the current position. You will probably also need methods to return the current item, and to test whether is it possible to move back or forwards.

    Your first task is to implement a history as a generic container.

  2. Next, define a class to represent moves, so that you can have a history of moves.

  3. Now extend the program with commands back and forward to go back and forwards through the history, updating the board appropriately and redisplaying it. You should not need to change the class Board or the output operator to do this. You might however want to restructure some of the main function.

Pair working

If you wish, you may work on and submit this coursework in pairs. Undeclared pairs will be referred to the academic misconduct procedure - ``We worked together'' will not be an excuse.

A pair should submit a single solution, with both names on it. A pair submission will obtain approximately 95% of the marks that an identical single submission would have obtained, so pairs are expected to do slightly more work than singles:

but even if you don't do this, you can expect a good mark if you do the basic task successfully and neatly.

Individuals should not do this extra task.

You don't have to what you did last time, and submitting as a pair (or a single) for this coursework does not commit you for the remaining coursework.