Java dice game counter unable to add correctly Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Java dice game counter unable to add correctly without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I wrote a dice game in java that rolls two dice and keeps score. The entire game has a total of four classes but the problem I am facing is in my EyesHaveIt class. It adds each turn to a total score but it is not calculating the math correctly. I’ve tried to find what is causing it but with no success. Can anyone help me find where the problem is?

public class EyesHaveIt {
Scanner kb = new Scanner(System.in);

private int turnScore;
private int computerTotalScore;
private int playerTotalScore;
private int computerTurnNumber;
private int playerTurnNumber;
public int roundPoints;
private String userName;

//Accepts a name value from PlayGame class.
public void init(String name) {
    userName = name;
}

//This method starts the game with a computer turn.
public void playGame() {
    computerTurn();
}

//Computers turn to roll the dice.
public void computerTurn() {
    turnScore = 0;
    System.out.println("Computer's turn: ");
    while (turnScore < 20) {
        rollTheDice();
        computerTurnNumber++;
        setComputerTotalScore(turnScore);
    }
    getGameScore();
    enterToContinue();
}

//Checks users input(enter) to continue player turn.
public void enterToContinue() {
    System.out.print("nPress ENTER to continue ...");
    try {
        System.in.read();
    } catch (Exception e) {
        e.printStackTrace();
    }
    playerTurn();
}

//Players turn to roll the dice.
public void playerTurn() {
    turnScore = 0;
    System.out.println(userName + "'s turn:");
    for (int i = 0; i < 5; i++) {
        rollTheDice();
        playerTurnNumber++;
        getPlayerTotalScore(turnScore);
        System.out.println("Roll again? (y/n) ");
        char continueGame = kb.next().charAt(0);
        continueGame = Character.toUpperCase(continueGame);
        if (continueGame == 'Y') {
            int test = 0;
        } else {
            getGameScore();
        }
    }
    getGameScore();
    computerTurn();
}

//Creates two dice from PairOfDice class and prints values rolled.
public void rollTheDice() {
    PairOfDice dieOne = new PairOfDice();
    PairOfDice dieTwo = new PairOfDice();
    int die1 = dieOne.getDieOneValue();
    int die2 = dieTwo.getDieOneValue();
    System.out.println("tRolled: " + die1 + " and " + die2);
    whatWasRolled(die1, die2);

}

//Accepts int from rollTheDie and checks the value.
public void whatWasRolled(int die1, int die2) {
    if (die1 == 1 && die2 == 1) {
        System.out.println("t   Rolled snake eyes! All turn points will be doubled.");
        roundPoints = (die1 + die2 * 2);
        setScore(roundPoints);
    } else if (die1 == 6 && die2 == 6) {
        System.out.println("t   Rolled box cars! All points are gone now!");
        roundPoints = 0;
        setScore(roundPoints);
    } else if (die1 == die2) {
        System.out.println("t   Rolled double. . . lose all turn points.");
        roundPoints = 0;
        setScore(roundPoints);
    } else {
        roundPoints = die1 + die2;
        setScore(roundPoints);
        getTurnScore();
    }
}

//Sets turnScore from whatWasRolled.
public void setScore(int roundPoints) {
    turnScore = turnScore + roundPoints;
}

//Sets computer game score.
public void setComputerTotalScore(int turnScore) {
    computerTotalScore = turnScore + computerTotalScore;

}

//Sets player game score.
public void setPlayerTotalScore(int turnScore) {
    playerTotalScore = turnScore + playerTotalScore;

}

//computerTotalScore accesor returns an int.
public int getComputerTotalScore() {
    return computerTotalScore;
}

//playerTotalScore accesor returns an int.
public int getPlayerTotalScore(int turnScore) {
    playerTotalScore = turnScore + playerTotalScore;
    return playerTotalScore;
}


//Returns turnScore after roll.
public int getTurnScore() {
    System.out.println("tCurrent score for this turn:" + turnScore);
    return turnScore;
}

//How the game ends and current game score is displayed.
public void getGameScore() {
    System.out.println(
            "CURRENT GAME SCORE: Computer: " + computerTotalScore + "t" + userName + ": " + playerTotalScore);

    if (computerTotalScore >= 150) {
        System.out.println("Sorry, " + userName + " you got beat by the computer!");
        System.exit(0);
    } else if (playerTotalScore > 150) {
        System.out.println(userName + ", Congratulations! You beat the computer!");
        System.exit(0);
    }
}

}

Answer

how are you?

I have made 3 alterations in 2 classes. have a look to see if it will work for you.

//Computers turn to roll the dice.
    public void computerTurn() {

        computerTurnNumber = 0; // here

        turnScore = 0;
        System.out.println("Computer's turn: ");
        while (computerTurnNumber < 20) { // here is the amount of time the dice will be rolled
            rollTheDice();
            computerTurnNumber++;
            setComputerTotalScore(turnScore);
        }
        getGameScore();
        enterToContinue();
    }

and

//Sets turnScore from whatWasRolled.
    public void setScore(int roundPoints) {
        turnScore = roundPoints; //here, before it was accumulating the points of each turn
    }
We are here to answer your question about Java dice game counter unable to add correctly - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji