Results 1 to 4 of 4
Thread: Rock, paper, scissors
- 01-30-2011, 09:34 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Rock, paper, scissors
Hey there, I'm new to Java and made a simple rock, paper, scissors program using Random() and if-else. Just curious as to whether or not there are different ways of doing it (I'm sure there are, but this is what I came up with).
EDIT: Small fix when entering something other than rock, paper, or scissors.Java Code:// Rock, paper, scissors program that will ask for input // then compare to random choice by computer import java.util.Scanner; import java.util.Random; public class RockPaperScissors { public static void main(String[] args) { // Rock Paper and Scissors as Static Values int ROCK = 0; int SCISSORS = 1; int PAPER = 2; // Variable intChoice for comparing to randomInt int intChoice = 4; // New Scanner Scanner key = new Scanner(System.in); System.out.print("Please enter rock, paper, or scissors: "); String choice = new String(key.nextLine()); if (choice.compareToIgnoreCase("rock") == 0) intChoice = ROCK; else if (choice.compareToIgnoreCase("paper") == 0) intChoice = PAPER; else if (choice.compareToIgnoreCase("scissors") == 0) intChoice = SCISSORS; else System.out.println("You must enter either rock, paper, or scissors."); // New random generator Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(3); if (intChoice == randomInt) System.out.println("Tie!"); else if (intChoice == 0 && randomInt == 1) System.out.println("Rock beats scissors! You win!"); else if (intChoice == 0 && randomInt == 2) System.out.println("Paper covers rock... You lose."); else if (intChoice == 1 && randomInt == 0) System.out.println("Rock beats scissors... you lose."); else if (intChoice == 1 && randomInt == 2) System.out.println("Scissors cuts paper! You win!"); else if (intChoice == 2 && randomInt == 0) System.out.println("Paper covers rock! You win!"); else if (intChoice == 2 && randomInt == 1) System.out.println("Scissors cuts paper... You lose."); else System.out.println(); } }Last edited by StevenF; 01-30-2011 at 09:58 PM.
-
This is overkill, but you could always use a payoff matrix a la Game Theory.
- 01-30-2011, 10:24 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
It's a good idea to declare your constants as final, and to have them outside the body of the main method.
- 01-30-2011, 11:38 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Paper rock Scissors Game inheritance problem
By kaanax in forum New To JavaReplies: 7Last Post: 08-20-2010, 02:44 PM -
Stone, Paper, Scissors game help
By Harris68 in forum NetworkingReplies: 1Last Post: 12-19-2009, 04:33 PM -
Rock Paper Scissors
By 54byler in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 06:23 AM -
Need Help with Rock paper and Scissors Java Game
By kingsun in forum New To JavaReplies: 3Last Post: 11-17-2008, 03:35 AM -
Need help with Rock Paper Scissors Game
By GettinGwap in forum New To JavaReplies: 12Last Post: 10-19-2008, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks