6 Replies - 10972 Views - Last Post: 08 July 2011 - 09:52 AM Rate Topic: -----

#1 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Rock-Paper-Scissors-Lizard-Spock

Posted 14 March 2011 - 07:19 AM

I'm trying to write a program that allows a user to play Rock-Paper-Scissors-Lizard-Spock against the computer. I have to write this program using methods that do the following:
1. Display the menu options to play the game, score, or quit.
2. Asks the user to input their choice of rock, paper, scissors, lizard or spock.
3. Randomly assigns the computer's throw & returns the value
4. Compares the two throws & returns who the winner is.

So far, i think i have majority of the code down, but when i run it my output seems to be wrong. For example this is how it should look like when you run it:
Menu:
1.  Play game
2.  Show score
3.  Quit
1
Choose your weapon:
R.  Rock
P.  Paper
S.  Scissors
L.  Lizard
K.  Spock
P
You chose Paper
Computer chose Lizard
You lose



Instead, when I run it, this what my outout looks like:
Menu:
1.  Play game
2.  Show score
3.  Quit
1
Choose your weapon:
R.  Rock
P.  Paper
S.  Scissors
L.  Lizard
K.  Spock
R
You chose 
Computer chose Paper
You win
-1


It doesn't even print what You choose as your weapon, still somehow finds a winner & also prints the -1 which is just supposed to be an int value that is the value of you winning. This is my code so far:
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int menu = 0;
		do {
			displayMenuPlayGame();
			/*String input = in.nextLine();*/
			

			boolean test = true;

			while (test) {
				if (in.hasNextInt()) {
					menu = in.nextInt();
					test = false;
				} else {
					System.out.println("Invalid Input - Retry");
					String dummy = in.next();
				}
			}
			String weapon ="";
			String compThrow ="";
			switch (menu) {
			case 1:
				displayWeaponMenu();
				choose();
				computerThrow();
				compareChoices(weapon, compThrow);
				break;
			}
		} while (menu != 3);
	}

	// This method displays the menu
	public static void displayMenuPlayGame() {
		System.out.printf("\n");
		System.out.printf("\nMenu:");
		System.out.printf("\n1.  Play game");
		System.out.printf("\n2.  Show score");
		System.out.printf("\n3.  Quit");
		System.out.printf("\n");
	}


	// This method displays the weapon menu & plays the game
	public static void displayWeaponMenu() {
		System.out.println("Choose your weapon:");
		System.out.println("R.  Rock");
		System.out.println("P.  Paper");
		System.out.println("S.  Scissors");
		System.out.println("L.  Lizard");
		System.out.println("K.  Spock");
	}
	public static void choose() {
		Scanner in = new Scanner(System.in);
		boolean test1 = true;
		String weaponChoice = in.nextLine();
		String weapon = "";
		String compThrow = "";
		int winner ;
		if (weaponChoice == "R") {
			weapon = "Rock";
		}
		else if (weaponChoice == "P") {
	    	    weapon = "Paper";
		}
		else if (weaponChoice == "S") {
	    	    weapon = "Scissors";
		}
		else if (weaponChoice == "L") {
	    	    weapon = "Lizard";
		}
		else if (weaponChoice == "K") {
	    	    weapon = "Spock";
		}

		System.out.println("You chose "  +weapon );
		
		/*System.out.println(winner);*/
		

	
	}

	// This method randomly assigns the computer's throw
	public static void computerThrow() {
		String compThrow = "";
		double rando = Math.random();
		int upperBound = 5;
		int lowerBound = 1;
		int rand = (int)((Math.random() * (upperBound - lowerBound + 1)) + lowerBound);
		if (rand == 1) {
	    	    compThrow = "Rock";
		}
		else if (rand == 2) {
	    	    compThrow = "Paper";
		}
		else if (rand == 3) {
	    	    compThrow = "Scissors";
		}
		else if (rand == 4) {
	    	    compThrow = "Lizard";
		}
		else if (rand == 5) {
	    	    compThrow = "Spock";
		}

		System.out.println("Computer chose " + compThrow);

	}

	// This method compares the choices
	public static void compareChoices(String weapon, String compThrow) {
		/*1 = computer wins
		  0 = tie
		 -1 = user wins*/
		int winner;
		if (compThrow == weapon) {
	    	    winner = 0;
		}
		if (compThrow == "Scissors" && weapon == "Paper" ) {
	    	    winner = 1;
		}
		if (compThrow == "Paper" && weapon == "Rock" ) {
	    	    winner = 1;
		}
		if (compThrow == "Rock" && weapon == "Lizard" ) {
	    	    winner = 1;
		}
		if (compThrow == "Lizard" && weapon == "Spock" ) {
	    	    winner = 1;
		}
		if (compThrow == "Spock" && weapon == "Scissors" ) {
	    	    winner = 1;
		}
		if (compThrow == "Scissors" && weapon == "Lizard" ) {
	    	    winner = 1;
		}
		if (compThrow == "Lizard" && weapon == "Paper" ) {
	    	    winner = 1;
		}
		if (compThrow == "Paper" && weapon == "Spock" ) {
	    	    winner = 1;
		}
		if (compThrow == "Spock" && weapon == "Rock" ) {
	    	    winner = 1;
		}
		if (compThrow == "Rock" && weapon == "Scissors" ) {
	    	    winner = 1;
		}
		else {
		    winner = -1;
		}

		if (winner == 1) {
		    System.out.println("Computer wins");
		}
		if (winner == 0) {
		    System.out.println("Tie");
		}
		if (winner == -1) {
		    System.out.println("You win");
		}
		System.out.println(winner);
	}
}


