Now we have an improved version of the game Rock Paper and S
Now we have an improved version of the game “Rock, Paper, and Scissor”, now make it more ROBUST by doing Error Checking for user input.
IMPROVED VERSION OF THE GAME BELOW:
package com.ssg;
import javax.swing.JOptionPane;
public class FirstGame {
public static void main(String args[]) {
// Variable declaration
int yourChoice, computerChoice; // comment please!
String yourInput; // comment please!
// Welcome screen and show the basic rule to the user
JOptionPane.showMessageDialog(null,
\"Welcome to Paper, Scissor and Rock\");
JOptionPane
.showMessageDialog(
null,
\"Let me remind you the rule first:\ Scissor cuts paper, paper covers rock, and rock breaks scissors.\");
JOptionPane.showMessageDialog(null, \"0: Rock\ 1: Paper\ 2: Scissor\");
// Get your choice
yourInput = JOptionPane.showInputDialog(\"Now tell me your choice!\");
yourChoice = Integer.parseInt(yourInput);
// Get computer choice
computerChoice = (int) (Math.random() * 10); // generate a random number
computerChoice %= 3; // Why we need to modulus 3 here? Think about it.
// Compare your Choice with computer’s choice and output the result
// Case I: Both you and computer pick the same choice, then it’s a tie!
if (yourChoice == computerChoice) {
JOptionPane.showMessageDialog(null, \"It’s a tie!\");
}
// Case II: You WIN :)
// if (yourChoice > computerChoice){
// JOptionPane.showMessageDialog(null,\"You Win!\");
else if (yourChoice == 0 && computerChoice == 1) {
JOptionPane.showMessageDialog(null, \"You chose 2 \"
+ \"\ Computer generated 1 \" + computerChoice
+ \"/n so you win!\");
}
else if (yourChoice == 0 && computerChoice == 2) {
JOptionPane.showMessageDialog(null, \"You chose 0 \"
+ \"\ Computer generated 2 \" + computerChoice
+ \"/n so you win!\");
} else if (yourChoice == 1 && computerChoice == 0) {
JOptionPane.showMessageDialog(null, \"You chose 1 \"
+ \"\ Computer generated 0\" + computerChoice
+ \"/n so you win!\");
}
// Case III: You lose :(
// if (yourChoice < computerChoice){
// JOptionPane.showMessageDialog(null,\"You lose\");
else if (yourChoice == 2 && computerChoice == 0) {
JOptionPane.showMessageDialog(null, \"You chose 2 \"
+ \"\ Computer generated 0 \" + computerChoice
+ \"/n so you lose!\");
} else if (yourChoice == 0 && computerChoice == 1) {
JOptionPane.showMessageDialog(null, \"You chose 0 \"
+ \"\ Computer generated 1 \" + computerChoice
+ \"/n so you lose!\");
} else if (yourChoice == 1 && computerChoice == 2) {
JOptionPane.showMessageDialog(null, \"You chose 1 \"
+ \"\ Computer generated 2 \" + computerChoice
+ \"/n so you lose!\");
}
}
}
Solution
check the modified code
package firstgame;
import javax.swing.JOptionPane;
public class FirstGame {
public static void main(String args[]) {
// Variable declaration
int yourChoice, computerChoice; // comment please!
String yourInput; // comment please!
// Welcome screen and show the basic rule to the user
JOptionPane.showMessageDialog(null,
\"Welcome to Paper, Scissor and Rock\");
JOptionPane
.showMessageDialog(
null,
\"Let me remind you the rule first:\ Scissor cuts paper, paper covers rock, and rock breaks scissors.\");
JOptionPane.showMessageDialog(null, \"0: Rock\ 1: Paper\ 2: Scissor\");
// Get your choice
yourInput = JOptionPane.showInputDialog(\"Now tell me your choice!\");
yourChoice = Integer.parseInt(yourInput);
//ERROR CHECKING
while(yourChoice !=0 && yourChoice !=1 && yourChoice !=2)
{
JOptionPane.showMessageDialog(null, \"You should select 0, 1 or 2 \");
yourInput = JOptionPane.showInputDialog(\"Now tell me your choice!\");
yourChoice = Integer.parseInt(yourInput);
}
// Get computer choice
computerChoice = (int) (Math.random() * 10); // generate a random number
computerChoice %= 3; // Why we need to modulus 3 here? Think about it.
// Compare your Choice with computer’s choice and output the result
// Case I: Both you and computer pick the same choice, then it’s a tie!
if (yourChoice == computerChoice) {
JOptionPane.showMessageDialog(null, \"It’s a tie!\");
}
// Case II: You WIN :)
// if (yourChoice > computerChoice){
// JOptionPane.showMessageDialog(null,\"You Win!\");
else if (yourChoice == 0 && computerChoice == 1) {
JOptionPane.showMessageDialog(null, \"You chose 2 \"
+ \"\ Computer generated 1 \" + computerChoice
+ \"/n so you win!\");
}
else if (yourChoice == 0 && computerChoice == 2) {
JOptionPane.showMessageDialog(null, \"You chose 0 \"
+ \"\ Computer generated 2 \" + computerChoice
+ \"/n so you win!\");
} else if (yourChoice == 1 && computerChoice == 0) {
JOptionPane.showMessageDialog(null, \"You chose 1 \"
+ \"\ Computer generated 0\" + computerChoice
+ \"/n so you win!\");
}
// Case III: You lose :(
// if (yourChoice < computerChoice){
// JOptionPane.showMessageDialog(null,\"You lose\");
else if (yourChoice == 2 && computerChoice == 0) {
JOptionPane.showMessageDialog(null, \"You chose 2 \"
+ \"\ Computer generated \" + computerChoice
+ \" so you lose!\");
} else if (yourChoice == 0 && computerChoice == 1) {
JOptionPane.showMessageDialog(null, \"You chose 0 \"
+ \"\ Computer generated \" + computerChoice
+ \" so you lose!\");
} else if (yourChoice == 1 && computerChoice == 2) {
JOptionPane.showMessageDialog(null, \"You chose 1 \"
+ \"\ Computer generated \" +computerChoice
+ \" so you lose!\");
}
}
}


