Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Iron Lion

Thread: I'm getting a weird output in my rock paper scissors spock game

  1. #1
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default 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.
    Java 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");
    		} 
    		
    	
    		
    	}
    		
    	}
    1. Play Game
    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

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default Re: I'm getting a weird output in my rock paper scissors spock game

    Firstly,
    You chose L //doesn't display the actual word
    You chose to print the input which is weaponChoice, it should just be weapon.
    Java Code:
    System.out.println("You chose " + weaponChoice);

  3. #3
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default 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");
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: I'm getting a weird output in my rock paper scissors spock game

    Quote Originally Posted by jammas615 View Post
    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");
    }
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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:
    Java Code:
            if (computerThrow=="Paper" && weapon =="Rock") {
                winner = 1;
            }
    should be this:
    Java Code:
            if (computerThrow.equals("Paper") && weapon.equals("Rock")) {
                winner = 1;
            }

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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.
    Fubarable likes this.

  7. #7
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default 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.......

  8. #8
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default 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);
    	}

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: I'm getting a weird output in my rock paper scissors spock game

    Why are you calling in.nextLine() twice?

  10. #10
    ss1
    ss1 is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Re: I'm getting a weird output in my rock paper scissors spock game

    Quote Originally Posted by Iron Lion View Post
    Why are you calling in.nextLine() twice?
    I've been used to doing that... so should I just say String weapon = in.nextLine?

Similar Threads

  1. Rock Paper Scissors Spock Lizard Player Problem
    By flyingcurry in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 12:19 AM
  2. Replies: 3
    Last Post: 02-17-2011, 02:42 AM
  3. Paper rock Scissors Game inheritance problem
    By kaanax in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 02:44 PM
  4. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 03:35 AM
  5. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •