Homework 7 : WheelOfFortune
CMP12a Winter 2002
Thomas Raffill

Due on the evening of Monday, May 13 by midnight.

Note: This assignment is graded, so do your best! Start early!

Introduction

The program will run a simplified version of the famous "Wheel of Fortune" letter guessing game for a human versus the computer. You will create a class called WheelOfFortune.

The rules of the game are: play takes place in rounds. In each round, the players are trying to guess a secret phrase and take turns guessing a letter. The source of these secret phrases is an input text file. Whenever a player guesses a letter which is in the word, the occurrences of this letter are revealed, the player's score increases by the number of occurrences of the letter, and the player gets another turn. When one player guesses wrong, then the other player takes a turn to guess letters. The first player to complete the secret phrase (by guessing the final missing letter) wins the round and keeps the accumulated score. The loser of the round does not keep any score for that round. Then another round starts until the supply of secret phrases from the input file is exhausted. Finally, the players get total scores based on their accumulated scores from all the rounds, and the one with the higher score is declared the winner. More detailed rules are described below.

Input

A file containing a list of secret phrases to be guessed by the players, and an integer to seed the random number generator. The input file name and random seed are passed to the program as command-line arguments. You don't need to validate the input! In other words, you can assume the program will always be run with two command line arguments (one file name and one integer). Of course, you will have to use Java's exception handling in case something goes wrong reading the file or during user interaction. Important! Your program must take a random number seed command line argument to make the random behavior reapeatable. The behavior of your program must be EXACTLY as in the example given below. The graders will rely on this when they mark your program, and they will take points off if your program does not match these specifications even in minor ways!

Output

The program runs a human-versus-computer wheel of fortune game interactively through standard input and standard output. In the beginning, the secret phrase is displayed with the letters replaced by asterisks. For example, if the secret phrase is "A PICTURE IS WORTH A THOUSAND WORDS" then the display  will  look like

 

* ******* ** ***** * ******** *****

 

 For simplicity we will assume that there are no punctuation marks in the secret phrase and all letters are always uppercase. When the user guesses letters, you can use the toUpperCase() method in the String class to guarantee that they are uppercase. The human player is  prompted to guess first by a prompt, Please guess a letter. If the guess is right, then the occurrences of the letter are displayed, the human's score is increased by the number of occurrences of the letter, the updated score is given in a message You guessed right. Your score is now W. (where W is the score), and the human is prompted again to guess a letter. This continues until the human guesses wrong. Then the message is displayed, You guessed wrong. The current pattern of known letters of the secret word is displayed, then the computer guesses randomly and the message is displayed, The computer guesses @.(where @ is the random letter). If the computer guesses right, the occurrences of the letter are displayed, the computer's score is increased by the number of occurrences of the letter, the updated score is given in a message The computer guessed right. The computer's score is now X. (where X is the score), and the computer guesses again. This continues until the computer guesses wrong. Then the message is displayed, The computer guessed wrong. The human and the computer go on taking turns in this way until the secret phrase is completely discovered. Depending on which player discovered the last letter to complete the secret phrase, there is a message, You win this round. or The computer wins this round. The winner's total score for all rounds is increased by the score for that round. Then both players' total score for all rounds is displayed in the message, Your score for all rounds is Y. and The computer's score for all rounds is Z (where Y and Z are the total scores). The game continues until all the lines of the file of secret phrases are exhausted. Then there are messages like,

 

Your score for all rounds is X.

The computer’s score for all rounds is Y.

You win this game. ( or The computer wins this game. or The game is tied.) (where the winner is the player with the higher score for all rounds).                                                                                                                                                                                       

Example

The file of secret phrases called wheel.txt contains the following:

A ROSE IS A ROSE 
STICKS NIX HICKS FLICKS
 

At the command line, the player enters: java WheelOfFortune wheel.txt 0  Then the following game takes place. NOTE: with a random number seed of 0, the computer should always guess letters in the order shown in this example: S, S, X, V, N, J, H, P, D, Q are the first 10 letters the computer should guess. So the program is REQUIRED to behave EXACTLY as in this example.

