Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default looks good looking for suggestions..

    Question:
    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 by 6 and converting to an integer results in an integer that
    is between 0 and 5.


    Java Code:
    public class CrapsGame
     {
    
    	public static  int gameswon = 0;
    	public static  int gameslost = 0;
    	public static int max = 6;
    	public static int min = 1;
    	
    	public static void main(String[] args)
    	
    	{
    		int diceValue,diceValue2,point;
    	
    	for ( int i = 0 ; i < 10000; i++)
    	{
    			diceValue = diceSimulator(max,min );
    			System.out.println("original dicevalue is " + diceValue);
    			
    		
    	switch (diceValue)
    	{
    	case  7:
    	case 11:
    			gameswon++;
    		break;
    	case  2:
    	case  3:
    	case 12:
    			gameslost++;
    		break;
    	default:
    		point = diceValue;
    		
    		do{
    			System.out.println("point " + point );
    			diceValue2 = diceSimulator(max,min);
    			if (diceValue2 == point)
    				{System.out.println("point is rolled..woo hooo");
    				gameswon++;}
    			else if 
    				(diceValue2 == 7)
    				{System.out.println("7 is rolled..boo");
    				gameslost++;}
    			else
    			{
    				System.out.println("diceValue2 is " + diceValue2);
    			}
    			
    		  }
    		while ((diceValue2 != 7) && (diceValue2 != point) );
    		
    		 break;
    	}		
    	}	
    	System.out.println("games won: " + gameswon);
    	System.out.println("games lost: " + gameslost);
    	System.out.printf("Probability of winning:%.2f " , (double) gameswon/ (gameswon + gameslost));
    }
    		
    	public static int diceSimulator(int nmax,int nmin)
    	{
    		  int randomn1  = (int) ( nmin +  Math.random() * ((nmax - nmin + 1) ));
    		  int randomn2  = (int) (nmin + Math.random() * ((nmax - nmin + 1 ))) ;
    		  int random = randomn1 + randomn2;
    		
    		  return random;
    		
    	}
    	
    
    }

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

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Fubarable View Post
    Suggestion: ask an answerable question.
    or get a questionable answer. ;-)

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    i am terribly sorry but i dont understand

    it is obvious that you guys are thousand times more experienced with java compared to me ..so i just asked for a suggestion..

    i needed someone to tell me if my code is good or bad...
    if good how can i improve it

    if bad how can i improve it.....

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So then say that.

    Return question: Does it work?

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

  7. #7
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    yes it works...(i think soo...)


    i am asking if my method is right....

    is there a method by which i can write better code than this...

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Have you tested it? Does it work for all tested conditions? If not, what doesn't work?...
    It works fine; it ain't broke so there's no need to fix it. I like the correct solution how to simulate the dice: generating a single uniformly distributed pseudo random number in the range [2, 12] is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    i actually thought about doing that...

    can you please tell me whats wrong with that approach?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by search4survival View Post
    i actually thought about doing that...

    can you please tell me whats wrong with that approach?
    You can generate 36 combinations with two dice; e.g. you can generate a 7 by throwing (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1); those are six combinations out of 36 (1/6). Had you used a single random number the probability of throwing 7 would have been 1/11, which is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    He said you are doing the correct way. A single 2-12 number is incorrect as with two dice there is a much larger chance of "6" value than there is of a "2" or "12" and generating a single number would not reflect this.

    Edit: Far too slow.

Similar Threads

  1. Looking for suggestions
    By tonino in forum New To Java
    Replies: 5
    Last Post: 09-26-2010, 08:17 PM
  2. Book suggestions
    By Lil_Aziz1 in forum Java Applets
    Replies: 3
    Last Post: 01-04-2010, 03:38 AM
  3. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 04:32 PM
  4. Desktop Application - Suggestions
    By jedai in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2009, 05:43 AM
  5. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:34 AM

Posting Permissions

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