java simple dice game trouble

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 9291 Views - Last Post: 21 June 2011 - 04:10 AM Rate Topic: -----

#1 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

java simple dice game trouble

Posted 19 June 2011 - 02:40 PM

Hello,

Thank you in advance for taking the time to help me!!

My assignment is to write a dice game in java using the Die.java class.The object of the game is to reach a total of 41 or more by rolling a pair of dice no more than 6 times. If you get 10, 20,30, or 40 as your total you automatically lose, and if you roll doubles you get an extra roll (however if you roll a 5 & 5 you loose).

I am having problems adding my turn totals !!! please help me.
for instance i get
Roll #1 5 2 Your total is : 7
Roll #2 3 1 Your total is : 7 when it should be 11
Roll #3 2 3 Your total is : 7 when it should be 16 and it keeps doing that for all the rolls

how do i get the turn total to add up after every roll?


I am just a beginner in programming so please be patient with me, and i apologize if didnt make myself too clear.

Thank you again!

my code :


public class FortyOne{
	public static void main(String [] args){
		
		
		Die die1, die2;
			
		//Create a pair of dice
		die1 = new Die();	
		die2 = new Die(6);
		
		//Rolls dice
		die1.roll();
		die2.roll();
		int rollCount;
		rollCount = 0;
		rollCount++;
		
		
		
		
		//Initiate game	
		System.out.println("Let's Play 41!!!");
		System.out.print("Roll #"+ rollCount+"  ");
		rollCount++;
		
		// After the roll, prints out the value of die1 & 2.
		System.out.print(die1.getUpValue() + "  ");
		System.out.print(die2.getUpValue() + "  ");
		
		//if you get doubles
		if (die1.getUpValue() == die2.getUpValue())
			System.out.print("YOU GOT DOUBLES!!!   ");
		
	
		
		//Gives turn total
		int turnTotal = die1.getUpValue()+die2.getUpValue();

		//prints out sum of die1 and die2
		System.out.print("Your total is: " + turnTotal+  "  ");
		
		//Adds turnTotal to the next ?????
		int playerTotal =0;
		playerTotal+=turnTotal;
	
		
		//gives the wining number
		int winner= 41;
		
		//Limits the amount of rolls to 6.
		if(rollCount >=7)
		System.out.println("YOU LOSE!!");
		
		//Tells player they lose if their roll amount or total equals 10,20,30,or 40
		if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
		else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
			else 		
			
		do {
			System.out.print("\n" + "Roll #"+ rollCount+"  ");
			rollCount++;			
			die1.roll();
			die2.roll();
			die1.getUpValue();
			die2.getUpValue();
			System.out.print(die1.getUpValue() + "  ");
			System.out.print(die2.getUpValue() + "  ");
			System.out.print("Your total is: " + (turnTotal)+  "  ");
		} while (rollCount<=6);
rollCount++;	
	
	}
	
}
		


Is This A Good Question/Topic? 0
  • +

Replies To: java simple dice game trouble

#2 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: java simple dice game trouble

Posted 19 June 2011 - 02:42 PM

On line 43 of your code, you reset playertotal to zero. Move that line to the top where you have the other instance variables, and it will keep track, :)

Like, put the code on line 43 on line 17 now. Good luck.

This post has been edited by maniacalsounds: 19 June 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 02:55 PM

Thank you very much for your quick responce.

I have grouped the instance variables together where you recommended, but I am still having the same problem.

I also changed line 70 to System.out.print("Your total is: " + (playerTotal)+ " ");

but no change either =/.

Any other ideas?

Again,

Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: java simple dice game trouble

Posted 19 June 2011 - 03:03 PM

First. Why did you put parentheses around player total on line 70? Take 'em off, lol.

Second. Lines 66 and 67 are unnecessary, the result is never used.

Third. You didn't increment turntotal with the new roll. So maybe put these lines in your do loop?:
turnTotal = turntotal + playerTotal;



Can you please post the Die class?
Was This Post Helpful? 0
  • +
  • -

#5 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 03:14 PM

lol please have patience with me! =/ I'm clueless, this is all so new to me and taking a summer course so everything is going at such a fast pace.

I did as you mentioned, but not it just keeps adding the turnTotal to itself throughout the whole thing.



this is the die class

public class Die
{	
	private int numFaces; 
	private int upValue;
	
	
	public Die()
	{	
		numFaces = 6;
		upValue = 1;
	}

	public Die(int faces)
	{	
		numFaces = faces;
		upValue = 1;
	}

	public void roll()
 	{	
		upValue = ((int)(Math.random() * numFaces)) + 1; 
 	}
	 

	
	public int getUpValue() 
	{	
		return upValue;	
	}
}



Was This Post Helpful? 0
  • +
  • -

#6 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: java simple dice game trouble

Posted 19 June 2011 - 03:23 PM

WOW! Really? I started this sumemr too! Haha, small world, :)

Anyway, updated classes:
FortyOne.java:
public class FortyOne{
	public static void main(String [] args){
		
		
		Die die1, die2;
			
		//Create a pair of dice
		die1 = new Die();	
		die2 = new Die(6);
		
		int playerTotal =0;
		
		//Rolls dice
		die1.roll();
		die2.roll();
		int rollCount;
		rollCount = 1;
		
		
		
		
		//Initiate game	
		System.out.println("Let's Play 41!!!");
		System.out.print("Roll #"+ rollCount+"  ");
		rollCount++;
		
		// After the roll, prints out the value of die1 & 2.
		System.out.print(die1.getUpValue() + "  ");
		System.out.print(die2.getUpValue() + "  ");
		
		//if you get doubles
		if (die1.getUpValue() == die2.getUpValue())
			System.out.print("YOU GOT DOUBLES!!!   ");
		
	
		
		//Gives turn total
		int turnTotal = die1.getUpValue()+die2.getUpValue();

		//prints out sum of die1 and die2
		System.out.print("Your total is: " + turnTotal+  "  ");
		
		//Adds turnTotal to the next ?????
		playerTotal= playerTotal + turnTotal;
	
		
		//gives the wining number
		int winner= 41;
		
		//Limits the amount of rolls to 6.
		if(rollCount >=7)
		System.out.println("YOU LOSE!!");
		
		//Tells player they lose if their roll amount or total equals 10,20,30,or 40
		if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
		else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
			else 		
			
		do {
			System.out.print("\n" + "Roll #"+ rollCount+"  ");
			rollCount++;			
			die1.roll();
			die2.roll();
			System.out.print(die1.getUpValue() + "  ");
			System.out.print(die2.getUpValue() + "  ");
			turnTotal = turnTotal + playerTotal;
			System.out.print("Your total is: " + turnTotal +  "  ");
		} while (rollCount<=6);
			rollCount++;	
	}
	
}


Die.java:
public class Die
{	
	private int numFaces; 
	private int upValue = 1;
	
	
	public Die()
	{	
		numFaces = 6;
	}

	public Die(int faces)
	{	
		numFaces = faces;
	}

	public void roll()
 	{	
		upValue = ((int)(Math.random() * numFaces)) + 1; 
 	}
	 

	
	public int getUpValue() 
	{	
		return upValue;	
	}
}



Like I mentioned, it was just that you forgot to add turnTotal and playerTotal together. :) Happy coding.
Was This Post Helpful? 0
  • +
  • -

#7 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 03:55 PM

=/ No luck still.
I keep getting the same results.
Was This Post Helpful? 0
  • +
  • -

#8 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: java simple dice game trouble

Posted 19 June 2011 - 04:00 PM

Weird. It works for me... sorry, I don't know what else to say.
Was This Post Helpful? 0
  • +
  • -

#9 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 04:11 PM

I copied exactly what you sent me, what that does is it keeps adding the original turnTotal to itslef.
for example:

Roll #1 4 3 Your total is: 7
Roll #2 1 4 Your total is: 14 <---should be 7 + 5 =12
Roll #3 1 2 Your total is: 21
Roll #4 4 3 Your total is: 28
Roll #5 6 2 Your total is: 35
Roll #6 2 2 Your total is: 42

but thank you anyway!
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: java simple dice game trouble

Posted 19 June 2011 - 05:12 PM

it should be:

int result = die1.getUpValue() + die2.getUpValue();
turnTotal += result
System.out.println( turnTotal ); 


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java simple dice game trouble

Posted 19 June 2011 - 05:37 PM

Actually in real life you will need 2 dices
In a programing world only one dice class that generates 2 numbers between 1 and 6 is required
class Dice {
   Random ran;

   Dice() {
     ran = new Random();
   }

   int getNext() {
     return ran.nextInt(6) + 1;
   }
}



  Dice dice = new Dice();
  ....
  int dice1Value = dice.getNext();
  int dice2Value = dice.getNext();
  ...


int di
Was This Post Helpful? 0
  • +
  • -

#12 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 06:06 PM

still doesn't give me the right sum for the rolls :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java simple dice game trouble

Posted 19 June 2011 - 06:09 PM

View Postnattienat7, on 19 June 2011 - 09:06 PM, said:

still doesn't give me the right sum for the rolls :dontgetit:

post your updated code. No reason it won't work
Was This Post Helpful? 0
  • +
  • -

#14 nattienat7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 11

Re: java simple dice game trouble

Posted 19 June 2011 - 06:21 PM

View Postpbl, on 19 June 2011 - 06:09 PM, said:

View Postnattienat7, on 19 June 2011 - 09:06 PM, said:

still doesn't give me the right sum for the rolls :dontgetit:

post your updated code. No reason it won't work


I kept the two Dice because I was instructed to do so. Also, I found it
confused me more when i only had one die :dozingoff:


Thank you for help and time

public class FortyOne{
	public static void main(String [] args){
		
		
		Die die1, die2;
			
		//Create a pair of dice
		die1 = new Die();	
		die2 = new Die(6);
		
		//Rolls dice
		die1.roll();
		die2.roll();
		int rollCount;
		rollCount = 0;
		rollCount++;
		
			
		//Initiate game	
		System.out.println("Let's Play 41!!!");
		System.out.print("Roll #"+ rollCount+"  ");
		rollCount++;
		
		// After the roll, prints out the value of die1 & 2.
		System.out.print(die1.getUpValue() + "  ");
		System.out.print(die2.getUpValue() + "  ");
		
		//if you get doubles
		if (die1.getUpValue() == die2.getUpValue())
			System.out.print("YOU GOT DOUBLES!!!   ");
			
		
		//Gives turn total
		int turnTotal = die1.getUpValue()+die2.getUpValue();

		//prints out sum of die1 and die2
		System.out.print("Your total is: " + turnTotal+  "  ");
		
		//Adds turnTotal to the next ?????
		int playerTotal =0;
		playerTotal+=turnTotal;
	
		
		//gives the wining number
		int winner= 41;
		
		//Limits the amount of rolls to 6.
		if(rollCount >=7)
		System.out.println("YOU LOSE!!");
		
		//Tells player they lose if their roll amount or total equals 10,20,30,or 40
		if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
		else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
			else 		
			
		do {
			System.out.print("\n" + "Roll #"+ rollCount+"  ");
			rollCount++;			
			die1.roll();
			die2.roll();
			System.out.print(die1.getUpValue() + "  ");
			System.out.print(die2.getUpValue() + "  ");
			System.out.print("Your total is: " + (playerTotal)+  "  ");
		} while (rollCount<=6);
rollCount++;	
	
	}
	
}
		






public class Die
{	
	
	private int numFaces; 
	private int upValue;
	
	public Die()
	{	
		numFaces = 6;
		upValue = 1;
	}

	public Die(int faces)
	{	
		numFaces = faces;
		upValue = 1;
	}

	public void roll()
 	{	
		upValue = ((int)(Math.random() * numFaces)) + 1; 
 	}

	public int getUpValue() 
	{	
		return upValue;	
	}
}


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java simple dice game trouble

Posted 19 June 2011 - 06:41 PM

you rolled your dices
die1.roll();
die2.roll();
but you never called
int val1 = die1.getUpValue();
int val2 = die2.getUpValue();

to get the value returned by shaking those dice :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2