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 5 of 5

Thread: dice game problem

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

    Default dice game problem

    Hello guys! I am a beginner in Java with a little experience in programming and from the first time i loved java so i am trying to improve my shelf reading some books firstly and maybe later make some bigger projects with help from tutorials! But now i am just reading the book Absolute Java and i am stacked at a example so i would like some advice...by the way i am using only cmd for compile since i think that i should move to netbean a bit more later!!!
    The program that i made it is about the Game of craps that it dice game...

    Specificly "
    In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are
    rolled; the first roll of the dice in a craps round is called the “come out roll.”
    A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12
    automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number
    becomes “the point.” The player keeps rolling the dice until either 7 or the point is
    rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first,
    then the player loses.
    Write a program that simulates a game of craps using these rules without human
    input. Instead of asking for a wager, the program should calculate whether the
    player would win or lose. The program should simulate rolling the two dice and
    calculate the sum. Add a loop so that the program plays 10,000 games. Add
    c ounters that count how many times the player wins, and how many times the
    player loses. At the end of the 10,000 games, compute the probability of winning
    [i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who
    is going to win the most games, you or the house?"

    I am half done it expect the part of calculating the wins/looses and the loop of 10 000 game that it is unnecessary!
    I am trying to "play" with the dices! So there is my code

    import java.util.Scanner;
    import java.util.Random;
     
    public class GameCraps {
     
    public static void main(String[] args) {
    	Random generator=new Random();
     
    	Scanner keyboard=new Scanner(System.in);
    	System.out.println("**Game of Craps**");
    	int r=keyboard.nextInt();
    	do{
    	int dice1=generator.nextInt(6);
    	int dice2=generator.nextInt(6);
    		} while(r==1);
     
    	int sumdices=dice1 + dice2;
     
    		if(sumdices==7 || sumdices==11) {
    			System.out.println(sumdices);
    			System.out.println("We have a winner's come out roll!");
    			System.out.println("Player wins!");
    			System.exit(0);
     
    				}
     
     
    		if(sumdices==2 || sumdices==3 || sumdices==12 ) {
    			System.out.println(sumdices);	
    			System.out.println("We have a looser's come out roll!");
    			System.out.println("Player looses!");
    			System.exit(0);
     
    				}
     
    		if(sumdices==4 || sumdices==5 || sumdices==6 || sumdices==8 || sumdices==9 || sumdices==10) {
    			System.out.printf("%d",sumdices);	
    			System.out.println("A point is setted!");
    			int point=sumdices;
    					}
    			System.out.println("reroll please");
     
    			 r=keyboard.nextInt();	
    			do {
    			int dice1=generator.nextInt(6);
    			int dice2=generator.nextInt(6);
    			int newsumedices=dice1+dice2;
     
    				} while(r==1);
     
    				System.out.println(newsumedices);
     
    				if(newsumedices==point) {
    					System.out.println("You catched the point!You win");
    					System.exit(0);
    						}
    				else if (newsumedices==7) {	
    					System.out.println("Bad roll m8!!!You loose");
    					System.exit(0);
     
    					}
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    	}
     
    }


    could you advice me?

    When i am compiling via javac on cmd throws 6 erros of the type :

    GameCraps.java:17: error: cannot find symbol
    int sumdices=dice1 + dice2;
    ^
    symbol: variable dice1
    location: class GameCraps
    GameCraps.java:17: error: cannot find symbol
    int sumdices=dice1 + dice2;
    ^
    symbol: variable dice2
    location: class GameCraps
    GameCraps.java:51: error: cannot find symbol
    System.out.println(newsumedices);
    ^
    symbol: variable newsumedices
    location: class GameCraps
    GameCraps.java:53: error: cannot find symbol
    if(newsumedices==point) {
    ^
    symbol: variable newsumedices
    location: class GameCraps
    GameCraps.java:53: error: cannot find symbol
    if(newsumedices==point) {
    ^
    symbol: variable point
    location: class GameCraps
    GameCraps.java:57: error: cannot find symbol
    else if (newsumedices==7) {
    ^
    symbol: variable newsumedices
    location: class GameCraps
    6 errors


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

    Default Re: dice game problem

    Several of the cannot find symbol errors are because the named variable's definition is not in scope where you are trying to use it. Move the variable's definition so that it is within the same pair of {}s with where you are trying to reference it.
    You can define a variable in one place and assign it values in another.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Nick87 (March 23rd, 2013)

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

    Default Re: dice game problem

    wow amazing the parameter =java to the code tag!!! surely helps alot the readers! Graaats!!!
    But what you mean about the variables definition?
    Should i define the variables that i am using inside the "ifs" or the loops?
    Or you mean define my variables in the start of my class and then giving values?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dice game problem

    Define the variables at the levels within the {}s where they are going to be used.
    If different methods need to access them, define them at the class level.
    If they are only used in a method, define them in the method outside of any loops of if statements.
    If they are only used locally with the current pair of {}s define them there.
    {
     int x = 2; //  define x inside these {}s
    //  x is known here
    }
    // x is NOT known outside the {}s

    Read up on the scope of variables.

    Should i define the variables that i am using inside the "ifs" or the loops?
    Only if you are not going to use them outside of the enclosing {}s
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Nick87 (March 23rd, 2013)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: dice game problem

    oh thank you so much! how stupid of me to make my variables only variables of a loop! so basic of java! thank you so much anyway!
    My new code but still i have a error message

    GameCraps.java:57: error: variable point might not have been initialized
    if(newsumedices==point) {
    ^
    1 error

    but point and newsumedices are definied!

    EDID:I fixed by moving point=sumdices; out of } blocks of If...so now my program compiles but act so crazy! firstly when i am pressing 1 for the rolling it does nothin! secondly when i am pressing any other number throws me some crazy numbers


    import java.util.Scanner;
    import java.util.Random;
     
    public class GameCraps {
     
    public static void main(String[] args) {
    	Random generator=new Random();
     
    	Scanner keyboard=new Scanner(System.in);
    	System.out.println("**Game of Craps**");
            System.out.println("**Press number 1 to roll");
    	int r=keyboard.nextInt();
    	int dice1;
    	int dice2;
    	int point;
            int newsumedices;
     
    	do{
    	 dice1=generator.nextInt(6);
    	 dice2=generator.nextInt(6);
    		} while(r==1);
     
    	int sumdices=dice1 + dice2;
     
    		if(sumdices==7 || sumdices==11) {
    			System.out.println(sumdices);
    			System.out.println("We have a winner's come out roll!");
    			System.out.println("Player wins!");
    			System.exit(0);
     
    				}
     
     
    		if(sumdices==2 || sumdices==3 || sumdices==12 ) {
    			System.out.println(sumdices);	
    			System.out.println("We have a looser's come out roll!");
    			System.out.println("Player looses!");
    			System.exit(0);
     
    				}
     
    		if(sumdices==4 || sumdices==5 || sumdices==6 || sumdices==8 || sumdices==9 ) {
    			System.out.printf("%d",sumdices);	
    			System.out.println("A point is setted!");
     
    					}
                            point=sumdices;
    			System.out.println("reroll please");
     
    			 r=keyboard.nextInt();	
    				do {
    				 	dice1=generator.nextInt(6);
    			 		dice2=generator.nextInt(6);
    			 		newsumedices=dice1+dice2;
     
    					} 	while(r==1);
     
    				System.out.println(newsumedices);
     
    				if(newsumedices== point) {
    					System.out.println("You catched the point!You win");
    					System.exit(0);
    						}
    				else if (newsumedices==7) {	
    					System.out.println("Bad roll m8!!!You loose");
    					System.exit(0);
     
    					}
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    	}
     
    }

Similar Threads

  1. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  2. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 07:42 PM
  3. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  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