Java Reference
In-Depth Information
PROGRAMMING EXAMPLE: The Rock, Paper, and Scissors Game
Everyone is familiar with the rock, paper, and scissors game. The game has two
players, each of whom chooses one of the three objects: rock, paper, or scissors. If
player 1 chooses rock and player 2 chooses paper, player 2 wins the game because
paper covers the rock. The game is played according to the following rules:
￿ If both players choose the same object, this play is a tie.
￿ If one player chooses rock and the other chooses scissors, the player choosing
the rock wins this play because the rock crushes the scissors.
￿ If one player chooses rock and the other chooses paper, the player choosing
the paper wins this play because the paper covers the rock.
￿ If one player chooses scissors and the other chooses paper, the player choos-
ing the scissors wins this play because the scissors cut the paper.
We write an interactive program that allows two players to play this game.
Input: This program has two types of input:
￿ The players' responses to play the game
￿ The players' choices
Output: The players' choices and the winner of each play. After the game is over, the
total number of plays and the number of times that each player won should be output
as well.
Two players play this game. Players enter their choices via the keyboard. Each player
enters R or r for Rock, P or p for Paper, or S or s for Scissors. While the first player
enters a choice, the second player looks away. Once both entries are in, if the entries
are valid, the program outputs the players' choices and declares the winner of the
play. The game continues until one of the players decides to quit. After the game
ends, the program outputs the total number of plays and the number of times that
each player won. This discussion translates into the following algorithm:
1. Provide a brief explanation of the game and how it is played.
2. Ask the users if they want to play the game.
3. Get plays for both players.
4. If the plays are valid, output the plays and the winner.
5. Update the total game count and winner count.
6. Repeat Steps 2-5 while the users continue to play the game.
7. Output the number of plays and times that each player won.
PROBLEM
ANALYSIS
AND
ALGORITHM
DESIGN
Search WWH ::




Custom Search