Results 1 to 8 of 8
  1. #1
    javakid is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default New to Java, need help with program

    Hello, I'm new to Java. I have been using Java for a couple of months. I need help with a program that uses dice to play a game of craps. The program needs to run 1000 games and I need to know:

    1. how many games are won on the first, second,..., twentieth rolls and after the twentieth roll?
    2.how many games are lost on the first, second,..., twentieth rolls and after the twentieth roll?
    3. What are the chances of winning at craps?
    4. What is the average length of a game of craps?
    5. Do the chances of winning improve with the length of the game?

    Here's my dice class:
    class dice
    {
    int ar [] [] = new int [6] [6];

    public dice()
    {
    for(int i = 0; i < 6; i++)
    {
    for(int j = 0; j < 6; j++)
    {
    ar [i] [j] = i+j+2;
    }
    }
    }

    public int get_dice(int rand1, int rand2)
    {
    return ar [rand1] [rand2];
    }
    }

    Here's my random number generator:

    import java.util.Random;
    class my_random
    {

    Random X= new Random();
    public my_random(long seed_value)
    {
    X.setSeed(seed_value);
    }

    public int get_num()
    {
    return(X.nextInt(6));
    }
    }

    Heres my my driver for the main program: I know, its flawed:o

    import java.util.Random;
    class crapsdriver
    {
    public static void main(String args [])
    {
    dice classdice_x = new dice();
    my_random rand_x = new my_random(56);

    int dice_roll = 0;
    int random_a = 0;
    int random_b = 0;
    int counter = 0;

    for(int roll=1; roll <1000; roll++)


    System.out.println("Number of games lost is:");
    counter++;

    System.out.println("Number of games won is:");
    counter++;


    random_a = rand_x.get_num();
    random_b = rand_x.get_num();

    dice_roll = classdice_x.get_dice(random_a, random_b);

    if(dice_roll == 7 || dice_roll == 11)
    System.out.println("You Win!");

    if(dice_roll == 2 || dice_roll == 12)
    System.out.println("You Lose");

    while(true)
    {
    System.out.println("sum= " + dice);
    System.out.println(random_a);
    System.out.println(random_b);

    if(dice_roll == dice_roll)
    {
    System.out.println("You Win!");
    break;
    }

    if(dice_roll == 7)
    {
    System.out.println("You Lose");
    break;
    }

    if(dice_roll != 7)
    {
    System.out.println("Roll Again");
    break;
    }
    }

    }
    }

    I will appreciate any and all help. Thank you.

  2. #2
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    you could use a for loop. that way u can control how many times it iterates. the code would look like this

    for(int i=0;i<1000;1++){

    the game code;

    }

    for displaying how many times it won you could use an accumulator value which could be

    int wins=0//accumulator value

    if(win==true)
    win+=1;

    then just display wins on the console.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) what problems are you having now?
    2) you may want to repost your code using code tags so that it will retain its formatting. To do this place the tag [cod&#101;] above your code and the tag [/cod&#101;] below the code.

  4. #4
    javakid is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    I won't have access to a computer that has Java for a couple of days:( I'm just trying to figure out what's wrong. Thanks for the help!!

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Some observations

    • This will only loop for 999 times, not 1000

    Java Code:
    for(int roll=1; roll <1000; roll++)
    Either make roll = 0 or roll< 1001 to get 1000 rolls
    • The following will ALWAYS result true:

    Java Code:
    if(dice_roll == dice_roll)
    {
    System.out.println("You Win!");
    break;
    }
    it should be dice_roll== (something else)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javakid View Post
    I won't have access to a computer that has Java for a couple of days:( I'm just trying to figure out what's wrong. Thanks for the help!!
    Whose computer are you on right now?

    The reason I ask is that it is somewhat futile to ask for help here if you can't use our recommendations to change your code and compile and test it. Without this ability you're just setting yourself up for a headache.

    If you're at a home computer perhaps you can simply load the Java developers kit. It doesn't take up much space and is easy to use.

  7. #7
    javakid is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Whose computer are you on right now?


    I just bought a new computer and it doesn't have Java on it.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Well you obviously have it connected to the internet. As I mentioned before, why not download java? Anyway, I'm outa here. Good luck.

Similar Threads

  1. Run program from Java
    By nenadm in forum Advanced Java
    Replies: 4
    Last Post: 12-07-2008, 10:36 PM
  2. Java program help
    By Crystaliss in forum New To Java
    Replies: 1
    Last Post: 11-20-2008, 03:46 PM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. New to Java Program
    By jvasilj1 in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 07:22 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •