## brinze

I have been helped here once before. I got the info I needed to solve my issues. I'm a beginner and this online class I'm taking is killing me. This assignment involves the use of loops, most likely do-while, and possibly others. Essentially the program is to have the user guess what the total of his roll of two dice will be (2 to 12). Then the two dice are rolled. The user has three rolls to achieve a WIN, if not he loses and must get the opportunity to play again. If he matches his guess, the game tells him he's a winner and the game is over then - no three rolls. He also must be given the opportunity to play again.
I've validated the user's guess (input). That part works great. This program uses a class that rolls and displays the dice - that too works ok. From there, I've gotten completely confused and frustrated. Here is my code, most of which does not work. Can anyone help me? I'm such a beginner, the book doesn't always help, and there is no online course assistance. Thanks in advance for any hints or help:

``````//********************************************************************
// DiceGame.java
// This program tells user to predict what the pair of dice will roll.
// Dice will roll a maximum of 3 times, using the Dice class already created.
// If the dice rolls the user's prediction in the 3 rolls, user wins
// If not, user loses. Either way user has option to play again.
//********************************************************************

import java.util.Scanner;

public class DiceGame
{
public static void main (String[] args)
{
// All variables including char variable that holds y and n.
int rolls, roll1, roll2, guessRoll;
int rollSum;
char repeat = ' ';
String input;
Scanner keyboard = new Scanner(System.in);

// Create two separate dice objects to create a pair of dice
Dice die1 = new Dice();
Dice die2 = new Dice();

// get the user to enter the what user predicts his/her pair of dice will roll
System.out.print ("Enter what your pair of dice will roll (a number 2 through twelve please:  ");
guessRoll = keyboard.nextInt();
// Validate user's input - page 261
while (guessRoll < 2 || guessRoll > 12)
{
System.out.println("Incorrect number entered. Please enter a number from 2 to to 12 only.");
System.out.println("Enter correct number here:   ");
guessRoll = keyboard.nextInt();
}
{
do
{
roll1 = die1.diceRoll();
roll2 = die2.diceRoll();

rollSum = roll1 + roll2;
// Set the number of times the loop can repeat itself.
for (rolls = 1; rolls < 3; rolls++)
{
System.out.println("Your Dice rolled:   " + rollSum +".");
System.out.println("NO MATCH");
}
while (repeat == 'Y' || repeat == 'y')
{ System.out.println("Play again?  Enter Y or N: ");
input = keyboard.next();
repeat = input.charAt(0);
}
} while (guessRoll != rollSum);
}
do
{
//This will run if the correct number is guessed
System.out.println("_______________Roll #: " + rolls + " _________________");
roll1 = die1.diceRoll();
roll2 = die2.diceRoll();
rollSum = roll1 + roll2;
System.out.println("Your Dice rolled:   " + rollSum +".");
System.out.println("IT'S a MATCH - YOU WIN!!");
while (repeat == 'Y' || repeat == 'y')
{
System.out.println("Play again?  Enter Y or N:  ");
input = keyboard.next();
repeat = input.charAt(0);
}
} while (guessRoll == rollSum);

}

}``````

## peaceful_soul

here is an example of how you ask the user if he wants to play again

``````      boolean playAgain;       // Record user's response when user is
//   asked whether he wants to play
//   another game.

do {
int scoreThisGame;        // Score for one game.
scoreThisGame = play();   // Play the game and get the score.
sumOfScores += scoreThisGame;
gamesPlayed++;
TextIO.put("Play again? ");
playAgain = TextIO.getlnBoolean();
} while (playAgain);
``````

for more information , check this web http://math.hws.edu/eck/cs124/javanotes3/c5/s3.html

let me know if this helps :) good luck

## brinze

Thanks for the assist.

## peaceful_soul

no problem , anytime :)

## brinze

Thank you so much again. I got the program turned in by its deadline. The "play again" worked great - most of the program did right, except I never did get the loop to do right as far as rolling the dice a max of three times if there was no match. The assignment is turned in AS -IS, so I'm not so desperate anymore, but I still want to know how to do it. I'm at work at the moment, but I'd like to still post my code later - when I am able - to see what I'm doing wrong. I appreciate the help and generosity of your time.

## sagedavis

I didn't really read your code, and I am a beginner with Java as well, but have done similar things in VB and in flash, as well as used similar concepts in javascript.

So, I can give you the theory here but can not give you exact java code.

Step one;
when the game loads you need to set a variable "myRolls" (give it your own name if you want) equal to 1 (or zero if you wish).
After each roll, give it a new number of
myRolls + 1 (or myRolls ++ as would work in most languages).

At each interval, you need to do the following.
Check if player's guess matched, if not then check the value of myRolls. If myRolls == 3... If so, game over, if not, roll again.

If the player has guessed, no need to do anything else except "you win, wanna play again"...
Make sure that if they choose to play again, you set myRolls back to the default number again.
Hope this helps.
Sage

## wonder_laptop -3

sure brinze , anytime :)