Help with Rock, Paper, Scissors, Lizard, Sock project

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2135 Views - Last Post: 15 April 2015 - 01:05 PM Rate Topic: -----

#1 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 11:39 AM

Hi guys this is what code i have so far i want to try and get the computer to output a String type result rather than a number and i wonder if there is a way to output the score after the user is done with the game. I couldnt really see if there was a way to incorperate a switch statement but thats what im working on also if you guys could also help to guide me with the best of 3,5,7 option!

heres my code

import java.util.Scanner;
import java.util.Random;

public class RPSLS {

	public static void main(String[] args) {
		
		//for the user to input their options 
		Scanner scan = new Scanner(System.in);
		
		Random random = new Random();
		
		int computerscore = 0;
		int userscore = 0;
		int Rock = 0 , Paper = 1, scissors = 2, Lizard = 3, Spock = 4;
		
		while (userscore <5 && computerscore <5){
			System.out.println("Please enter your desired weapon of choice:Rock, Paper," +
		"Scissors, Lizard or Spock!: ");
				String user = scan.nextLine();
				int computer = random.nextInt(5);
				System.out.println("The computer chose: " + computer);
				if ( (user == "Rock" && computer == 2)//Rock vs Scissors
					|| (user == "Rock" && computer == 3)//Rock vs. Lizard
					|| (user == "Paper" && computer == 0)//paper vs Rock
					|| (user == "Paper" && computer == 4)//Paper vs Spock
					|| (user == "Scissors" && computer == 1 )//Scissors vs Paper
					|| (user == "Scissors" && computer == 3)//Scissors vs Lizard
					|| (user == "Lizard" && computer == 1)//Lizard vs paper
					|| (user == "Lizard" && computer == 4)//Lizard vs Spock
					|| (user == "Spock" && computer == 0)// Spock vs Rock 
					|| (user == "Spock" && computer == 2) )// Spock vs Scissors
				{
					System.out.println("You Win!!!! Congrats!");
					userscore++;
				}
				
				else if ( (user == "Rock" && computer == 1)//Rock vs Paper
						|| (user == "Rock" && computer == 4)//Rock vs. Spock
						|| (user == "Paper" && computer == 2)//paper vs scissors
						|| (user == "Paper" && computer == 3)//Paper vs Lizard
						|| (user == "Scissors" && computer == 0 )//Scissors vs Rock
						|| (user == "Scissors" && computer == 4)//Scissors vs Spock
						|| (user == "Lizard" && computer == 2)//Lizard vs Scissors
						|| (user == "Lizard" && computer == 0)//Lizard vs rock
						|| (user == "Spock" && computer == 1)// Spock vs Paper
						|| (user == "Spock" && computer == 3) )// Spock vs Lizard
				{
					System.out.println("The Computer wins! I'm sorry but you lost :P/>/> ");
					computerscore++;
				}
			System.out.println("Here's your score: " + userscore   +   "  Computers score: " + computerscore);
		}
		
		
		
		
		
		
		
		
		
		
	}

}

:code:

This post has been edited by modi123_1: 10 April 2015 - 11:45 AM
Reason for edit:: Please use the 'code' button in the editor to apply code tags to your code.


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Rock, Paper, Scissors, Lizard, Sock project

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,565
  • Joined: 12-December 12

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 12:09 PM

Off topic: I'm not sure what use a Sock is? Perhaps you could put the Lizard in the sock..
Was This Post Helpful? 0
  • +
  • -

#3 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 12:35 PM

I meant to put spock lol
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4055
  • View blog
  • Posts: 18,098
  • Joined: 24-September 08

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 12:39 PM

Use an ENUM then?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 06:18 PM

Functions are your friends. Use some.

I'd do something like:
class RPSLS {
    private final int Rock = 0 , Paper = 1, scissors = 2, Lizard = 3, Spock = 4, ChoiceCount=5;
    private final Scanner scan;
    private final Random rnd;

