Java Reference
In-Depth Information
2. Write a reverse Hangman game in which the user thinks of a word and the computer tries to guess the letters in that
word. The user tells the computer how many letters the word contains.
3. Write a program that plays a guessing game with the user. The program should generate a random number between 1
and some maximum (such as 100 ), then prompt the user repeatedly to guess the number. When the user guesses incor-
rectly, the game should give the user a hint about whether the correct answer is higher or lower than the guess. Once the
user guesses correctly, the program should print a message showing the number of guesses that the user made.
Consider extending this program by making it play multiple games until the user chooses to stop and then printing
statistics about the player's total and average number of guesses.
4. Write a program that plays a reverse guessing game with the user. The user thinks of a number between 1 and 10 , and
the computer repeatedly tries to guess it by guessing random numbers. It's fine for the computer to guess the same
random number more than once. At the end of the game, the program reports how many guesses it made. Here is a
sample execution:
This program has you, the user, choose a number
between 1 and 10. Then I, the computer, will try
my best to guess it.
Is it 8? (y/n) n
Is it 7? (y/n) n
Is it 5? (y/n) n
Is it 1? (y/n) n
Is it 8? (y/n) n
Is it 1? (y/n) n
Is it 9? (y/n) y
I got your number of 9 correct in 7 guesses.
For an added challenge, consider having the user hint to the computer whether the correct number is higher or lower
than the computer's guess. The computer should adjust its range of random guesses on the basis of the hint.
5. Write a game that plays many rounds of Rock Paper Scissors. The user and computer will each choose between three
items: rock (defeats scissors, but loses to paper), paper (defeats rock, but loses to scissors), and scissors (defeats paper,
but loses to rock). If the player and computer choose the same item, the game is a tie.
You could extend this program to include different algorithmic strategies for choosing the best item. Should the com-
puter pick randomly? Should it always pick a particular item or a repeating pattern of items? Should it count the num-
ber of times the opponent chooses various items and base its strategy on this history?
6. Write a program that draws a graphical display of a 2D random walk using a DrawingPanel . Start a pixel walker in
the middle of the panel. On each step, choose to move 1 pixel up, down, left, or right, then redraw the pixel. (You can
draw a single pixel by drawing a rectangle of size 1
×
1.)
Search WWH ::




Custom Search