Java Reference
In-Depth Information
be allowed to guess letters. If a letter appears in the word, then they should be told at what
position. After 6 guesses, the player has one shot of guessing the word. A possible run of
the program follows (user input is in italic).
Player 1 enter word: mississippi
Player 2 enter guess: c
No letter c
Player 2 enter guess: d
no letter d
Player 2 enter guess: e
no letter e
Player 2 enter guess: m
at position: 1
Player 2 enter guess: a
no letter a
Player 2 enter guess: i
at position 2 5 8 11
Enter word: mississippi
This is correct!
Use a for loop to iterate through all the guesses. Use a nested for loop to iterate through
all the characters of the word. When there is a hit (i.e., the user character matches a word
character), report the value of the index of the character in the word. Do not forget to add
one to the result because the characters of a string are counted starting at index 0.
3.11 Project
The game of Nim is played as follows. You have n toothpicks to choose from. Player
A can remove 1, 2, or 3 toothpicks. Player B can then remove 1, 2, or 3 toothpicks. This
process repeats, alternating between players A and B , until there are no more toothpicks
left. The player who removes the last toothpick is the loser. Assume that one of the players
is human and the other player is the computer (your program).
Use a random generator to decide who goes first. The player that goes second chooses
the value of n ,where n must be bigger than 20 and smaller than 30 (if this is the computer,
then use a random generator to determine the value of n ). Remember to print the remaining
number of toothpicks on the screen after every move. If it is the human's turn, then ask him
or her how many toothpicks he or she wants to remove. After the last toothpick is removed,
print “YOU WIN” or “YOU LOSE”.
Example program run (player input in italic ) follows.
Welcome to my game of Nim.
This time you are going to go first.
I choose the total number of toothpicks to be 21.
Your Turn. There are 21 toothpicks left. How many toothpicks do you want to remove? 2
My turn. There are 19 toothpicks left. I, the Computer, will remove 3 of them.
Your Turn. There are 16 toothpicks left.
Search WWH ::

Custom Search