    private int userScore, computerScore;

    public RPSLS() { /* your code here */ }
    private int getUserChoice() { /* your code here */ }
    private int getComputerChoice() { /* your code here */ }
    private String getChoiceName(int choice) { /* your code here */ }
    private int choiceCompare(int choice1, int choice2) { /* your code here */ }
    private boolean isDone() {  /* your code here */ }

    private void playRound() {
        int user = getUserChoice();
        int computer = getComputerChoice();
        System.out.println("You choose: " + getChoiceName(user));
        System.out.println("The computer chose: " + getChoiceName(computer));
        int cmp = choiceCompare(user, computer);
        if (cmp<0) {
            System.out.println("The Computer wins! I'm sorry but you lost.");
            computerScore++;
        } else if (cmp>0) {
            System.out.println("You Win!!!! Congrats!");
            userScore++;
        } else {
            System.out.println("Tie!");
        }
        System.out.println("Here's your score: " + userScore   +   "  Computers score: " + computerScore);
    }

    public void play() {
        while(!isDone()) {
            playRound();
        }
    }

    public static void main(String[] args) {
        new RPSLS().play();

    }
}



Note a method called getChoiceName which will offer up a name for the number. However, as noted, this is the ideal place for an enum.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 10 April 2015 - 07:29 PM

Okay i understand your code but the thing is we havent gotten that far plus my professor said only one class is necessary lol
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 11 April 2015 - 02:49 AM

You haven't gotten to methods? Seriously?!?

