Results 1 to 6 of 6
  1. #1
    meesh is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Question setting counter in craps game....

    I am trying to write a craps game wit no human input that counts the # of wins and losses and displays the probability of winning. I can not figure out why it is not counting right. Any help would be appreciated. Thanks


    public class Craps
    {


    public static void main(String[] args)
    {
    int roll1 = (int)((Math.random()*6)+ 1);
    int roll2 = (int)((Math.random()*6)+ 1);
    int sum = roll1 + roll2;
    int point = sum;
    int win = 0;
    int loss = 0;

    for (int i = 0; i<=99; i++)
    if ((sum == 7) || (sum == 11))
    {
    win++;
    }
    else if ((sum == 2)||(sum == 3)||(sum == 12))
    {
    loss++;
    }
    while ((sum != 7)&&(sum != point))
    {
    roll1 = (int)((Math.random()*6)+ 1);
    roll2 = (int)((Math.random()*6)+ 1);
    sum = roll1 + roll2;
    }


    if (sum == point)
    {
    win++;
    }
    else
    {
    loss++;
    }
    int probability = win/(win + loss);
    System.out.println("The probability of winning is: " + probability);
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: setting counter in craps game....

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    meesh is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: setting counter in craps game....

    Sorry I did not post that correctly

    Java Code:
    public class Craps 
    {
    
    
    public static void main(String[] args) 
    {
    int roll1 = (int)((Math.random()*6)+ 1);
    int roll2 = (int)((Math.random()*6)+ 1);
    int sum = roll1 + roll2;
    int point = sum;
    int win = 0;
    int loss = 0;
    
    for (int i = 0; i<=99; i++) 
    if ((sum == 7) || (sum == 11))
    {
    win++;
    }
    else if ((sum == 2)||(sum == 3)||(sum == 12))
    {
    loss++;
    }
    while ((sum != 7)&&(sum != point))
    {
    roll1 = (int)((Math.random()*6)+ 1);
    roll2 = (int)((Math.random()*6)+ 1);
    sum = roll1 + roll2;
    }
    
    
    if (sum == point)
    {
    win++;
    }
    else 
    {
    loss++;
    }
    int probability = win/(win + loss);
    System.out.println("The probability of winning is: " + probability);
    }
    
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: setting counter in craps game....

    Do you really write code without indentation? Go through Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    meesh is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: setting counter in craps game....

    Java Code:
    
    
    public class Craps 
    {
    
    
    	public static void main(String[] args) 
    	{
    	    int roll1 = (int)((Math.random()*6)+ 1);
    		int roll2 = (int)((Math.random()*6)+ 1);
    		int sum = roll1 + roll2;
    		int point = sum;
    		int win = 0;
    		int loss = 0;
    		
    	for (int i = 0; i<=99; i++)  
    		if ((sum == 7) || (sum == 11))
    		  {
    		    win++;
    		  }
    		else if ((sum == 2)||(sum == 3)||(sum == 12))
    		  {
    			loss++;
    		  }
    	    while ((sum != 7)&&(sum != point))
    	      {
    	    	roll1 = (int)((Math.random()*6)+ 1);
    	 	    roll2 = (int)((Math.random()*6)+ 1);
    	 	    sum = roll1 + roll2;
    	 	  }
    	    
    	    	
    	    if (sum == point)
    	      {
    	    	win++;
    	      }
    	    else 
    	      {
    	    	loss++;
    	      }
    	    int probability = win/(win + loss);
    	    System.out.println("The probability of winning is: " + probability);
    	 }
    
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: setting counter in craps game....

    Thanks, that looks so much better, and is now readable. Now to your problem:
    Quote Originally Posted by meesh View Post
    I can not figure out why it is not counting right.
    You need to tell us what result you expected, and what you got. And you could add some more System.out.println(...) calls to see the values of intermediate variables at various points in the program.

    The java.util.Random class might be a better choice for generating a random int as it has a method that does exactly that, leading to more code clarity.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help setting up collisions for a Java game
    By sur4j in forum New To Java
    Replies: 5
    Last Post: 12-08-2012, 02:12 PM
  2. Replies: 3
    Last Post: 02-17-2011, 02:42 AM
  3. Problem with Ending Craps game
    By Ebayer in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 07:17 PM
  4. How do you go about setting boundaries in a game?
    By robertbob in forum New To Java
    Replies: 6
    Last Post: 05-10-2010, 11:24 PM
  5. Basic counter timer for a game?
    By Laura in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 01:46 AM

Tags for this Thread

Posting Permissions

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