Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Java Programming - Dice Game Help!

1. Java Programming - Dice Game Help!

Hi guys, first time poster here. I'm a beginner at java and I'm having some serious difficulty with this code. The problem is to create a dice game, that has two dice, that you roll, in turns with the computer. Here are the rules for the game:

1.) You must accumulate 100 points to win.
2.) If you roll a 1, you loose all your points for your round and your turn.
3.) If you roll two 1's, you loose ALL your total points and your turn.
4.) You can voluntarily give up your turn.
5.) The computer plays by the same rules, but if it rolls 20 points in a round, it must give up its turn
6.) You must roll at least once in a round, you cannot just give up dice

I'm having the most trouble right now with being able to pass the turns back and fourth between the computer and the player. The program complies but never acts like I want it to. I've spent soooo much time on this. Any help would be greatly appreciated. THANKS!

```
public class pairOfDice
{

private Die dice1, dice2;
private int faceValue1, faceValue2, sum;
private final int MAX = 6;

public pairOfDice()
{
dice1 = new Die();
dice2 = new Die();
faceValue1 = 1;
faceValue2 = 1;
sum = faceValue1 + faceValue2;
}

public int rollDice1()
{
faceValue1 = (int)(Math.random()*MAX) + 1;
return faceValue1;
}

public int rollDice2()
{
faceValue2 = (int)(Math.random()*MAX) + 1;
return faceValue2;
}

public int getDie1()
{
return faceValue1;
}

public int getDie2()
{
return faceValue2;
}

public int getDiceSum()
{
sum = getDie1() + getDie2();
return sum;
}

}```

```
import java.util.Scanner;

public class Player
{

public static void main (String[] args)
{

int humanTurnTotal = 0;
int humanRoundTotal = 0;
int humanGameTotal = 0;
int cpuTurnTotal = 0;
int cpuRoundTotal = 0;
int cpuGameTotal = 0;
int rollAOne = 1;
int win = 100;
int endCpuTurn = 20;

Scanner scan = new Scanner(System.in);

pairOfDice dice = new pairOfDice();

System.out.println("****** WELCOME TO THE DICE GAME! ******");
System.out.println("");
System.out.println("-- The Rules of the Game --");
System.out.println("");
System.out.println("1.) You must accumulate 100 points to win.");
System.out.println("2.) If you roll a 1, you loose all your points for your round and your turn.");
System.out.println("3.) If you roll two 1's, you loose ALL your total points and your turn.");
System.out.println("4.) You can voluntarily give up your turn.");
System.out.println("5.) The computer plays by the same rules, but if it rolls 20 points in a round, it must give up.");
System.out.println("6.) You must roll at least once in a round, you cannot just give up dice.");
System.out.println("");

while (humanGameTotal < win && cpuGameTotal < win)
{
System.out.print("Type 'Y' to roll dice, type 'N' to skip turn (You must roll once)... ");
{
dice.rollDice1();
dice.rollDice2();
System.out.println ("");
System.out.println("Your first die roll: " + dice.getDie1());
System.out.println("Your second die roll: " + dice.getDie2());
System.out.println("");
if (dice.getDie1() == rollAOne && dice.getDie2() == rollAOne)
{
humanTurnTotal = 0;
humanRoundTotal = 0;
humanGameTotal = 0;
System.out.println("Your round total is: " + humanTurnTotal);
System.out.println("Your game total is: " + humanGameTotal);
System.out.println("");
}
if (dice.getDie1() == rollAOne || dice.getDie2() == rollAOne)
{
System.out.println("Sorry you lost all your points for this round and your turn.");
humanTurnTotal = 0;
humanRoundTotal = 0;
System.out.println("Your round total is: " + humanRoundTotal);
System.out.println("Your game total is: " + humanGameTotal);
System.out.println("");
}
else
{
humanTurnTotal = dice.getDiceSum();
humanRoundTotal = humanRoundTotal + humanTurnTotal;
System.out.println("Your turn total is: " + humanTurnTotal);
System.out.println("Your round total is: " + humanRoundTotal);
System.out.println("Your game total is: " + humanGameTotal);
System.out.println("Computers game total is: " + cpuGameTotal);
System.out.println("");
}
}

{
while ((cpuRoundTotal < endCpuTurn) && (humanGameTotal < win && cpuGameTotal < win))
{
System.out.println("");
dice.rollDice1();
dice.rollDice2();
cpuTurnTotal = dice.getDiceSum();
cpuRoundTotal = cpuRoundTotal + cpuTurnTotal;
System.out.println("Computers first die roll: " + dice.getDie1());
System.out.println("Computers second die roll: " + dice.getDie2());
System.out.println("");
if (dice.getDie1() == rollAOne || dice.getDie2() == rollAOne)
{
cpuTurnTotal = 0;
cpuRoundTotal = 0;
System.out.println("Computers round total is: " + cpuRoundTotal);
System.out.println("Computers game total is: " + cpuGameTotal);
System.out.println("");
}
if (dice.getDie1() == rollAOne && dice.getDie2() == rollAOne)
{
cpuTurnTotal = 0;
cpuRoundTotal = 0;
cpuGameTotal = 0;
System.out.println("Computers round total is: " + cpuRoundTotal);
System.out.println("Computers game total is: " + cpuGameTotal);
System.out.println("");
}
if (cpuRoundTotal >= endCpuTurn)
{
cpuRoundTotal = 20;
System.out.println("Computers round total is: " + cpuRoundTotal);
System.out.println("Computers game total is: " + cpuGameTotal);
System.out.println("");
}
if ((dice.getDie1() != rollAOne || dice.getDie2() != rollAOne) && (cpuRoundTotal < endCpuTurn))
{
System.out.println("Computers turn total is: " + cpuTurnTotal);
System.out.println("Computers round total is: " + cpuRoundTotal);
System.out.println("Computers game total is: " + cpuGameTotal);
}
}

}
}
}
}```

2. Re: Java Programming - Dice Game Help!

never acts like I want it to.
Please explain what it does and what you want it to do differently.
Post the console output and add some comments explaining what you want to be different.

3. Re: Java Programming - Dice Game Help!

Originally Posted by Norm
Please explain what it does and what you want it to do differently.
Post the console output and add some comments explaining what you want to be different.
The program is designed to ask the user if it wants to play a dice game and roll 2 dice. The user wants to get to 100 points, but it he/she roll a 1 on one of the die, or two 1's, then they loose there points and there turn. They are asked after every turn if they want to roll again (except when they roll a 1 or two 1's) When they dont want to roll again they to give the dice to the computer, which plays by the same rules except it automatically rolls again, until it reaches 20 points (if it doesnt get a 1 or two 1's) and then gives the dice but to the user.

I cant seem to get the program to correctly pass back and fourth the turns between the computer and the user.