Help with simple java code

Topic by JollyMisanthrope

JollyMisanthrope

Home Forums Computers, Games and Technology Help with simple java code

This topic contains 8 replies, has 4 voices, and was last updated by Moderator  Moderator 4 years ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #181387
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    I’m trying to create a basic craps simulator that outputs a total wins and losses based on x number of iterations, as well as outputting the win percentage.

    This is what I have so far.

    public class craps {

    public static void main(String[] args)
    {
    int dieOne = (int)(Math.random()* 6) + 1;
    int dieTwo = (int)(Math.random()* 6) + 1;
    int roll = dieOne + dieTwo;
    int winCount = 0;
    int lossCount = 0;

    while (winCount + lossCount < 10)

    if (roll == 2 || roll == 3 || roll == 12){
    lossCount++;
    }
    else if (roll == 7 || roll == 11){
    winCount++;
    }
    else {
    dieOne = (int)(Math.random()* 6) + 1;
    dieTwo = (int)(Math.random()* 6) + 1;

    int roll2 = dieOne + dieTwo;

    while (roll2 != 7){

    if(roll == roll2){
    winCount++;
    break;
    }
    else {

    dieOne = (int)(Math.random()* 6) + 1;
    dieTwo = (int)(Math.random()* 6) + 1;
    roll2 = dieOne + dieTwo;

    }
    if (roll2 == 7){
    lossCount++;

    }

    if (winCount + lossCount == 10){
    System.out.println(“Wins: ” + winCount);
    System.out.println(“Losses: ” + lossCount);
    System.out.println(“Win Percentage: ” + (winCount/100);
    }
    }
    }
    }}

    I use eclipse and it will run successfully and show Wins: , Losses:, yet other times it doesn’t return anything.

    If anyone sees anything that might need to be changed I’d love to know about it. I’ve tried to follow the chain of logic as best as I can.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #181390
    Keymaster
    Keymaster
    Keymaster

    My biggest problem with helping you here is … I just don’t know craps! Never played the game.

    But assuming your math is all correct, check your IF statement.

    You have IF…. followed by ELSE IF….. then ELSE…. and then ELSE again.

    You need IF…. followed by ELSE IF….. then ELSE IF…. and then ELSE to finish.

    Since I don’t know the game, it’s unclear if you need that extra IF statement

    if (roll2 == 7){
    lossCount++;
    }
    

    So the problem I spot is with your large IF conditional statement.
    But maybe it’s right because of your while loop.

    A while loop doesn’t use else. It’s a conditional loop all by itself, but then again I never used an else after while. Watch your brackets , because you’re not closing the first IF statements before your while loop.

    It looks like your brackets are the problem here. Make sure they are correct. If they are, then let me know and let’s look at it again. According to your brackets, your first else is the problem. It should be else if.

    if (condition) {
    
    // do something
    
    } else if (condition) {
    
    // do something else
    
    } else if (condition) {
    
    // do something else 
    
    } else {
    
    // do this
    
    }

    yet other times it doesn’t return anything.

    That’s probably why. According to the way you wrote it, it will not work all the time.
    I’m surprised it doesn’t kick out a syntax error.

    If you keep doing what you've always done... you're gonna keep getting what you always got.
    #181412
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Basically it’s this:

    The first roll in craps is the “come out roll”

    Roll two dice. If the sum of those two dice is 2, 3 or 12 you lose.
    If the sum is 7 or 11 you win.

    If either of the above situations occur you start with a new come out roll.

    If it’s any other number (4,5,6,8,9,10,) you establish a “point”.
    After this you roll the dice until you either roll the point number again (in which case you win) or you roll 7 and crap out (you lose).

    Then you go back to a new come out roll after one of the above happens.

    That’s the basics and all that applies to this project.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #181421
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    The establishment of the point via the else if statement here is what creates the need to roll until either one of those conditions is met. Same number rolled again or 7. So that loops is processing it until it’s satisfied.

    So I have the loss, win, or point conditions. The first two are easy.

    else if (roll == 4 || roll == 5 || roll == 6 || roll == 8 || roll == 9 || roll == 10){
        dieOne = (int)(Math.random()* 6) + 1; 
            dieTwo = (int)(Math.random()* 6) + 1;
            
      }
    
    So if that condition is met it should put me into the loop. The loop runs until condition is met then back to the top to start over, incrementing the win or loss count each time. 
            
            int roll2 = dieOne + dieTwo;
            while (roll2 != 7){
            
            if(roll == roll2){  //This roll made the point
            winCount++;
            break;
            }
            else {
               dieOne = (int)(Math.random()* 6) + 1; //This is the new roll if the point is not made
               dieTwo = (int)(Math.random()* 6) + 1; //Keeps going until the point or 7 is rolled
               roll2 = dieOne + dieTwo;
            }
            if (roll2 == 7){ //This roll crapped out for a loss
               lossCount++;
                }        
                if (winCount + lossCount == 10000){
               System.out.println("Wins: " + winCount);
               System.out.println("Losses: " + lossCount);
               System.out.println("Win Percentage: " + (winCount/winPercent) + "%");
    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #181424
    Keymaster
    Keymaster
    Keymaster

    OK. But It still looks like your brackets are the problem. You don’t have an opening bracket after your first while either. That’s a problem right there. Your brackets make a huge difference. One out of place can create an entirely different statement. Forget the math and just care that your statements and conditions are all correct first. Note the “//OPENING BRACKET” which I added as a correction to your code at the top……

    while (winCount + lossCount < 10)  {  // OPENING BRACKET
    
      if (roll == 2 || roll == 3 || roll == 12) {
        
        lossCount++;
    
     } else if (roll == 7 || roll == 11) {
    
        winCount++;
    
     } else { 
    
       dieOne = (int)(Math.random()* 6) + 1;
       dieTwo = (int)(Math.random()* 6) + 1;
    
       int roll2 = dieOne + dieTwo;
    
          while (roll2 != 7) {
    
             if(roll == roll2) {
    
               winCount++;
                break;
    
             } else {
    
                dieOne = (int)(Math.random()* 6) + 1;
                dieTwo = (int)(Math.random()* 6) + 1;
                roll2 = dieOne + dieTwo;
    
                if (roll2 == 7) lossCount++;  // no brackets required
    
                  if (winCount + lossCount == 10) {
                     System.out.println("Wins: " + winCount);
                     System.out.println("Losses: " + lossCount);
                     System.out.println("Win Percentage: " + (winCount/100);
                   } // end if 
    
                } // end if
    
            }  // end if 
    
          }  // end while
    
       } // end if 
    
    } // end while
     
    If you keep doing what you've always done... you're gonna keep getting what you always got.
    #181452
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    I’ve got my brackets all matched up but it’s still acting up.

    public class craps {
    
    public static void main(String[] args) 
    {
      int dieOne = (int)(Math.random()* 6) + 1;
              int dieTwo = (int)(Math.random()* 6) + 1;
              int roll = dieOne + dieTwo;
              int winCount = 0;
              int lossCount = 0;
              double winPercent = 100.00;
          
        while (winCount + lossCount < 10000) {
          
          if (roll == 2 || roll == 3 || roll == 12){
      
         lossCount++;
      
          } else if (roll == 7 || roll == 11){
          
     winCount++;
      
      } else {
        
        dieOne = (int)(Math.random()* 6) + 1; 
                dieTwo = (int)(Math.random()* 6) + 1;
            
        int roll2 = dieOne + dieTwo;
              
           while (roll2 != 7) {
            
              if(roll == roll2) {
            
                 winCount++;
                  break;
            
              } else {
            
                 dieOne = (int)(Math.random()* 6) + 1;
                 dieTwo = (int)(Math.random()* 6) + 1;
                 roll2 = dieOne + dieTwo;
            
                 if (roll2 == 7)lossCount++;
                        
                           if (winCount + lossCount == 10000){
                       System.out.println("Wins: " + winCount);
                       System.out.println("Losses: " + lossCount);
                       System.out.println("Win Percentage: " + (winCount/winPercent) + "%");
                }
                 
              }
             
            }
          
          }
            
        }
             
      }
           
    }
    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #181519
    +1
    Eek
    Eek
    Participant
    1162

    Your “if (winCount + lossCount == 10000)” was inside the while loop defined as “while (winCount + lossCount < 10000)” Those conditions are mutually exclusive, so the printout you are looking for at the end will never happen. I cleaned it up a little bit and this appears to work (about 48%) winning percentage.

    Though running repeatedly sometimes doesnt show results.

    Wins: 4833
    Losses: 5167
    Win Percentage: 48.33%

    public class craps {
    
    public static void main(String[] args) 
    {
    int dieOne = (int)(Math.random()* 6) + 1;
    int dieTwo = (int)(Math.random()* 6) + 1;
    int roll = dieOne + dieTwo;
    int winCount = 0;
    int lossCount = 0;
    double winPercent = 100.00;
        
    while (winCount + lossCount < 10000) 
    {
    
    if (roll == 2 || roll == 3 || roll == 12)
    {
    
    lossCount++;
    
    } 
    else if (roll == 7 || roll == 11)
    {
    
    winCount++;
    
    } else 
    {
    
    dieOne = (int)(Math.random()* 6) + 1; 
    dieTwo = (int)(Math.random()* 6) + 1;
    
    int roll2 = dieOne + dieTwo;
    
    while (roll2 != 7) 
    {
    
    if(roll == roll2) 
    {
    winCount++;
    break;
    
    } 
    else 
    {
    dieOne = (int)(Math.random()* 6) + 1;
    dieTwo = (int)(Math.random()* 6) + 1;
    roll2 = dieOne + dieTwo;
    
    if (roll2 == 7)lossCount++;
    }
    
    }
    
    }
    
    }
    
    if (winCount + lossCount == 10000)
    {
    System.out.println("Wins: " + winCount);
    System.out.println("Losses: " + lossCount);
    System.out.println("Win Percentage: " + (winCount/winPercent) + "%");
    }
    }
    }
    #181847
    +1
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Eek, I had a feeling that was the problem. I just couldn’t figure out where to put the condition that creates the print out so that it would work right. Now it makes a lot more sense since I’ve looked at your code! Thanks a million!

    Bunker Mode, I really appreciate you taking the time to pass down some wisdom. I guess the plus side right now is that I find it enjoyable trying to figure out where I f~~~ed up when writing the code. It’s a puzzle of sorts and I end up smacking myself on the forehead in one of those “Eureka” type moments when I see just how difficult I was making things.

    I appreciate all the help an input from KM as well!

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #183218
    Moderator
    Moderator
    Participant
    187

    So it worked out. Good stuff.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.