CS 124, Spring 2013
Lab 4: More Control

Once again this week, the main topic for the lab is control structures. You'll need some while loops and if statements, but you will also want to use at least one for loop in each of the two exercises.

Make a lab4 directory, as usual, to hold your work for this lab. You will need a copy of TextIO.java. Other than that, you will be writing two programs from scratch.

Your work for this lab is due next Thursday, at the beginning of the next lab. You should copy your lab4 folder into your homework folder, using either the GUI interface or command line. Make sure that you do all the exercises!

Don't forget about style!!

Exercise 1: Loopy Baseball

Write a program that simulates a baseball game in the following sense. In each inning, each team scores some random number of runs. Add the number of runs to the team's score. Report the number of runs by each team and report the score after each inning. Use a loop to run the game for (at least) nine innings. Use Math.random() to generate the number of runs for each team. At the end, say which team won. If the scores are the same at the end of nine innings, you might say that the game is a tie. (Of course, that's not the way baseball really works.) The output should look pretty much like this:

            Inning 1:
                The Yankees score 1
                The Red Sox score 2
                Score at the end of the inning:  1 to 2
            Inning 2:
                The Yankees score 0
                The Red Sox score 1
                Score at the end of the inning:  1 to 3
            Inning 3:
                The Yankees score 4
                The Red Sox score 0
                Score at the end of the inning:  5 to 3
             Inning 9:
                The Yankees score 0
                The Red Sox score 1
                Score at the end of the innning:  5 to 6
             The Red Sox win.

You should think about the best way to give a team a "random number of runs." Just picking a random number between, say, 0 and 5, is a possibility, but not very realistic. You might think about trying for something more realistic. For example, the most likely number of runs in an inning is zero. There are other ways to increase the realism. For example, the second team doesn't bat in the ninth inning if they are ahead at the middle of the inning. And the game should really continue if the score is tied after nine innings. For full credit, you should try for a reasonably realistic simulation. (Extra credit might be given for particularly nice approaches.)

Exercise 2: Towards Hangman

In this exercise, you will write some programming that is needed in the well-known game, Hangman. For this exercise, you will work on just a part of the game. Later, we might extend this work to an implementation of the complete game.

In Hangman, a player tries to guess a word by guessing letters that might be in the word. The player initially sees a blank space for each letter of the word. As the player guesses letters, the blank spaces are filled in, until the user has guessed all the letters that occur in the word. (In the real game, the user loses if they guess too many letters that don't occur in the word.)

Your program should use some particular word, of your choice, as the word that the user has to guess. Let's say, for example, that the word is "hippopotamus". You want to show the user the word with all the guessed letters filled in, and all the unguessed letters blank. Use an underscore character, '_', to represent a blank, unguessed letter. At the beginning, all the spaces are blank. As the user guesses letters, some of the blanks are changed to letters. Here is approximately what the process should look like:

        The word so far:  _ _ _ _ _ _ _ _ _ _ _ _  

        Choose a letter: t
        Yes, there is a t in the word.
        The word so far:  _ _ _ _ _ _ _ t _ _ _ _ 

        Choose a letter: e
        No, there is no e in the word.
        The word so far:  _ _ _ _ _ _ _ t _ _ _ _ 

        Choose a letter: i
        Yes, there is a i in the word.
        The word so far:  _ i _ _ _ _ _ t _ _ _ _ 

        Choose a letter: o
        Yes, there is a o in the word.
        The word so far:  _ i _ _ o _ o t _ _ _ _ 

        Choose a letter: n
        No, there is no n in the word.
        The word so far:  _ i _ _ o _ o t _ _ _ _ 

        Choose a letter: s
        Yes, there is a s in the word.
        The word so far:  _ i _ _ o _ o t _ _ _ s 
        The word so far:  h i _ _ o _ o t a m u s

        Choose a letter: p
        Yes there is a p in the word.
        The word so far:  h i p p o p o t a m u s

        You've done it!

To implement this, you need to be able to check whether a string contains a given letter. Fortunately, Java makes that easy. Suppose word is a String and letter is a char. Then the function


checks whether letter occurs in word. This function returns an int. The value is -1 if letter does not occur in word. If letter does occur in word, then the value is greater than or equal to zero. (In fact, the value gives the position of the first occurrence of letter in word.) So, you can test whether a letter occurs in a word simply by saying

           if ( word.indexOf(letter) >= 0 ) { 

Your program will need a String to hold the word that the user is trying to guess. You will also need a String to hold the letters that the user has guessed so far. This second string starts out as the empty string, "". When the user inputs a character, add that character onto the string using a command such as

           guessedLetters = guessedLetters + userLetter;

The hardest part of the program is to output the word showing some letters and some blanks. Think carefully about how you are going to do that and how you are going to use the two strings to do it.

The program should continue until all the letters in the word have been guessed. Once the word is complete, you should end the program. (This part is also pretty hard, and we might discuss it in class once you've had a chance to work on it a bit.)