* **** ** * ****
Please guess a letter.
B
You guessed wrong.
The computer guesses S.
* **S* *S * **S*
The computer guessed right. 
The computer's score is now 3.
The computer guesses S.
The computer guessed wrong. 
Please guess a letter.
A
A **S* *S A **S*
You guessed right.
Your score is now 2.
Please guess a letter.
E
A **SE *S A **SE
You guessed right.
Your score is now 4.
Please guess a letter.
I
A **SE IS A **SE 
You guessed right.
Your score is now 5.
Please guess a letter.
D
You guessed wrong.
The computer guesses X.
The computer guessed wrong.
A **SE IS A **SE
Please guess a letter.
R
A R*SE IS A R*SE
You guessed right.
Your score is now 7.
Please guess a letter.
O
A ROSE IS A ROSE
You guessed right.
Your score is now 9.
You win this round. 
Your total score for all rounds is now 9. 
The computer's total score for all rounds is now 0.
 
****** *** ***** ******
Please guess a letter.
I
**I*** *I* *I*** **I***
You guessed right.
Your score is now 4.
Please guess a letter.
S
S*I**S *I* *I**S **I**S
You guessed right.
Your score is now 8.
Please guess a letter.
A
You guessed wrong.
The computer guesses V.
The computer guessed wrong.
Please guess a letter.
T
STI**S *I* *I**S **I**S
You guessed right.
Your score is now 9.
Please guess a letter
C
STIC*S *I* *IC*S **IC*S
You guessed right.
Your score is now 12.
Please guess a letter
K
STICKS *I* *ICKS **ICKS
You guessed right.
Your score is now 15.
Please guess a letter
W
You guessed wrong.
The computer guesses N.
The computer guessed right.
STICKS NI* *ICKS **ICKS
The computer guessed right.
The computer's score is now 1.
The computer guesses J.
The computer guessed wrong.
Please guess a letter.
H
STICKS NI* HICKS **ICKS
You guessed right.
Your score is now 16.
Please guess a letter
F
STICKS NI* HICKS F*ICKS
You guessed right.
Your score is now 17.
Please guess a letter
X
STICKS NIX HICKS F*ICKS
You guessed right.
Your score is now 18.
Please guess a letter
L
STICKS NIX HICKS FLICKS
You guessed right.
Your score is now 19.
You win this round. 
Your total score for all rounds is now 28. 
The computer's total score for all rounds is now 0.
You win this game. 
 
 
 Starter code
This is starter code to help you get started on your program.
               
import java.util.Random;
import java.io.*;
 
/* Homework 7 
 * WheelOfFortune.java
 *            
 * Author : Sung Kim <hunkim@cse>
 */
 
class WheelOfFortune {
 
  public static void main (String[] args) {
 
 
     }
 
}
 
 
-->

Extra Credit

For extra credit, create a smarter computer opponent. We aim for two smarter features. You can implement either one of them, or you can get even more extra credit by implementing both of them.

  1. Make the computer keep track of the already guessed letters during each round, make sure it doesn't guess letters that have already been eliminated.
  2. Make the computer aware of letter frequencies and guess accordingly. It is a well-known fact that certain letters are very common in English (for example, E, T, A) and other letters are very rare (for example, Q, X, Z). Studies show that the following are the rankings of the letters in order of frequency of occurrence from most common to most rare:

    E, T, A, O, N, R, I, S, H, D, L, F, C, M, U, G, Y, P, W, B, V, K, X, J, Q, Z

    That means in each round the computer will guess E first, T second, then A, O, N, and so on, always guessing letters in order of frequency.

Notes

·  Start early. Read the newsgroup.

·  Do your own work. Don't submit other people's code.

·  Remember to import java.util.Random and java.io.*.

·  You don't need to validate input values.

·  The extra credit is harder! Make sure you have a working solution to the basic problem before you attempt the extra credit problem! Also, if you have a working version, make sure you save a backup file of it before you modify it!

To Submit Homework

You only need to submit your source code. DON'T submit other files except your source code. When submitting homework, use the following form (when logged on to your cats account):

     submit cmps012a-tr.s02 hw7 WheelOfFortune.java
 
     submit is the script on cats you use to turn in your
     homework.
 
     cmps012a-tr.s02 is the class locker for this class. 
 
     hw7 is for homework 7. This designation will be hwn where n is the
     number of the homework assignment.
 
     WheelOfFortune.java is the source file name.

To Check Homework

     peek cmps012a-tr.s02 hw7

Questions ?

·  Use the newsgroup ucsc.class.cmps012a.

·  Attend labs and office hours.

·  Send an email to the instructor or TA.