Craps wins and losses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsduck
    New Member
    • Mar 2012
    • 10

    #1

    Craps wins and losses

    Hi, I'm writing a program that simulates 100 rounds of craps. I have the code written mostly. My one problem is that I can't seem to get the total numbers of wins and losses.
    Code:
    import java.util.Random;
    
    public class Test {
    
    	/**
    	 * This Program show the results of simulated games of craps using if else statements
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Random rand=new Random();
    		int xWon=0;
    		int xLost=0;
    		for(int i=1;i<=100;i++){
    			craps(rand);
    			if(true){
    				xWon++;
    			}else{
    				xLost++;
    			}
    		}System.out.println("Games Won: "+xWon);
    	     System.out.println("Games Lost: "+xLost);
    	}	
    	public static boolean craps(Random rand){
    		int dieI=rand.nextInt(6)+1;
    		int dieII=rand.nextInt(6+1);
    		int sumDies=dieI+dieII;
    			System.out.print("["+dieI+","+dieII+"]");
    		
    			if (sumDies==7||sumDies==11){
    				System.out.println(sumDies+" You Win!!!");
    				return true;
    			} else if(sumDies==2||sumDies==3||sumDies==12){
    				System.out.println(sumDies+" You Lose...");
    				return false;
    		}
    		int point=sumDies;
    				System.out.print("Point= "+point+" ");
    		
    		do {
    			dieI=rand.nextInt(6)+1;
    			dieII=rand.nextInt(6+1);
    			sumDies=dieI+dieII;
    				System.out.print("["+dieI+","+dieII+"]");
    		} while(sumDies!=7&&sumDies!=point);
    		
    		if (sumDies==point){
    			System.out.println(sumDies+" You Win!!!");
    			return true;
    		} else{
    			System.out.println(sumDies+" You Lose...");
    			return false;
    		}
    		
    			
    	}
    
    }
    If someone could help me out that would be grand. I've been at this one part for 8 hours straight...
    Last edited by Meetee; Mar 6 '12, 04:24 AM. Reason: use code tags [CODE][/CODE]
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    When you're checking whether or not it was a win, you have to actually check the return value.
    Code:
    if(craps(rand))

    Comment

    • rsduck
      New Member
      • Mar 2012
      • 10

      #3
      Thank-you so much! I literally tried everything except that.

      Comment

      Working...