CPS 100E FALL 1997: Assignment #2

Due: Tuesday, Sept. 30 by 8am

Last LATE day to turn in LATE programs: Thursday, Oct. 9 by 8am

EXTENSION on due date click here

25 points

Note: Mon. Oct. 6 and Tue. Oct. 7 are not considered late days since Test 1 is on Tue., Oct. 7.

Introduction

For this assignment you'll implement a solution to the josephus problem. In Mark Weiss Algorithms, Data Structures and Problem Solving with C++ (Addison-Wesley), the genesis of the problem is described as (slightly paraphrased, pp 397-398).
N people, numbered 0 to N-1, are sitting in a circle. Starting at person 0, a hot potato is passed. After M passes, the person holding the potato is eliminated, the circle closes ranks, and the game continues with the person sitting after the eliminated person starting with the potato. The last person remaining wins.

[the story begins] in the first century AD, in a cave on a mountain in Israel, where Jewish zealots were besieged by Roman soldiers. The historian Josephus was among them. The zealots voted to form a suicide pact rather than surrender to the Romans, to Josephus' consternation. He suggested the game mentioned here. The "hot potato" was the sentence of death to the person who got the potato. Josephus rigged the game to get the last potato. This is how we know about the game, in effect Josephus cheated.

You'll be simulating the potato passing, and using object oriented techniques to facilitate making an animated version of the process. You'll use JAWAA, a Java, web-based animation package developed by an undergraduate at Duke under the guidance of Prof. Susan Rodger.

There are two parts and some setup to this assignment. They are outlined below; particulars can be found in the sections that follow.


Getting Started

In your cps100e directory, create a directory called assign2 using the mkdir command. Change into the assign2 directory.

In order to do this assignment, you need to copy some files using the following cp command (don't forget the trailing period, or dot):

       cp  ~rodger/cps100e/assign2/*  .

This command will copy files into your directory for you to use. If you type ls you should see the files listed below.

The josephusAnim.html page is a web page that has been created for this assignment. If you don't already have a web page on the acpub system, from your home directory (the directory you are in when you login) create a directory called public_html by typing: mkdir public_html

Change into this directory by typing: cd public_html

Check the permissions of this directory by typing: fs la

This directory needs to be readable by system:anyuser, that is, it should have rl permission set. If it doesn't then you will need to type the command: fs setacl . system:anyuser read

Move the josephusAnim.html to your public_html directory (use the mv command to move the file). You will need to modify one line in this file. On line 14 shown below, change rodger to your login id.

VALUE="http://www.duke.edu/~rodger/josephus.anim">

Using Netscape, you can view this page by entering in the URL below, change rodger below to your login id

http://www.duke.edu/~rodger/josephusAnim.html

The animation won't work yet until your program creates the josephus.anim file.

For the programming problem that follows, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments (pre and postconditions) at the top of the file and for each function. Also include your name, date, course, and purpose in a comment at the top of the program.


Part 1: Josephus Problem (array version) (15 points)

The program doparty currently simulates a much simplified version of the Josephus game, one in which each player is chosen in turn. This is equivalent to a version of the Josephus problem in which M is 1. Recall that M is the number of people "touched" by the hot potato before a person is chosen.

Program Requirements

In brief, you must modify the program to play a "real game" of hot potato as per the description of the Josephus problem. Your implementation in party.cc should use a vector. You must also implement the class JawaaObserver so that it can be used to generate an animated version of the process you're simulating.

Details

Writing files

Before compiling your program the first time, you must edit doparty.cc to change the name of the output file written. In particular, in the jawaOut declaration shown below, change /r/o/rodger to correspond to the first two letters of your login id, followed by your login id.

    string jawaOut = "/afs/acpub/users/r/o/rodger/public_html/josephus.anim";   // write to public_html directory

Type make doparty to create a runnable program and type doparty to run your program. Now when you run your program, the file with animation commands, josephus.anim will automatically be written into your public_html directory. To run the animation, you can reload netscape to see the newest version of your animation. Currently, there is no animation.

Interaction of Classes

The diagram below illustrates how the classes interact. This program uses inheritance (you don't need to pay attention to the word virtual for now, we will talk about inheritance later in the semester).

NoPicture

The Party class

The party class is the actual playing of the game. People are added to the game, then turns are taken repeatedly until a winner is found.

You are to modify the function Party::TakeOneTurn so that instead of using a value of 1, a value equal to the magic number passed to the Party class' constructor is used. You'll have to think carefully how to do this.

You may need to modify other Party member functions too, or add private variables to the class. You can modify the class in any way.

You must call the update function of all registered observers each time a player is touched or picked. You can do this by calling the Party::Notify private function each time a player is picked or touched.

The JawaaObserver class

This program is setup to have many observers. An Observer watches the game and produces output in some form. There are currently two observers. The JawaaObserver produces JAWAA commands, animation commands that are processed by JAWAA to create the animation. The PartyObserver (described later) produces text.

You must modify the JawaaObserver class so that it stores JAWAA commands in the file bound to its output stream myOut. The JAWAA commands, when replayed using the JAWAA animator, should provide an animated version of the simulation. In the version you're given, the only JAWAA output is a sequence of comments.

Documentation on JAWAA is available here, including sample JAWAA animations.

The PartyObserver class

The PartyObserver class is an observer that produces textual output to the screen that might be helpful in debugging the program.

You do not need to modify this class. It can be used to help debug your program assuming you call the update function at the right time (via the Party::Notify function in all likelihood).

The Player class

You probably will need to modify this class.

The main program, doparty.cc

You are to modify main so that 1) the magic number is asked for (currently it is set to 5) and 2)the names of the players at the party are read from a file whose name is specified by the user. You do not know how many players will be read in, but you can assume that each name contains no blanks or white space.

Also see the modification described earlier about the jawaa output file.


Animation Requirements and Sample

The animation must clearly illustrate the magic number, the passing of the potato from person to person, the person to be removed when a "hot potato" occurs, and the order of the people removed from the "circle". For animation purposes only, you can assume that names are no more than 8 characters long. Your program should handle longer names, but the animation will only be tested on names with at most 8 characters.

Click here to see a sample animation for this assignment. Your animation does not have to look like this one, you are probably more creative than your professors. In this animation, the passing of the potato is illustrated by blinking a person blue, the "hot potato" person is physically removed, and the order is indicated by the first number in light blue (actually the color is cyan)(starting at 0). The second number in light blue indicates the position in the array of the previous person pulled out. Thus the order can be obtained by using either the first or second number in light blue.

Some Hints on running JAWAA


Part 2: Josephus Problem(linked list version) (10 points)

In this part you will reimplement the myPlayers vector in party.cc and party.h to a circular linked list.

In your cps100e directory, you'll need to create another directory called assign2.2 and make copies of all your files from part 1 in this directory by typing

cp assign2/* assign2.2

In party.h and party.cc, modify the party of people to be a circular linked list instead of a vector.


Submitting the Program

If you copied these files before 9/22, then you need to recopy the Makefile as there was a typo in it for submitting programs.

When your programs compile and produce the correct output, you should create a README file for this and all assignments. In this file, include your name, section number, the date, and an estimate of how long you worked on the assignment. You must also include a list of names of all those people (students, prof, tas, tutor) with whom you consulted on the assignment. See the rules for collaboration in the CPS 100e syllabus.

Both parts 1 and 2 have to be submitted on time to receive full credit. To submit parts 1 and 2 of your assignment, you can just type

make submitpart1

and this will invoke the submitpart1 command in the Makefile to automatically call the submit100e command and list out a README file and all your .h and .cc files.

If this doesn't work, then you need to type the submit100e command yourself, including a README file and all your .h and .cc files, type:

submit100e assign2 README doparty.cc lldoparty.cc party.cc party.h ...

For part2, type

make submitpart2

You should receive a message telling you that the program was submitted correctly. If it doesn't work try typing ~rodger/bin/submit100e in place of submit100e above.


Extra Credit(3 pts)

Copy either the array version or the linked list version of the program to yet another directory called assign2X.

Add a third observer to create a different JAWAA animation. The output of the third observer will go to another output file. The new observer should have its own .h and .cc file.

You can put both animations on the same web page, you'll have to cut and paste the applet for the first animation, and change the name of the file. You'll also need to modify the Makefile to compile this program.

Submitting Extra Credit

To submit the extra credit assignment, your README should describe the extra credit you did, the names of the new files, etc. To submit, type (include the Makefile you modified, plus all .h and .cc files):

submit100e assign2X README Makefile ..........
rodger@cs.duke.edu
Last modified: Tue Sep 16 13:34:29 EDT 1997