|
|
Line 6: |
Line 6: |
| 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 in 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. During the execution of the program, the player may request a summary that prints the current score record. The score record includes the number of wins, loses, and ties, as well as the winning average (in percentage) and the player's highest winning streak. | | 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 in 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. During the execution of the program, the player may request a summary that prints the current score record. The score record includes the number of wins, loses, and ties, as well as the winning average (in percentage) and the player's highest winning streak. |
| | | |
− | |SideSection= | + | |SideSectionTitle=...by the Students |
− | [[Image:OperatingSystemExample.jpg|float|267px]]
| + | |SideSection=Whenever you are having trouble planning or solving a problem, writing a program, debugging a program, or just analyzing code, write/draw it out on paper. Step through each line of code so you know what you want the program to do, or what that code is doing. When you refer back to the code in your program, use print statements and test your variables to make sure your getting the same results or the results that you should be getting. When you write or draw each step on paper (ex: arrays), this gives you a visual of what your program is doing. In the case you are fixing errors, you can compare and match your program with your diagrams to pin point where the errors occur. |
− | <BR>
| + | |
− | Taken from http://www.flickr.com/photos/daniello/565304023/
| + | |
| | | |
− | An image or By Students section
| + | |Solution= |
| | | |
− | |Solution=The solution...
| |
| | | |
| | | |
Revision as of 01:44, 8 April 2010
Back to the Program-A-Day homepage
Problem
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 in 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. During the execution of the program, the player may request a summary that prints the current score record. The score record includes the number of wins, loses, and ties, as well as the winning average (in percentage) and the player's highest winning streak.
|
|
...by the Students
Whenever you are having trouble planning or solving a problem, writing a program, debugging a program, or just analyzing code, write/draw it out on paper. Step through each line of code so you know what you want the program to do, or what that code is doing. When you refer back to the code in your program, use print statements and test your variables to make sure your getting the same results or the results that you should be getting. When you write or draw each step on paper (ex: arrays), this gives you a visual of what your program is doing. In the case you are fixing errors, you can compare and match your program with your diagrams to pin point where the errors occur.
|
Solution
|
Code
Solution Code
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;
}
}
|
Back to the Program-A-Day homepage