Difference between revisions of "Rock Paper Scissors"
From CompSciWiki
Line 15: | Line 15: | ||
|Solution=The solution... | |Solution=The solution... | ||
+ | |||
+ | |SolutionCode= | ||
+ | <pre> | ||
+ | import javax.swing.JOptionPane; | ||
+ | |||
+ | public class RockPaperScissors { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | final int MAX = 1000; | ||
+ | final int ROCK = 0; | ||
+ | final int PAPER = 1; | ||
+ | final int SCISSORS = 2; | ||
+ | |||
+ | |||
+ | String input = ""; | ||
+ | int player; | ||
+ | int count = 0; | ||
+ | int [] rounds = new int [MAX]; | ||
+ | boolean loop = true; | ||
+ | |||
+ | System.out.println("Rock Paper Scissors!"); | ||
+ | |||
+ | while((count < MAX) && (loop)) { | ||
+ | input = JOptionPane.showInputDialog(null,"Enter an option: rock, paper, scissors, summary, or quit: "); | ||
+ | |||
+ | if((input == null) || (input.equalsIgnoreCase("quit"))) { | ||
+ | loop = false; | ||
+ | } | ||
+ | else if(input.equalsIgnoreCase("rock")) { | ||
+ | player = ROCK; | ||
+ | rounds[count] = playRound(player,ROCK,PAPER,SCISSORS); | ||
+ | count++; | ||
+ | } | ||
+ | else if (input.equalsIgnoreCase("paper")) { | ||
+ | player = PAPER; | ||
+ | rounds[count] = playRound(player,ROCK,PAPER,SCISSORS); | ||
+ | count++; | ||
+ | } | ||
+ | else if (input.equalsIgnoreCase("scissors")) { | ||
+ | player = SCISSORS; | ||
+ | rounds[count] = playRound(player,ROCK,PAPER,SCISSORS); | ||
+ | count++; | ||
+ | } | ||
+ | else if (input.equalsIgnoreCase("summary")) { | ||
+ | summary(rounds,count); | ||
+ | } | ||
+ | else { | ||
+ | System.out.println("Invalid input."); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | System.out.println("End of processing."); | ||
+ | System.exit (0); | ||
+ | |||
+ | } | ||
+ | |||
+ | public static int playRound(int player, int ROCK, int PAPER, int SCISSORS) { | ||
+ | int computer; | ||
+ | int result = 0; | ||
+ | |||
+ | computer = (int)(Math.random() * 3); | ||
+ | |||
+ | if((player == ROCK) && (computer == SCISSORS)) { | ||
+ | result = 1; | ||
+ | System.out.println("Computer chose scissors. You are victorious!"); | ||
+ | } | ||
+ | else if((player == ROCK) && (computer == PAPER)) { | ||
+ | result = 2; | ||
+ | System.out.println("Computer chose paper. You have been defeated."); | ||
+ | } | ||
+ | else if((player == PAPER) && (computer == ROCK)) { | ||
+ | result = 1; | ||
+ | System.out.println("Computer chose rock. You are victorious!"); | ||
+ | } | ||
+ | else if ((player == PAPER) && (computer == SCISSORS)) { | ||
+ | result = 2; | ||
+ | System.out.println("Computer chose scissors. You have been defeated."); | ||
+ | } | ||
+ | else if ((player == SCISSORS) && (computer == PAPER)) { | ||
+ | result = 1; | ||
+ | System.out.println("Computer chose paper. You are victorious!"); | ||
+ | } | ||
+ | else if ((player == SCISSORS) && (computer == ROCK)) { | ||
+ | result = 2; | ||
+ | System.out.println("Computer chose rock. You have been defeated."); | ||
+ | } | ||
+ | else if (player == computer) { | ||
+ | result = 3; | ||
+ | System.out.println("Computer chose the same. It's a draw!"); | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | |||
+ | } | ||
+ | |||
+ | public static void summary(int [] rounds, int count) { | ||
+ | int wins = 0; | ||
+ | int loses = 0; | ||
+ | int draws = 0; | ||
+ | double average; | ||
+ | int winStreak; | ||
+ | |||
+ | for(int i = 0; i < count; i++) { | ||
+ | if(rounds[i] == 1) { | ||
+ | wins++; | ||
+ | } | ||
+ | else if(rounds[i] == 2) { | ||
+ | loses++; | ||
+ | } | ||
+ | else if(rounds[i] == 3) { | ||
+ | draws++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | average = getAverage(wins,count); | ||
+ | winStreak = winningStreak(rounds,count); | ||
+ | |||
+ | System.out.println("===Summary==="); | ||
+ | System.out.println(""); | ||
+ | System.out.println("Total wins: " + wins); | ||
+ | System.out.println("Total losses: " + loses); | ||
+ | System.out.println("Total draws: " + draws); | ||
+ | System.out.println("Winning Average: " + average + "%"); | ||
+ | System.out.println("Win Streak: " + winStreak); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | public static double getAverage(int wins, int count) { | ||
+ | double average = 0; | ||
+ | int temp; | ||
+ | |||
+ | average = (double) wins / count; | ||
+ | average = average * 100; | ||
+ | temp = (int) (average * 10); | ||
+ | average = (double) temp / 10; | ||
+ | |||
+ | return average; | ||
+ | } | ||
+ | |||
+ | public static int winningStreak(int [] rounds, int count) { | ||
+ | int streak = 0; | ||
+ | int winStreak = 0; | ||
+ | |||
+ | for(int i = 0; i < count; i++) { | ||
+ | if(rounds[i] == 1) { | ||
+ | streak++; | ||
+ | } | ||
+ | else { | ||
+ | streak = 0; | ||
+ | } | ||
+ | |||
+ | if(streak > winStreak) { | ||
+ | winStreak = streak; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return winStreak; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
}} | }} |
Revision as of 02:24, 6 April 2010
Back to the Program-A-Day homepage
ProblemRock Paper Scissors Rock-Paper-Scissors is a game played by two players with a one in three chance of winning. The outcome of each round will result the player to either win, lose, or tie. In this example, we're going to make a simulation of the game, Rock-Paper-Scissors, to play against the computer. |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution... | ||
Code |