I thought it would be neat to try and apply what I read about methods to the idea and this is what I came up with.
/** * A simple Rock Paper Scissors game * * @author Andrew Hood * @version 2/11/2012 */ import java.util.*; class RockPaperScissors { public static int playerScore(int winner) { int xyz =0; if(winner == 1) { xyz++; } else if(winner == 2) { xyz--; } return xyz; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int playerChoice, compChoice, playAgain = 0, playerWins = 0; System.out.println("Rock Paper Scissors!\n"); while(playAgain!=9) // Contine loop until sentinel { System.out.println("[1]Rock, [2]Paper, [3]Scissors"); playerChoice = input.nextInt(); compChoice = (int) (3*Math.random()+1); switch(playerChoice) { case 1: System.out.print("You chose Rock, "); if(compChoice == 1) { System.out.println("Computer chose Rock."); System.out.println("Draw!"); } if(compChoice == 2) { System.out.println("Computer chose Paper."); System.out.println("Computer wins!"); playerWins += playerScore(2); } if(compChoice == 3) { System.out.println("Computer chose Scissors."); System.out.println("Player wins!"); playerWins += playerScore(1); } break; case 2: System.out.print("You chose Paper, "); if(compChoice == 1) { System.out.println("Computer chose Rock."); System.out.println("Player Wins!"); playerWins += playerScore(1); } if(compChoice == 2) { System.out.println("Computer chose Paper."); System.out.println("Draw!"); } if(compChoice == 3) { System.out.println("Computer chose Scissors."); System.out.println("Computer wins!"); playerWins += playerScore(2); } break; case 3: System.out.print("You chose Scissors, "); if(compChoice == 1) { System.out.println("Computer chose Rock."); System.out.println("Computer Wins!"); playerWins += playerScore(2); } if(compChoice == 2) { System.out.println("Computer chose Paper."); System.out.println("Player wins!"); playerWins += playerScore(1); } if(compChoice == 3) { System.out.println("Computer chose Scissors."); System.out.println("Draw!"); } break; default: System.out.println("You must choose 1,2 or 3"); break; } // End switch System.out.println("Your score is: " + playerWins); System.out.println("Would you like to play again? [1]Yes, [9]No"); playAgain = input.nextInt(); } // End while loop } // End public static void main } // End RockPaperScissors
I was thinking about making a two player version, where your choice would influence but not dictate what you got. That way even though the other person saw what you put. You still have a chance to win. Like computer vs computer where you both take sides.