1

I have the following code at the end of my main class to initialize objects and start the program:

HumanPlayer humanPlayer = new HumanPlayer(baseHold);
Controller controller = new Controller(new ComputerPlayer(), humanPlayer, new Dice(seed));
controller.start();

Inside my Controller class, is the following code:

public class Controller
{
    int roller;
    public Controller(ComputerPlayer cpuPlayer, HumanPlayer userPlayer, Dice dice)
    {
    }
//....
}

I am unsure what to initialize above, as I've tried a few things already and it still does not find my object dice in the following code:

public void start()
{
    for (int count = 0; count < 5; count++)
    {
        roller = dice.roll();
        System.out.println("Die roll: " + roller);
    }
}

roll is a method within the Dice class. Is there a particular way for me to tell it to look for dice as an object within my controller object instead of a variable within my controller object, or am I going about this completely wrong?

I want to be able to roll the die 5 times here.

error:

Controller.java:39: error: cannot find symbol
                roller = dice.roll();
                         ^
symbol:   variable dice
location: class Controller    
1 error
3
  • 1
    Add stacktrace please (The actual error logs) Commented Feb 19, 2015 at 22:17
  • Just a heads up (not sure if this is the case with your actual code) you have a comma missing between humanPlayer and new Dice(seed) when you create an instance of Controller. Commented Feb 19, 2015 at 22:22
  • Fixed both, and edits. Commented Feb 19, 2015 at 22:28

2 Answers 2

1

You have to declare dice as an instance field of Controller, like this:

public class Controller {
    private Dice dice;
    // rest of the class code
}

Then in the constructor you will do:

public Controller(ComputerPlayer cpuPlayer, HumanPlayer userPlayer, Dice dice) {
    // some other code
    this.dice = dice;
}

Then you can use this.dice in the other methods inside the Controller class.

Sign up to request clarification or add additional context in comments.

1 Comment

This was my problem. This has fixed it, thank you. Someone with reputation please upvote.
0
HumanPlayer humanPlayer = new HumanPlayer(baseHold);
Controller controller = new Controller(new ComputerPlayer(), humanPlayer new Dice(seed));
controller.start();

You need a comma between humanPlayer and new Dice(seed)

HumanPlayer humanPlayer = new HumanPlayer(baseHold);
Controller controller = new Controller(new ComputerPlayer(), humanPlayer, new Dice(seed));
controller.start();

1 Comment

Fixed the original code to reflect this. Same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.