Much help will be appreciated. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Rock-Paper-Scissors-Lizard-Spock

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10374
  • View blog
  • Posts: 38,411
  • Joined: 27-December 08

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 14 March 2011 - 07:25 AM

Don't compare Strings using the == operator, as it compares their locations in memory. Use the String equals() method instead, which compares their values.
Was This Post Helpful? 1
  • +
  • -

#3 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 14 March 2011 - 04:34 PM

Thank you. That worked. Now I am having another problem though, everytime i run & play the game, it always results in a tie? This is my new code after I did the corrections you told me.
public class assign14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int menu = 0;
		do {
			displayMenuPlayGame();
			/*String input = in.nextLine();*/
			

			boolean test = true;

			while (test) {
				if (in.hasNextInt()) {
					menu = in.nextInt();
					test = false;
				} else {
					System.out.println("Invalid Input - Retry");
					String dummy = in.next();
				}
			}
			String weapon ="";
			String compThrow ="";
			switch (menu) {
			case 1:
				displayWeaponMenu();
				choose();
				computerThrow();
				compareChoices(weapon, compThrow);
				break;
			}
		} while (menu != 3);
	}

	// This method displays the menu
	public static void displayMenuPlayGame() {
		System.out.printf("\n");
		System.out.printf("\nMenu:");
		System.out.printf("\n1.  Play game");
		System.out.printf("\n2.  Show score");
		System.out.printf("\n3.  Quit");
		System.out.printf("\n");
	}


	// This method displays the weapon menu & plays the game
	public static void displayWeaponMenu() {
		System.out.println("Choose your weapon:");
		System.out.println("R.  Rock");
		System.out.println("P.  Paper");
		System.out.println("S.  Scissors");
		System.out.println("L.  Lizard");
		System.out.println("K.  Spock");
	}
	public static void choose() {
		Scanner in = new Scanner(System.in);
		boolean test1 = true;
		String weaponChoice = in.nextLine();
		String weapon = "";
		String compThrow = "";
		int winner ;
		if (weaponChoice.equals("R")){
			weapon = "Rock";
		}
		else if (weaponChoice.equals("P")) {
	    	    weapon = "Paper";
		}
		else if (weaponChoice.equals("S")) {
	    	    weapon = "Scissors";
		}
		else if (weaponChoice.equals("L")) {
	    	    weapon = "Lizard";
		}
		else if (weaponChoice.equals("K")) {
	    	    weapon = "Spock";
		}

		System.out.println("You chose "  +weapon );
		
		/*System.out.println(winner);*/
		

	
	}

	// This method randomly assigns the computer's throw
	public static void computerThrow() {
		String compThrow = "";
		double rando = Math.random();
		int upperBound = 5;
		int lowerBound = 1;
		int rand = (int)((Math.random() * (upperBound - lowerBound + 1)) + lowerBound);
		if (rand == 1) {
	    	    compThrow = "Rock";
		}
		else if (rand == 2) {
	    	    compThrow = "Paper";
		}
		else if (rand == 3) {
	    	    compThrow = "Scissors";
		}
		else if (rand == 4) {
	    	    compThrow = "Lizard";
		}
		else if (rand == 5) {
	    	    compThrow = "Spock";
		}

		System.out.println("Computer chose " + compThrow);

	}

	// This method compares the choices
	public static void compareChoices(String weapon, String compThrow) {
		/*1 = computer wins
		  0 = tie
		 -1 = user wins*/
		int winner;
		if (compThrow.equals(weapon)) {
	    	    winner = 0;
		}
		else if (compThrow.equals("Scissors") && weapon.equals("Paper" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Paper") && weapon.equals("Rock" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Rock") && weapon.equals("Lizard" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Lizard") && weapon.equals("Spock" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Spock") && weapon.equals("Scissors" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Scissors") && weapon.equals("Lizard" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Lizard") && weapon.equals("Paper" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Paper") && weapon.equals("Spock" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Spock") && weapon.equals("Rock" )) {
	    	    winner = 1;
		}
		else if (compThrow.equals("Rock") && weapon.equals("Scissors" )) {
	    	    winner = 1;
		}
		else {
		    	winner = -1;
		}

		if (winner == 1) {
		    System.out.println("Computer wins");
		}
		if (winner == 0) {
		    System.out.println("Tie");
		}
		if (winner == -1) {
		    System.out.println("You win");
		}
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 15 March 2011 - 10:42 AM

yea for some reason it just always ends up in a tie. for example,
Menu:
1.  Play game
2.  Show score
3.  Quit
1
Choose your weapon:
R.  Rock
P.  Paper
S.  Scissors
L.  Lizard
K.  Spock
R
You chose Rock
Computer chose Spock
Tie

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10374
  • View blog
  • Posts: 38,411
  • Joined: 27-December 08

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 15 March 2011 - 10:59 AM

You declare these Strings, but don't read anything into them. You need to get input, but you need to assign it to these variables as well. That's why over-doing it with methods isn't a good thing.
String weapon ="";  
String compThrow ="";  



Also, why not just declare a Scanner global rather than keep declaring a Scanner in the method? Everytime the method is invoked, a new Scanner is instantiated.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 16 March 2011 - 05:02 AM

Your check method looks fine. This is how I tested it:
String optList[] = {"Rock","Paper","Scissors","Lizard","Spock"};

for(String weapon : optList) {
	for(String compThrow : optList) {
		System.out.print(weapon + " vs. " + compThrow + ": ");
		compareChoices(weapon, compThrow);
	}
}



You know, rather than passing all those strings about, you could just have a list and pass the position in the list...

Or an enum. An enum would be cool.

Ok, I couldn't resist. Hope this helps:
enum ElementType {
	Rock('R'), Paper('P'), Scissors('S'), Lizard('L'), Spock('K');

	private char ch;

	ElementType(char ch) { this.ch = ch; }

	public int beats(ElementType other) {
		if (this==other) { return 0; }
		return beatVerb(other)==null ? -1 : 1;
	}

	public String beatVerb(ElementType other) {
		if (this==Rock) {
			if (other==Scissors) { return "crushes"; }
			if (other==Lizard) { return "crushes"; }
		} else if (this==Paper) {
			if (other==Rock) { return "covers"; }
			if (other==Spock) { return "disproves"; }
		} else if (this==Scissors) {
			if (other==Paper) { return "cut"; }
			if (other==Lizard) { return "decapitate"; }
		} else if (this==Lizard) {
			if (other==Spock) { return "poisons"; }
			if (other==Paper) { return "eats"; }
		} else if (this==Spock) {
			if (other==Scissors) { return "smashes"; }
			if (other==Rock) { return "vaporizes"; }
		}
		return null;
	}

	public char getChar() { return ch; }


	public static ElementType fromChar(char ch) {
		for(ElementType t : ElementType.values()) {
			if (t.getChar()==ch) { return t; }
		}
		return null;
	}

	public static ElementType pickRandom() {
		int i = (int)(Math.random() * ElementType.values().length);
		return ElementType.values()[i];
	}
}


Was This Post Helpful? 1
  • +
  • -

#7 ripdajacker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Re: Rock-Paper-Scissors-Lizard-Spock

Posted 08 July 2011 - 09:52 AM

Your code is very complex for no particular reason. You can avoid the complexity by splitting the functionality into a few small classes.

The idea is to create a class, that handles the rules, so your UI (in your case the console) does not have to worry about the rather complex logic of the game.

The Move class:


/**
 * A move with a string representation.
 *
 * @author Jesenko Mehmedbasic
 * @since 7/8/11, 6:23 PM
 */
public class Move {
   private String representation;

   public Move(String representation) {
      this.representation = representation;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj instanceof Move) {
         Move other = (Move) obj;
         return representation.equals(other.representation);
      }
      return false;
   }

   @Override
   public String toString() {
      return representation;
   }
}



