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: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

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

    Default Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

    Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 ( called craps), you lose; if the sum is 7 or 11 ( called natural), you win; if the sum is another value ( i. e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player.


    import java.util.Random;
    public class Dices {
    public static void main(String[] args) {


    int point;
    int dice1;
    int dice2;


    dice1=(int)(Math.random()*6+1);
    dice2=(int)(Math.random()*6+1);
    int sum = dice1 + dice2;


    System.out.println("You rolled " +dice1+ " + " +dice2+ " = " +sum);


    if(sum==2 || sum==3 || sum==12)
    System.out.println("You lose");
    else if(sum==7 || sum==11)
    System.out.println("You win");
    else if(sum==4 ||sum==5 ||sum==6 ||sum==8 ||sum==9 ||sum==10)
    System.out.println("Point is " +sum);
    else if()
    System.out.println("You win");
    else
    System.out.println("You lose");

    }
    }


    I am stuck at if the sum is another value ( i. e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player. Can someone give me a hint of how to fix it? I just can't get the logic of how to set it up.


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

    First thing people are going to tell you is to wrap your code in tags

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

    First of all, I would define and implement a method named rollTwoDice(), or some such thing, that returns the sum of two random die tosses. I would do this because the program will involve rolling the dice in more than one place, and I don't like to write duplicate code in multiple places in the program.

    Anyhow...

    Here's pseudo-code for the main game flow, as I see it:

    Declare an int named value.
     
    Set value equal to the result from a call to rollTwoDice().
     
    IF value is equal to 2 or value is equal to 3 or value is equal to 12 THEN
        You lose.
        Game Over.
    ELSE IF value is equal to 7 or value is equal to 11 THEN
        You Win.
        Game Over.
    ELSE
        Declare an int named point.
        Set point equal to value.
        Repeat the following loop as long as Game is not Over:
        BEGIN LOOP
            Set value equal to the result from a call to rollTwoDice();
            IF value is equal to point THEN
                You Win.
                Game Over.
            ELSE IF value is equal to 7 THEN
                You Lose.
                Game Over.
            END IF
        END LOOP
    END IF

    If that flow makes sense to you and if you think it satisfies the rules of the game as spelled out in your assignment, then try implementing it in Java. Any questions? Post again.

    Now, if you want to practice using real dice so that you can compare with your program, come by my place, and I'll give you some lessons. We will be using my dice.

    Bring cash. Checks and credit cards not accepted.



    Cheers!

    Z

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

    Default Re: Craps is a popular dice game played in casinos. Write a program to play a variation of the game.

    Quote Originally Posted by Zaphod_b View Post
    First of all, I would define and implement a method named rollTwoDice(), or some such thing, that returns the sum of two random die tosses. I would do this because the program will involve rolling the dice in more than one place, and I don't like to write duplicate code in multiple places in the program.

    Anyhow...

    Here's pseudo-code for the main game flow, as I see it:

    Declare an int named value.
     
    Set value equal to the result from a call to rollTwoDice().
     
    IF value is equal to 2 or value is equal to 3 or value is equal to 12 THEN
        You lose.
        Game Over.
    ELSE IF value is equal to 7 or value is equal to 11 THEN
        You Win.
        Game Over.
    ELSE
        Declare an int named point.
        Set point equal to value.
        Repeat the following loop as long as Game is not Over:
        BEGIN LOOP
            Set value equal to the result from a call to rollTwoDice();
            IF value is equal to point THEN
                You Win.
                Game Over.
            ELSE IF value is equal to 7 THEN
                You Lose.
                Game Over.
            END IF
        END LOOP
    END IF

    If that flow makes sense to you and if you think it satisfies the rules of the game as spelled out in your assignment, then try implementing it in Java. Any questions? Post again.

    Now, if you want to practice using real dice so that you can compare with your program, come by my place, and I'll give you some lessons. We will be using my dice.

    Bring cash. Checks and credit cards not accepted.



    Cheers!

    Z
    ahahhaaha Thanks dude but no thank for your dice lessons =).

Similar Threads

  1. Dice Game Program toString() method help
    By JonSnow in forum Java Theory & Questions
    Replies: 4
    Last Post: January 17th, 2013, 03:59 PM
  2. [Homework]Game of craps help
    By mullinz in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2011, 10:48 PM
  3. i like to play this game with my phone and i wanted to make a program to help me
    By Imreallyawesome in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 12th, 2011, 07:34 PM
  4. Making a Craps game.
    By SOK in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2010, 08:23 PM
  5. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM