LPSanz
LPSanz

Reputation: 5

Assistance with Rock, Paper, Scissor game

I am very new to this. I have just started taking programming classes at college (CS 107 and CS 108 intro to java). My homework was to create a Rock, Paper Scissor game between computer and user. I was getting the error message on java that on my compPlay string has not been declared. It has already been fixed thanks to @caps lock. Now, after I run the program it does print the instructions but once I enter the input (0,1 or 2) it does not tell me if it's a tie, if I won or lost. Can anyone spot where I messed up? Thanks!

import java.util.Scanner;

import java.util.Random;

public class Lab3 {

    public static void main(String[] args) 
{
        String userPlay; // 0=Scissor, 1=Rock, 2=Paper
        String compPlay = ""; // 0=Scissor, 1=Rock, 2=Paper
        int compInt; // Random generated number from 0-2

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        System.out.print("Let's play Rock, Paper, Scissor! Please enter 0 for Scissor, 1 for Rock or 2 for Paper.  ");
        System.out.println();

        // Random generated number from 0-2
        compInt = (int)(Math.random())*3;


    // Translate computer random number selected into string

    if (compInt == (0))
{
    compPlay = "Scissor";
}
    else if (compInt == (1))
{
    compPlay = "Rock";
}
    else if (compInt == (2))
{
    compPlay = "Paper";
}

    // Get user input
    System.out.println("Enter your choice ");
    userPlay = scan.next();

    // Print computer random number
    System.out.println("Computer is: " + compInt);

    // Set who wins, loses or ties

    if(userPlay.equals (compInt))
{
    System.out.println("It's a tie!");
}
    else if (userPlay.equals (1))
{
    if (compPlay.equals("0"))
    System.out.println("Computer is Scissor. You are Rock. You win!");
    else if (compPlay.equals("2"))
    System.out.println("Computer is Paper. You are Rock. You lose.");
}

    else if (userPlay.equals(2))
{
    if (compPlay.equals("Scissor"))
    System.out.println("Computer is Scissor. You are Paper. You lose.");
    else if (compPlay.equals("Rock"))
    System.out.println("Computer is Rock. You are Paper. You win!");
}
    else if (userPlay.equals(0))
{
    if (compPlay.equals("Rock"))
    System.out.println("Computer is Rock. You are Scissor. You lose.");
    else if (compPlay.equals("Paper"))
    System.out.println("computer is Paper. You are Scissor. You win!");
}

}// main
}// class

Upvotes: 0

Views: 909

Answers (3)

duffymo
duffymo

Reputation: 308928

You're a student, so you can be forgiven for writing a wall of code in a main method, but here's something to consider when you're farther along in your programming education:

Java's an object oriented language. Start trying to think in terms of behavior encapsulated in objects, like this:

Choices:

package game.rps;

/**
 * Choices for rock paper scissors
 * @author Michael
 * @link  https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:42 PM
 */
public enum Choice {
    ROCK, PAPER, SCISSORS
}

Outcomes:

package game.rps;

/**
 * Outcome for rock paper scissors
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:44 PM
 */
public enum Outcome {
    LOSE, DRAW, WIN
}

Player represents the computer:

package game.rps;

import java.util.Random;

/**
 * Player represents the computer in the game
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:54 PM
 */
public class Player {

    private Random random;

    public Player() {
        this.random = new Random();
    }

    public Player(long seed) {
        this.random = new Random(seed);
    }

    public Choice decide() {
        int index = this.random.nextInt(Choice.values().length);
        return Choice.values()[index];
    }
}

Driver for playing the game:

package game.rps;

import java.util.Scanner;

/**
 * Game loop for playing rock paper scissors
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:43 PM
 */
public class Game {

    public static void main(String[] args) {
        String input;
        Scanner in = new Scanner(System.in);
        Game game = new Game();
        Player player = new Player();
        do {
            System.out.println("ROCK, PAPER, SCISSORS, or QUIT: ");
            input = in.nextLine().trim();
            Choice human = Choice.valueOf(input);
            Choice computer = player.decide();
            System.out.println(String.format("computer: %s", computer));
            System.out.println(String.format("human   : %s", human));
            System.out.println(String.format("outcome : %s", game.play(human, computer)));
        } while (!"QUIT".equalsIgnoreCase(input));
    }

    public  Outcome play(Choice player1, Choice player2) {
        if (player1 == player2) {
            return Outcome.DRAW;
        } else {
            if (player1 == Choice.ROCK) {
                return (player2 == Choice.PAPER ? Outcome.LOSE : Outcome.WIN);
            } else if (player1 == Choice.PAPER) {
                return (player2 == Choice.SCISSORS ? Outcome.LOSE : Outcome.WIN);
            } else {
                return (player2 == Choice.ROCK ? Outcome.LOSE : Outcome.WIN);
            }
        }
    }
}

Upvotes: 0

user3377716
user3377716

Reputation:

You can't use .equals() with a string to compare with an int. What I recommend doing is changing the type of userPlay to an int and make the following changes. First line after main:

int userPlay;

Now change userPlay = scan.next(); to:

userPlay = scan.nextInt();

And when comparing userPlay, change the several if statements to:

if(userPlay == compInt)
if(userPlay == 0)
if(userPlay == 2)
... etc

I think you don't need strings at all here, because you are entering simple numbers, not words like scissors. You can take this code over to Code Review and they can help you improve it! I recommend also practicing more comparisons between data types! Hope this helped.

Upvotes: 1

caps lock
caps lock

Reputation: 511

If you want to compile you need to initialize first ..

String compPlay = "";

Upvotes: 0

Related Questions