programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# next step in craps program

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
I have a project. the project is:
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 counters 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?

Note: To generate a random number X, where 0 <= X < 1, use X = Math.random();. For example, multiplying Math.random() by 6 and converting to an integer results in a random integer that is between 0 and 5.

And, I already have some, I just havd no idea how to do the next step...
there is my code:

import java.util.Random;

public class Project2
{

public static void main(String[] args)
{
Random rand = new Random();

int grt=10000;//game run times
int dice3=0;
for(int i=0; i<100; i++)
{
int dice1 = (int)((Math.random()*6)+ 1);
int dice2 = rand.nextInt(6)+1;
dice3 = (dice1 + dice2);
}

if(dice3 == 2 || dice3 == 3 || dice3 == 12)
{
System.out.println("you lose");
}
else if(dice3 == 7 || dice3 == 11)
{
System.out.println("you win");
}
else
{

}
}

}

lowercase baba
Posts: 13013
66
• Number of slices to send:
Optional 'thank-you' note:
Please learn to format your code properly. your java needs to go between the code tags, not after them.

My guess is that you read the assignment, and started writing code. now, you're not sure what the next step is.

That is the wrong way to code.

The first steps are to think through everything you need your code to do, a literally write it down in English (or any other Natural language of your choice). Once that is done, you revise it to be more detailed and use smaller and smaller words. Eventually you should have it written out in such a way that a ten year old child can understand.

THEN you start coding.

kamil bidan
Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
thanks for your guys to tring to help me, exactly I have no idea to how to let the dice reroll again, and and get a new number see it equal to the point or 7. And I'm so sorry about my format code and titleă€‚

fred rosenberger
lowercase baba
Posts: 13013
66
• Number of slices to send:
Optional 'thank-you' note:

I have no idea to how to let the dice reroll again

If this is what you are trying to do next, then you have ignored my advice.

You need to understand what the logic is WITHOUT thinking about "how to do it in java". I'd start with something like this:

This is my first draft. I would then go back and revise it 2-3 times, fleshing out more details each and ever time. Only once I had done that would i write my first line of Java. and then, I'd write nothing more than enough code to print "i'm in the main method". That should be about 7-8 lines of code, most of which are nothing but curly braces (My personal coding style puts curly braces on lines by themselves). it is probably three lines of executable statements.

ONLY when I was sure that compiled and ran, would I write the next part - which would probably be a call to a startGame method - and that method would do nothing but print "starting game". I'd compile, test, debug. Once that worked, I might write a method that rolls the dice - again, writing 2-3 lines at a time before I compile/test/debug.

Not that at many places, I don't have code that actually DOES what I want the method to do. It only does enough for me to test the next piece. writing nothing more than the method that returns the 2-12 value would probably be at least a dozen compile/test/debug iterations.

This is really general advice for any program. you have to fully understand the problem before you write any code, and you should compile/test/debug about 10 times more often that you think you should.

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

kamil bidan wrote:there is my code:...

I can't really add much to fred's excellent advice, except to wonder about:Why are you using two different methods to get your two dice results; particularly when you've been told how to do it?

Personallly, I like the second one better; but it's not usually a good idea to go against your tutor unless you've asked him/her if it's alright.

Winston

 PS: <nitpick>
(int)(Math.random()*6.0) + 1
is better than what you wrote. I'll leave you to work out why.
</nitpick>