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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Programming - Dice Game Help!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    	  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);
    						}
    				}
     
    			}
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming - Dice Game Help!

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Programming - Dice Game Help!

    What does the console output look like? Can you post that and add comments where the problem is?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Couple compile errors for dice game
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 01:16 PM
  2. Dice game code creating help!
    By kodols in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2011, 07:01 PM
  3. Dice Wagering Game new to Multiple Classes
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 16th, 2011, 08:22 PM
  4. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM
  5. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM

Tags for this Thread