CS 107 (Spring '09)
[Schedule] [Programs] [Notes & Reference] [Examples][Syllabus] [Lab & TA] [Tests] [Grades]

Program #2: Peg Jump

Prof. Reed, CS 107, Spring '09
Due Monday 2/16 at 1:00 PM

Write a program to play a peg jumping game (examples can be played online here and here). Running the program looks like what is shown below, where the boldfaced text is something that you type in.

Author: Dale Reed
Assignment: 2, PegJump
TA: Englebert Humperdink, Mon 1:00-1:05

This program represents the peg jumping puzzle.

The board starts out with a single blank position, represented by
the 'O'.  To make a move, select the letter of the peg to be moved,
then the letter of the destination position. (e.g. the first move
might be: d a, meaning we move peg ‘d’ into blank position ‘a’.)
A peg must be able to jump over an adjacent peg into a blank for a 
move to be valid.  The jumped peg is then removed from the board.  
The game is over when there are no valid moves left to be made, or
when there is a single peg left.

At any point enter 'x' to exit the program.
-----------------------
  Board    Positions
    o          A              
   + +        B C            
  + + +      D E F         
 + + + +    G H I J      
+ + + + +  K L M N O  

1. Enter your move: D a


-----------------------
  Board    Positions
    +          A              
   o +        B C            
  o + +      D E F         
 + + + +    G H I J      
+ + + + +  K L M N O  

2. Enter your move: K D


-----------------------
  Board    Positions
    +          A              
   o +        B C            
  + + +      D E F         
 o + + +    G H I J      
o + + + +  K L M N O  

3. Enter your move: i g


-----------------------
  Board    Positions
    +          A              
   o +        B C            
  + + +      D E F         
 + o o +    G H I J      
o + + + +  K L M N O  

4. Enter your move: n p
   *** Invalid destination.  Please retry ****
4. Enter your move: q n
   *** Invalid source.  Please retry ****
4. Enter your move: j h
   *** Must jump a piece.  Please retry. ***
4. Enter your move: b i
   *** Source must have a piece.  Please retry. ***
4. Enter your move: l n
   *** Destination must be empty.  Please retry. ***
4. Enter your move: g b

-----------------------
  Board    Positions
    +          A              
   + +        B C            
  o + +      D E F         
 o o o +    G H I J      
o + + + +  K L M N O  

5. Enter your move: a d


-----------------------
  Board    Positions
    o          A              
   o +        B C            
  + + +      D E F         
 o o o +    G H I J      
o + + + +  K L M N O  

6. Enter your move: c h 


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  + o +      D E F         
 o + o +    G H I J      
o + + + +  K L M N O  

7. Enter your move: l e


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  + + +      D E F         
 o o o +    G H I J      
o o + + +  K L M N O  

8. Enter your move: j c


-----------------------
  Board    Positions
    o          A              
   o +        B C            
  + + o      D E F         
 o o o o    G H I J      
o o + + +  K L M N O  

9. Enter your move: c h


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  + o o      D E F         
 o + o o    G H I J      
o o + + +  K L M N O  

10. Enter your move: n l


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  + o o      D E F         
 o + o o    G H I J      
o + o o +  K L M N O  

11. Enter your move: d m


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  o o o      D E F         
 o o o o    G H I J      
o + + o +  K L M N O  

12. Enter your move: l n


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  o o o      D E F         
 o o o o    G H I J      
o o o + +  K L M N O  

13. Enter your move: o m


-----------------------
  Board    Positions
    o          A              
   o o        B C            
  o o o      D E F         
 o o o o    G H I J      
o o + o o  K L M N O  


# left  Rating
------  ---------------
  >4    Dufus
  4     Poor
  3     Mediocre
  2     Good Job
  1     Awesome Genius!

You had 1 left.  Awesome Genius!

Thanks for playing.  Exiting...

You need to know the following concepts in order to write this program:

Variables, assignment statements, loops, if statements, copying your program into your CS account, using turnin on your CS account.

Notes:

  1. Giving input of 'x' as the first input for every move must result in displaying the ratings table and then exiting the program.
  2. As illustrated above, either lower case or upper case should be accepted for input. You may assume that the user gives exactly one character, one space, then a second character on each input line (unless they enter 'x' for exit).
  3. I suggest you write the program in the following stages, compiling it and running your code at each stage. The number of run-time points (out of 55 total) for an otherwise perfect program are shown below. Note that unless your program doesn't run substantially according to program requirements, you don't get any points at all (score of 0 for the entire program.)
    1. (0 points) Use a template to start your program, instead of starting with a blank document. I suggest you use this one. Note that your Java class must be called  PegJump.java with an upper-case 'P' and an upper-case 'J', and the other letters all lower case.
    2. (0 points) Create 15 char variables (e.g. char c1,c2,c3,c4,c5,.... all the way up to 15), all (except c1) initialized to '+' (e.g. c2=c3=c4...=c15='+';). Initialize c1 to 'o'. Write the code to display the board and the positions.
    3. (0 points) Create the two char variables that will store the user move positions. Create a String variable to hold user input. Write the code to prompt for user input and read it into the declared string. Use the charAt() method to retrieve the two characters from the input String. For instance, to retrieve the first character (in position 0) you would use something like: firstChar = userInput.charAt( 0); Check to see if the first character is an 'x'. If it is, simply display a message for now.
    4. (15 points) Write the code to make a single move, which consists of:
      1. Placing an 'o' in the variable corresponding to the position we are moving from.
      2. Place an 'o' in the variable corresponding to the position we are jumping over
      3. Place an '+' in the variable corresponding to the destination position
    5. (10 points) Place the code that displays the board and prompts for a move into a loop. The loop should continue while the number of pieces remaining is greater than 1.
    6. (10 points) Write the code to display the table at the end of the program. Add a variable to your program to count how many pegs are remaining on the board. Write the "if" statement(s) or switch-case statements to display the correct ending message, depending on how many pieces are remaining. Also change the code that displays a message when the user selects 'x' to exit. Rather than just displaying a message, write the code to have it now break out of the loop, which should have the effect of branching down to the code that displays the Ratings table.
    7. (20 points) Write the input verification code, with the appropriate error message for each different kind of input error, as shown in move #4 in the sample program run above. You must verify:
      1. The source is a valid character (between 'a' and 'o')
      2. The destination is a valid character (between 'a' and 'o')
      3. The source has a peg (is '+')
      4. The destination is blank (is 'o')
      5. The piece being jumped has a peg (is '+')

  4. Turnin your program electronically using the "turnin" command from your CS account on ernie.cs.uic.edu as follows:
  5. turnin -c cs107 -p program2 PegJump.java
    where the single file containing the solution for this project is called PegJump.java. Do not turn in a directory containing the file, only turn in that one file. Failure to follow this instruction will result in a 5 point deduction. See the instructions given at the end of program1 to see how you can verify that the turned in program is in the CS107 course account.

 


[CS Dept] [UIC] [Prof. Reed]