0

I have tried to run the guessing game from Head First Java 2nd Edition, and I can't seem to identify what's causing the problem here.

class GuessGame {
    Player p1;
    Player p2;
    Player p3;
    public void startGame() {
        p1= new Player();
        p2= new Player();
        p3= new Player();

        int guessp1=0;
        int guessp2=0;
        int guessp3=0;

        boolean p1isRight=false;
        boolean p2isRight=false;
        boolean p3isRight=false;

        int targetNumber= (int) (Math.random()*10);
        System.out.println("i'm thinking of a number between 0 and 9...");
        while(true){
            System.out.println("nr to guess is "+ targetNumber);

            p1.guess();
            p2.guess();
            p3.guess();

            guessp1=p1.number;
            System.out.println("player one guessed "+guessp1);

            guessp2=p2.number;
            System.out.println("player two guessed "+guessp2);

            guessp3=p3.number;
            System.out.println("player three guessed "+guessp3);

            if (guessp1==targetNumber){
                p1isRight=true;
            }
            if (guessp2==targetNumber){
                p2isRight=true;
            }
            if (guessp3==targetNumber){
                p3isRight=true;
            }
            if(p1isRight || p2isRight || p3isRight) {
                System.out.println("we have a winner!");
                System.out.println("Player one has got it right? "+p1isRight);
                System.out.println("Player two has got it right? "+p2isRight);
                System.out.println("Player three has got it right? "+p3isRight);
                System.out.println("game over bitch");
                break;
            } else { System.out.println("player will have to try again.");
            }
        }
    }
}
class Player{
    int number=0;

    public void guess(){
        number = (int) (Math.random()*10);
        System.out.println("i'm guessing " + number);
    }
}
public class GameLauncher{
    public static void main(String[] args){
        GuessGame = new GuessGame();
        game.startGame();
    }
}

4 Answers 4

2

The problem is you are missing the variable name when instancing the game:

public class GameLauncher{
    public static void main(String[] args){
        GuessGame game= new GuessGame(); //game is the variable
        game.startGame();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You need to give your class instance a variable name. game is suitable since you're already using it to call startGame

GuessGame game = new GuessGame();
          ^

Comments

0

Everything looks good. Except one Typo mistake.

Object is not created for GuessGame , thats the reason you are not able to run this.

Modified:

   GuessGame game= new GuessGame();

    game.startGame();

Current:

   GuessGame = new GuessGame();

    game.startGame();

Comments

0
public class GameLauncher{
    public static void main(String[] args){
        GuessGame = new GuessGame();
        game.startGame();
    }
}

You haven't declared "game". It should be:

GuessGame game = new GuessGame()
game.startGame()

Comments

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.