Results 1 to 10 of 10
- 10-15-2011, 11:51 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
I'm getting a weird output in my rock paper scissors spock game
So I'm trying to create a rock paper scissors spock game where we use methods that display menu options to play the game, display the score, or quit, and checks for user validity. then the user inputs their choice, also asking for user validity. the computer throws a random choice and returns the value. then it's supposed to compare the two throws and returns who the winner is, and the scores are displayed.
1. Play GameJava Code:import java.util.Scanner; public class Project7 { public static void main(String[] args) { Scanner in = new Scanner (System.in); int displayMenu = 0; int displayweaponChoice = 0; do { displayMenu(); boolean test = true; while (test) { if (in.hasNextInt()) { displayMenu = in.nextInt(); test = false; } else { //user validity, i used the letter a System.out.println("Invalid Input"); String dummy = in.next(); } } String weapon =""; String computerThrow =""; switch (displayMenu) { case 1: displayweaponChoice(); weaponChoice(); computerThrow(); compareChoices( ); break; } } while (displayMenu!= 3); } public static void displayMenu () { Scanner in = new Scanner(System.in); System.out.println("1. Play Game"); System.out.println("2. Show Score"); System.out.println("3. Quit"); } public static void displayweaponChoice ( ) { Scanner in = new Scanner (System.in); int displayweaponChoice = 0; System.out.println("Choose your weapon"); System.out.println("Enter R for Rock"); System.out.println("Enter P for Paper"); System.out.println("Enter S for Scissors"); System.out.println("Enter L for Lizard"); System.out.println("Enter O Spock"); if (in.hasNextInt()) { //another user validity = i used a number here displayweaponChoice = in.nextInt(); System.out.println("invalid input"); } } public static void weaponChoice () { Scanner in = new Scanner (System.in); boolean test = true; String weaponChoice = in.nextLine(); String weapon = in.nextLine(); if (weaponChoice=="R") { weapon = "Rock"; } if (weaponChoice == "P"){ weapon ="Paper"; } if (weaponChoice =="S") { weapon = "Scissors"; } if (weaponChoice == "L"){ weapon = "Lizard"; } if (weaponChoice == "K") { weapon = "Spock"; } System.out.println("You chose " + weaponChoice); } public static void computerThrow(){ String computerThrow = ""; double random = Math.random(); int upperBound = 5; int lowerBound = 1; int rand = (int) ((Math.random()*(upperBound - lowerBound +1)) + lowerBound); if (rand == 1) { computerThrow = "Rock"; } else if (rand == 2) { computerThrow = "Paper"; } else if (rand == 3) { computerThrow = "Scissors"; } else if (rand == 4) { computerThrow = "Lizard"; } else if (rand==5){ computerThrow = "Spock"; } System.out.println("Computer chose " + computerThrow); } public static void compareChoices () { int winner=0; String computerThrow = ""; String weapon =""; if (computerThrow==weapon) { winner = 0; } if (computerThrow == "Scissors" && weapon == "Paper") { winner = 1; } if (computerThrow=="Paper" && weapon =="Rock") { winner = 1; } if (computerThrow == "Rock" && weapon == "Lizard") { winner = 1; } if (computerThrow == "Lizard" && weapon == "Spock") { winner = 1; } if (computerThrow == "Spock" && weapon=="Rock") { winner = 1; } if (computerThrow == "Rock" && weapon == "Paper") { winner = 1; } if (computerThrow == "Paper" && weapon == "Scissors") { winner = 1; } if(computerThrow == "Scissors" && weapon == "Lizard") { winner = 1; } if (computerThrow == "Lizard" && weapon == "Spock") { winner = 1; } if (computerThrow=="Spock"&& weapon=="Scissors") { winner = 1; } if (winner ==1) { System.out.println("Computer won"); } if (winner== 0) { System.out.println("Tie"); } if (winner == -2) { System.out.println("Player won"); } } }
2. Show Score
3. Quit
a
Invalid Input
1
Choose your weapon
Enter R for Rock
Enter P for Paper
Enter S for Scissors
Enter L for Lizard
Enter O Spock
R
You chose //Did not display the word "Rock"
Computer chose Lizard
Tie
1. Play Game
2. Show Score
3. Quit
1
Choose your weapon
Enter R for Rock
Enter P for Paper
Enter S for Scissors
Enter L for Lizard
Enter O Spock
3
invalid input
L
You chose L //doesn't display the actual word
Computer chose Lizard
Tie
1. Play Game
2. Show Score
3. Quit
1
Choose your weapon
Enter R for Rock
Enter P for Paper
Enter S for Scissors
Enter L for Lizard
Enter O Spock
P
You chose
Computer chose Paper
Tie // I'm getting a tie everytime, and it won't say whether or not the computer or the user won
1. Play Game
2. Show Score
3. Quit
2 //will not show score
1. Play Game
2. Show Score
3. Quit
3
- 10-16-2011, 12:06 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: I'm getting a weird output in my rock paper scissors spock game
Firstly,
You chose to print the input which is weaponChoice, it should just be weapon.You chose L //doesn't display the actual word
Java Code:System.out.println("You chose " + weaponChoice);
- 10-16-2011, 12:12 AM #3
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: I'm getting a weird output in my rock paper scissors spock game
Also, a better way to decide who wins is by using a heirachy of 'weapons'. Assign each weapon a number and then compare the numbers, you then only need one or two controll statements, like:
Java Code:int rock = 5; int paper = 4; int scissors = 3; etc... if (weapon > computerWeapon){ System.out.println("you win!"); } if (weapon < computerWeapon){ System.out.println("you lose!"); } else { System.out.println("tie"); }
-
Re: I'm getting a weird output in my rock paper scissors spock game
This may work for somethings but most definitely will not work for the logic of rock-paper-scissors, since there is no linear greater than or less than here, but rather a circular one: rock beats scissors, scissors beats paper and paper beats rock. If you wanted to do this more "esoterically" you could use an enum, and use a payoff matrix, but this is gross over-kill.
-
Re: I'm getting a weird output in my rock paper scissors spock game
Also, to the original poster, don't use == to compare Strings. Instead use the equals method.
so this:
should be this:Java Code:if (computerThrow=="Paper" && weapon =="Rock") { winner = 1; }
Java Code:if (computerThrow.equals("Paper") && weapon.equals("Rock")) { winner = 1; }
- 10-16-2011, 11:51 AM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: I'm getting a weird output in my rock paper scissors spock game
What about if you create a class called RSPObject that implements Comparable?
Java Code:class RSPObj implements Comparable { String name; RSPObj[] defeats; // doesn't need to be an array if it's just a standard 3-item game of RSP RSPObj[] losesTo; // ditto // constructor and getter/setter methods omitted public int compareTo(RSPObj obj) { // return 1 for object that it defeats, 0 for same object/tie, -1 for object that it loses to } } public class RSP { // declare and initialise RSPObj instances String[] outcomes = { "loses to", "ties with", "defeats" }; public void play(RSPObj player1Choice, RSPObj player2Choice) { System.out.println(player1Choice.name + " " + outcomes[player1Choice.compareTo(player2Choice) + 1] + " " + player2Choice.name); } }Last edited by Iron Lion; 10-16-2011 at 11:55 AM.
- 10-16-2011, 08:48 PM #7
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Re: I'm getting a weird output in my rock paper scissors spock game
I haven't learned arrays yet and if I used them I wouldn't get credit for it.......
- 10-16-2011, 09:37 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Re: I'm getting a weird output in my rock paper scissors spock game
Now when I pick a weapon the output looks like this:
Choose your weapon
Enter R for Rock
Enter P for Paper
Enter S for Scissors
Enter L for Lizard
Enter O Spock
R
You chose //will not display the word
Computer chose Scissors
Am I doing something wrong in the code?
Java Code:public static void weapon() { Scanner in = new Scanner (System.in); String weapon = in.nextLine(); weapon = in.nextLine(); if (weapon.equals("R")) { weapon = "Rock"; } else if (weapon.equals ("P")) { weapon ="Paper"; } else if (weapon.equals("S")) { weapon = "Scissors"; } else if (weapon.equals ("L")) { weapon = "Lizard"; } else if (weapon.equals("O")) { weapon = "Spock"; } System.out.println("You chose " + weapon); }
- 10-16-2011, 10:35 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: I'm getting a weird output in my rock paper scissors spock game
Why are you calling in.nextLine() twice?
- 10-16-2011, 10:55 PM #10
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Rock Paper Scissors Spock Lizard Player Problem
By flyingcurry in forum New To JavaReplies: 1Last Post: 03-03-2011, 12:19 AM -
Help with setting up a GUI for a Rock, Paper, Scissors game
By thorobred in forum New To JavaReplies: 3Last Post: 02-17-2011, 02:42 AM -
Paper rock Scissors Game inheritance problem
By kaanax in forum New To JavaReplies: 7Last Post: 08-20-2010, 02:44 PM -
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


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks