CSCI 201 Fall 2001

Assignment 3


Due Friday Oct 19 at midnight


Work problem 4.2 on page 217 of your text. To write the program described in problem 4.2, you will need to write two classes, one of which must be Pig and the other may be either PairOfDice or Die (the class provided on page 204 of your text). (Normally, you could consider breaking the program into more than two classes, but because your programs are automatically compiled and executed, you are required to use exactly two classes in this assignment.) Both classes must be submitted to your ftp directory and both must have the correct names.

If you choose to write a PairOfDice class, you may want to make use of the Die class provided on page 204 of your text in writing the PairOfDice class. If you choose to make use of the Die class, include it in the file named PairOfDice.java as illustrated below.

Be sure to remove the modifier "public" from the Die class definition.

In order to write this program correctly you will have to carefully consider the program design. Before writing any code, outline the steps that are required to play the game of Pig. Start with a high-level outline (lacking detail) such as the one below.


  initiallize the necessary variables

  while neither player has 100 points do the following:

      alternate turns letting each player roll until one of two
      things occurs: (1) the player rolls one or more 1's, (2) the
      player decides to relinquish control of the dice.
  
  announce the winner and end the game
Refine your outline (or game plan), adding more and more detail until you have a game plan that you can understand and that could be translated into java code. Below is slightly more refined game plan:

  initialize one variable for each player to keep track of their
  total points

  initialize one variable to keep track of whose turn it is

  while neither player has 100 points do the following:

      if (turn equal player1)

          let player1 roll until one of two things occurs: (1) the
	  player rolls one or more 1's, (2) the player decides to
	  relinquish control of the dice
 
                keep track of the number of points that a player rolls
                during their turn

          if player rolls a single 1, set their points for this turn
          to zero

          if a player rolls two 1's, set their total points to zero

          set turn to player2

      else

          let player2 roll until one of two things occurs: (1) the
	  player rolls one or more 1's, (2) the player decides to
	  relinquish control of the dice.
 
                keep track of the number of points that a player rolls
                during their turn

          if player rolls a single 1, set their points for this turn
          to zero

          if a player rolls two 1's, set their total points to zero

          set turn to player1

  announce the winner and end the game
Notice that in the game plan above, the body of the if and the body of the else are the same. When steps are repeated such as they are above, it is good program design to write a method to perform those steps. For example, if we write a method called takeTurn() to perform the following steps:

          let player roll until one of two things occurs: (1) the
	  player rolls one or more 1's, (2) the player decides to
	  relinquish control of the dice.
 
                keep track of the number of points that a player rolls
                during their turn

          if player rolls a single 1, set their points for this turn
          to zero

          if a player rolls two 1's, set their total points to zero

          set turn to other player
Then our overall game plans now looks like:

  initialize one variable for each player to keep track of their
  total points

  initialize one variable to keep track of whose turn it is

  while neither player has 100 points do the following:

      if (turn equal player1)

          call takeTurn() for player1
      else

          call takeTurn() for player2

  announce the winner and end the game
Do not trust the above play game to be complete; there are important details missing. Remember that the computer is always one of the players in the game, and the computer does not provide keyboard inputs. The computer always relinquishes control of the dice after accumulating at least 20 pts in a single turn.

The final step in designing your program before you beginning writing the program in java is to design your classes. For each class, determine its state and behaviors. For example, what instance variables will you need to represent the state of the PairOfDice class? What methods will that class need?

Once your program design is complete, you can begin to implement your program, that is, write your program in java. Don't write and test everything at once, instead test your program in pieces. For example, test the PairOfDice class first and make sure that it works before you begin testing the pieces of the Pig class.

If you need help with this program, come by during office hours, but be sure to bring your game outline (also called pseudo-code) and/or your program on disk.

Submitting Programs

Both classes written for this assignment, Pig and either PairOfDice or Die, must be turned in by transferring the respective java source code files to your ftp directory for this class. You were shown how to transfer files to your class ftp directory in lab 1, here is a quick refresher.