0

I have a Enum Class, a Player Class, and a class called Lisa that extends Player class. Im trying to randomly generate a value (PAPER, ROCK, or SCISSORS) from the Enum. Error: "The primitive type int of Roshambo does not have a field ROCK." Any advice or pointers would be greatly appreciated. It may be apparent, but this is my fists Java class and Google and Stackoverflow searches have not helped. Here is what I have coded so far:

UPDATE: Thanks for all the assistance. I've updated my whole program below. I was wondering if anyone could suggest the best way/place possible to implement logic to determine the winner/loser of the game? Here is the full code:

MAIN

package gameOfRoshambo;
import java.util.Scanner;
public class RoshamboApp {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    System.out.println("Welcome to Roshambo!");
    System.out.println("Enter your name:");

    //Create a new payer
    Player1 player1 = new Player1();
    String name = sc.nextLine();
    player1.setName(name);

    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {
        System.out.println("Hello " + name + ". " + "Would you like to play against Bart or Lisa? (B/L)");
        String opponent = sc.next();

            if(opponent.equalsIgnoreCase("B")){
                //Create a new Bart opponent
                Bart bart = new Bart();
                System.out.println(player1.getName() + ": " + player1.getChoice());
                System.out.println("Bart: " + bart.getRoshambo());

            }
            else if (opponent.equalsIgnoreCase("L")){
                //Create a new Lisa opponent
                Lisa lisa = new Lisa();
                System.out.println(player1.getName() + ": " + player1.getChoice());
                System.out.println("Lisa: " + lisa.getRoshambo());  

            }

        // Ask user if they want to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }

    //Close Scanner
    System.out.println("Thanks for playing! Goodbye!");
    sc.close();
}

}

ENUM

package gameOfRoshambo;
public enum Roshambo

{ROCK, PAPER, SCISSORS;

public String toString() {
    switch(this) {
      case ROCK: return "Rock";
      case PAPER: return "Paper";
      case SCISSORS: return "Scissors";
      default: throw new IllegalArgumentException();
    }
  }
}

PLAYER

package gameOfRoshambo;
abstract class Player {
String name;
Roshambo roshambo;

abstract int generateRoshambo();

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Roshambo getRoshambo() {
    return roshambo;
}
public void setRoshambo(Roshambo newRoshambo) {
    roshambo = newRoshambo;
}
}

PLAYER1

package gameOfRoshambo;
import java.util.Scanner;

public class Player1 extends Player{

String player1 = "";

public Player1(){
    super();
    }

Scanner scan = new Scanner(System.in);

public Roshambo getChoice(){
    System.out.println("Enter Choice: Paper, Rock, Scissors (r/p/s): ");
    char playerChoice = scan.nextLine().toUpperCase().charAt(0);

    switch (playerChoice){
        case 'R':
            return Roshambo.ROCK;
        case 'P':
            return Roshambo.PAPER;
        case 'S':
            return Roshambo.SCISSORS;
        }
    System.out.println("Invalid input!");
return getChoice();
}

public String getPlayer1() {
    return player1;
}

public void setPlayer1(String player1) {
    this.player1 = player1;
}

@Override
int generateRoshambo() {
    // TODO Auto-generated method stub
    return 0;
}
}

BART

package gameOfRoshambo;
public class Bart extends Player {

public Bart(){
    super();
}

public Roshambo getRoshambo(){
    return Roshambo.ROCK;
    }

@Override
int generateRoshambo() {
    // TODO Auto-generated method stub
    return 0;
}
}

LISA

package gameOfRoshambo;
import java.util.Random;
public class Lisa extends Player {
private Random rand;

public Lisa(){
    super();
    rand = new Random();
}

public Roshambo getRoshambo(){
    int shoot = rand.nextInt(3);
    return Roshambo.values()[shoot];
    }

@Override
int generateRoshambo() {
    return 0;
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
CMXVII
  • 7
  • 2
  • `Roshambo.values()[rand.nextInt(3)]` – Zefick Mar 14 '17 at 04:46
  • 3
    Constructor should be `this.rand = new Random();` – Falla Coulibaly Mar 14 '17 at 04:48
  • 1
    "Im running into problems trying to do this" is not problem description, it just shows you have one. Please [edit] your question and describe how exactly it [*doesn't work*](http://importblogkit.com/2015/07/does-not-work/). Do you see any errors/exception/unexpected output? – Pshemo Mar 14 '17 at 04:59
  • My apologies. The errors I am getting are "The primitive type int of Roshambo does not have a field ROCK/PAPER/SCISSORS (respectively)". – CMXVII Mar 14 '17 at 05:09
  • Your abstract class `Player` has `int Roshambo` field which is inherited by `Lisa`. It is hart to tell why you need this field since it is int but to avoid conflict with enum name change its name to lowercase (that one of Java naming standard anyway). You probably also should make that field private since you already have its getter and setter. – Pshemo Mar 14 '17 at 05:29
  • Thanks @Jerry B Your info was helpful and cleared up my confusion. – CMXVII Mar 14 '17 at 06:34

2 Answers2

1
  • You should store your roshambo field as a Roshambo not an int and update your setter and getter accordingly. This is because in Java Enums cannot be casted to int. See the below stack overflow link for explanation:

Cast Int to enum in Java

  • Field names should start with a lower case
  • Use Roshambo.values()[choice]
  • Get rid of the 1 + in 1 + rand.nextInt(3); because the nextInt() method has the first enum value in position 0. So
Roshambo.values()[0] = ROCK
Roshambo.values()[1] = PAPER
Roshambo.values()[2] = SCISSORS
  • In the Lisa constructor, change to rand = new Random() instead of Random rand = new Random() to avoid assigning to a new local variable which you lose once the constructor finishes

See the code snippets I've attached below for you

Player Class

package gameOfRoshambo;

abstract class Player {
    String name;
    Roshambo roshambo;

    abstract int generateRoshambo();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Roshambo getRoshambo() {
        return roshambo;
    }
    public void setRoshambo(Roshambo newRoshambo) {
        roshambo = newRoshambo;
    }
}

Lisa Class

package gameOfRoshambo;

import java.util.Random;

public class Lisa extends Player {

    private Random rand;

    public Lisa(){
        super();
        rand = new Random();
    }

    public Roshambo getRoshambo(){
        int choice = rand.nextInt(3);
        return Roshambo.values()[choice];
        }

    @Override
    int generateRoshambo() {
        return 0;
    }

}

Also with the new above implementation you don't use the abstract int generateRoshambo() method so consider removing it and its implementation in Lisa...

Community
  • 1
  • 1
0

Your field Roshambo is of type int. I think you want to declare it as something more like this:

Roshambo roshambo;

It is bad practice to capitalize field names. In this case, it confused you because you mixed up the field name with the type. You will have to replace int with Roshambo in several other places in your code.

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45