Coupled with a rule class:
package dk.sneakysensation.rockpaperscissors;

/**
 * Representing a rule.
 *
 * @author Jesenko Mehmedbasic
 * @since 7/8/11, 6:23 PM
 */
public class Rule {
   private Move winning;
   private Move losing;
   private String winningVerb;

   public Rule(Move winning, Move losing, String winningVerb) {
      this.winning = winning;
      this.losing = losing;
      this.winningVerb = winningVerb;
   }

   /**
    * This method determines a winner.
    *
    * @param move1 the first move.
    * @param move2 the second move.
    *
    * @return an integer between -1 and 2.
    *         <ul>
    *         <li>-1: the rule does not apply to the moves. </li>
    *         <li> 0: it is a tie.</li>
    *         <li> 1: move 1 wins.</li>
    *         <li> 2: move2 wins.</li>
    *         </ul>
    */
   public int getWinner(Move move1, Move move2) {
      if (move1 == move2) {
         return 0;
      }
      if (move1 == winning) {
         if (move2 == losing) {
            return 1;
         }
      }
      if (move1 == losing) {
         if (move2 == winning) {
            return 2;
         }
      }
      return -1;
   }

   public String getStringRepresentation() {
      return winning + " " + winningVerb + " " + losing + ".";
   }
}



