public class FirstRPSGame {
public static void main(String args[]) throws Exception {
//Variable declaration
int yourChoice, computerChoice;
String yourInput = "";
//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:\nScissor cuts paper, paper covers rock, and rock breaks scissors.");
JOptionPane.showMessageDialog(null, "0: Rock\n1: Paper\n2: Scissor");
//Get your choice
try {
yourInput = JOptionPane.showInputDialog("Now tell me your choice!");
yourChoice = Integer.parseInt(yourInput);
if("0".equals(yourInput) || "1".equals(yourInput) || "2".equals(yourInput)) {
}else {
throw new MyException("Please enter choice 0-2");
}
}
catch(MyException e) {
System.out.println(e);
}
//computer choice
computerChoice = (int)(Math.random()*10);
computerChoice %= 3;
//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 ("0".equals(yourInput) && "0".equals(computerChoice)){
JOptionPane.showMessageDialog(null, "It’s a tie!");
}else if("1".equals(yourInput) && "1".equals(computerChoice)) {
JOptionPane.showMessageDialog(null, "It’s a tie!");
}else if("2".equals(yourInput) && "2".equals(computerChoice)) {
JOptionPane.showMessageDialog(null, "It’s a tie!");
}
//Case II: You WIN :)
if ("0".equals(yourInput) && "2".equals(computerChoice)) {
JOptionPane.showMessageDialog(null, "You win!");
}else if("1".equals(yourInput) && "0".equals(computerChoice)){
JOptionPane.showMessageDialog(null, "You win!");
}else if("2".equals(yourInput) && "1".equals(computerChoice)) {
JOptionPane.showMessageDialog(null, "You win!");
}
//Case III: You lose :
if ("0".equals(computerChoice) && "2".equals(yourInput)) {
JOptionPane.showMessageDialog(null, "You lose!");
}else if("1".equals(computerChoice) && "0".equals(yourInput)) {
JOptionPane.showMessageDialog(null, "You lose!");
}else if("2".equals(computerChoice) && "1".equals(yourInput)) {
JOptionPane.showMessageDialog(null, "You lose!");
}
}
}