Ok, if you must do it all in one big messy main...
String computerName = "Oops, I did it wrong.";
if (computer==0) {
    computerName = "Rock";
} else if computer==1) {
...



A cleaner way is to have an array with all the names... but if you haven't done methods, arrays are probably right out.

The thing is, you don't want your user to have to type "Rock". Ideally, you've give them five choices and allow them to enter one through five. This will allow both user and computer to be using the same set of numbers. Which, really, is the only sane way to do it.

Now, if your user is giving you a number, you'll have to figure out what the text is for that at some point. When you find yourself doing the same code twice, it's time for a method.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 11 April 2015 - 12:17 PM

Thank you man and so far yeah we havent gotten to use methods or learning about arrays yet but thank you for your input that makes sense for the computer to output a string, and but is there any way to do a switch statement in this program?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,565
  • Joined: 12-December 12

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 11 April 2015 - 12:38 PM

You could rewrite it to use switch, for example:
switch user:
    case "Lizard":
        // handle Lizard choice
        break;

It requires a substantial rewrite though.
Was This Post Helpful? 0
  • +
  • -

#10 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 11 April 2015 - 05:00 PM

Wait could i do the same thing for the computer?? or just for the user??
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,565
  • Joined: 12-December 12

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 12 April 2015 - 02:13 AM

The switch could be based on the user's choice, and the cases would then check the computer's choice. Or you could do it the other way round.
Was This Post Helpful? 0
  • +
  • -

#12 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 13 April 2015 - 11:36 AM

Okay nevermind my professor did cover using Enumerated types! I'm sorry but what can i do with the enumerated options once i input them?
Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,650
  • Joined: 10-May 07

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 13 April 2015 - 12:17 PM

View Postandrewsw, on 10 April 2015 - 03:09 PM, said:

Off topic: I'm not sure what use a Sock is?

There are rocks in my socks said the Ox to the fox
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 13 April 2015 - 01:52 PM

From your code: if ( (user == "Rock" && computer == 2)

Don't compare Strings using the == operator. It checks their locations in memory, not the actual text. When comparing Objects (and Strings are Objects), use the equals() method instead which compares the actual objects.

if( (user.equals("Rock") && computer == 2)


Was This Post Helpful? 0
  • +
  • -

#15 COMPSCI720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-March 15

Re: Help with Rock, Paper, Scissors, Lizard, Sock project

Posted 13 April 2015 - 08:51 PM

Okay guys here is an update on my code so far, the problem now is the scoring system, there's an error message asking to initialize computerPlay but when i do that it screws up the switch statement how would i fix that and all of the messages are popping up at once as you can see in the picture
import java.util.Scanner;
import java.util.Random;

public class RPSLS {
	
	enum playerChoices {Rock, Paper, Scissors, Lizard, Spock}

	

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		Random random = new Random();
		
		int computerPlay;
		int userScore = 0, computerScore = 0;
		String str , another = "y";
		int Rock =0 , Paper = 1, Scissors = 2, Lizard = 3, Spock = 4;
		
	while (another.equalsIgnoreCase("y")){
			System.out.println("Would you like to play a game?: Y/N ");
			str = scan.nextLine();
		
		
		while (userScore<= 5 && computerScore <= 5){
			System.out.println("Please enter your weapon of choice! Rock, Paper, Scissors, Lizard, or Spock");
			String userPlay = scan.nextLine();
			int computer = random.nextInt(5);
			
			
			switch (computerPlay)
			{
				case 0:
					Rock = 0;
						System.out.println("Computer chose Rock");
					break;
				case 1:
					Paper = 1;
					System.out.println("Paper");
					break;
				case 2:
					Scissors = 2;
					System.out.println("Scissors");
					break;
				case 3:
					Lizard = 3;
					System.out.println("Lizard");
					break;
				case 4:
					System.out.println("Spock");
					break;
				default :
					System.out.println("Nothing was chosen");
			}
			
			System.out.println("The computer chose: " + computerPlay);
			if ( (userPlay.equals("Rock") && computer == 2)//Rock vs Scissors
				|| (userPlay.equals("Rock") && computer == 3)//Rock vs. Lizard
				|| (userPlay.equals("Paper") && computer == 0)//paper vs Rock
				|| (userPlay.equals("Paper")&& computer == 4)//Paper vs Spock
				|| (userPlay.equals("Scissors")&& computer == 1 )//Scissors vs Paper
				|| (userPlay.equals("Scissors")&& computer == 3)//Scissors vs Lizard
				|| (userPlay.equals("Lizard") && computer == 1)//Lizard vs paper
				|| (userPlay.equals("Lizard") && computer == 4)//Lizard vs Spock
				|| (userPlay.equals("Spock") && computer == 0)// Spock vs Rock 
				|| (userPlay.equals("Spock") && computer == 2) ) userScore++; // Spock vs Scissors
			{
				System.out.println("You Win!!!! Congrats!");
			
			}
			
			
			 if ( (userPlay.equals("Rock") && computer == 1)//Rock vs Paper
					|| (userPlay.equals("Rock") && computer == 4)//Rock vs. Spock
					|| (userPlay.equals("Paper") && computer == 2)//paper vs scissors
					|| (userPlay.equals("Paper") && computer == 3)//Paper vs Lizard
					|| (userPlay.equals("Scissors") && computer == 0 )//Scissors vs Rock
					|| (userPlay.equals("Scissors") && computer == 4)//Scissors vs Spock
					|| (userPlay.equals("Lizard") && computer == 2)//Lizard vs Scissors
					|| (userPlay.equals("Lizard") && computer == 0)//Lizard vs rock
					|| (userPlay.equals("Spock") && computer == 1)// Spock vs Paper
					|| (userPlay.equals("Spock") && computer == 3) )computerScore++;// Spock vs Lizard
			{
				System.out.println("The Computer wins! I'm sorry but you lost :P/>/>");
				
			}
		
		
			
		
		
		
		
		}
		
		if (userScore ==5 || computerScore == 5);
		System.out.println("Your score: " + userScore + "\tComputers Score: " +computerScore );
		
	}
	

	}
	

}


This post has been edited by macosxnerd101: 13 April 2015 - 08:56 PM
Reason for edit:: Please learn to use code tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2