These are bound together with a set of rules:

import java.util.ArrayList;
import java.util.List;

/**
 * A set of rules.
 *
 * @author Jesenko Mehmedbasic
 * @since 7/8/11, 6:41 PM
 */
public class RuleSet {
   private List<Rule> rules = new ArrayList<Rule>();

   public boolean add(Rule rule) {
      return rules.add(rule);
   }

   public int getWinner(Move move1, Move move2) {
      for (Rule rule : rules) {
         int winner = rule.getWinner(move1, move2);
         if (winner >= 0) {
            return winner;
         }
      }
      return -1;
   }
}



You could argue for an Enum instead of the int that is returned by getWinner, but as this is an example I did not bother. You can then create a new ruleset and add the rules:

/**
 * A game of rock paper scissors.
 * <p/>
 * This class utilizes the Move and Rule class to create a game.
 *
 * @author Jesenko Mehmedbasic
 * @since 7/8/11, 6:23 PM
 */
public class RockPaperScissors {
   public static void main(String[] args) {
      RuleSet ruleSet = new RuleSet();
      Move rock = new Move("Rock");
      Move paper = new Move("Paper");
      Move scissors = new Move("Scissors");
      Move lizard = new Move("Lizard");
      Move spock = new Move("Spock");

      ruleSet.add(new Rule(scissors, paper, "cuts"));
      ruleSet.add(new Rule(paper, rock, "covers"));
      ruleSet.add(new Rule(rock, lizard, "crushes"));
      ruleSet.add(new Rule(lizard, spock, "posions"));
      ruleSet.add(new Rule(spock, scissors, "smashes"));
      ruleSet.add(new Rule(scissors, lizard, "decapitates"));
      ruleSet.add(new Rule(lizard, paper, "eats"));
      ruleSet.add(new Rule(paper, spock, "disproves"));
      ruleSet.add(new Rule(spock, rock, "vaporizes"));
      ruleSet.add(new Rule(rock, scissors, "crushes"));

      // Get the winner by calling ruleSet.getWinner(move1, move2);

   }
}



And voila. You only have to handle user input and notification, the rules do the rest.

This is a better solution since it provides an easy way to extend the game. You can add/remove rules as you wish. Hope the code helps!

This post has been edited by ripdajacker: 08 July 2011 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1