jeudi 8 octobre 2015

Rock Paper Scissors Lizard Spock in java

Doing a project for class and I tried to do it a different way from what the professor ended up doing. When compiling I get:

RPSLS.java:65: error: incompatible types: Object cannot be converted to int
  switch (userChoice){
     ^
1 error
With the error pointing at the start of the switch statement.
import javax.swing.JOptionPane;
import java.util.Random;
public class RPSLS {
  public static void main(String[] args) {

    //Setting choices and compChoice (for the number the computer chooses)
    int compChoice;
    final int Rock = 0;
    final int Paper = 0;
    final int Scissors = 0;
    final int Lizard = 0;
    final int Spock = 0;

    //Comp Choice
    Random generator = new Random();
    compChoice = generator.nextInt(5) + 1;

    //User Choice
    Object[] possibleValues = {
      "Rock", "Paper", "Scissors", "Lizard", "Spock"
    };

    Object userChoice = JOptionPane.showInputDialog(null,
      "Choose one", "Input",
      JOptionPane.INFORMATION_MESSAGE, null,
      possibleValues, possibleValues[0]); //Set the default option (AFIAK it doesn't matter for this kind of thing)

    System.out.println("The computer chose: " + compChoice);
    System.out.println("The computer chose: " + userChoice);

    //Set the userChoice equal to a number based on what was picked in userChoice question after both have entered choices and the choices are printed
    //Values are to be set as follows:
    //Rock 0
    //Paper 1
    //Scissors 2
    //Lizards 3
    //Spock 4
    if (userChoice == "Rock") {
      userChoice = String.valueOf(Rock);
    } else if (userChoice == "Paper") {
      userChoice = String.valueOf(Paper);
    } else if (userChoice == "Scissors") {
      userChoice = String.valueOf(Scissors);
    } else if (userChoice == "Lizard") {
      userChoice = String.valueOf(Lizard);
    } else if (userChoice == "Spock") {
      userChoice = String.valueOf(Spock);
    }





    //Determine who wins
    switch (userChoice) {
      case 0: //User chooses Rock
        if (compChoice == 0) //Rock
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Computer Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Player Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Player Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Computer Win");
        }
        break;


      case 1: //User chooses paper
        if (compChoice == 0) //Rock
        {
          System.out.println("Player Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Computer Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Computer Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Player Win");
        }
        break;


      case 2: //User chooses scissors
        if (compChoice == 0) //Rock
        {
          System.out.println("Computer Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Player Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Player Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Computer Win");
        }
        break;


      case 3: //User chooses lizard
        if (compChoice == 0) //Rock
        {
          System.out.println("Computer Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Player Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Computer Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Player Win");
        }
        break;


      case 4: //User chooses spock
        if (compChoice == 0) //Rock
        {
          System.out.println("Player Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Computer Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Player Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Computer Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Tie, try again.");
        }
        break;


      default:
        System.out.println("There was an error. Please try again.");
        break;
    }
  }
}

Originally I thought I could set the string to a value based on what the option chose was. It seems I can't though and i'm not really sure how it wants me to put the switch as an int. What I think I might be able to do is, in my initial if statements, instead of putting the string equal to a value, I make an int that gets set to a value based on the choice of the user in the joption section. Any help or advice would be appreciated.

Aucun commentaire:

Enregistrer un commentaire