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; String answer; 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)... "); answer = scan.next(); if (answer.equalsIgnoreCase("y")); { 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) { System.out.println("Sorry you lost all your points and your turn."); 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(""); answer = "n"; } 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(""); answer = "n"; } 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(""); answer = "y"; } } if (answer.equalsIgnoreCase("n")) { 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(""); answer = "y"; } 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(""); answer = "y"; } if (cpuRoundTotal >= endCpuTurn) { cpuRoundTotal = 20; System.out.println("Computers round total is: " + cpuRoundTotal); System.out.println("Computers game total is: " + cpuGameTotal); System.out.println(""); answer = "y"; } 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); } } } } } }