Results 1 to 18 of 18
  1. #1
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default dice game code creating help!

    Hello! I know it is silly to ask that kind of question but can someone help me to create code to kind of the dice game that with random number generator simulating dice rolls. And rolling ends only when comes 1 or 6! And print out every roll points and all score and throw times!

    Thank you in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    There are many people here that will help you get your code to work.
    Post your code and your questions. Or ask specific questions about the problems you are having.

  3. #3
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    Java Code:
    import java.util.Random;
    
    //genere nejausi izveletus skaitljus
    public final class Dice {
      
      public static final void main(String... aArgs){
        log("Jusu metieni:");
        
        int START = 1;
        int END = 6;
        Random random = new Random();
        for (int idx = 1; idx <= 100; ++idx){
          showRandomInteger(START, END, random);
        }
        
        log("Pabeigts.");
      }
      
      private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
        if ( aStart > aEnd ) {
          throw new IllegalArgumentException("Start cannot exceed End.");
        }
        
        long range = (long)aEnd - (long)aStart + 1;
       
        long fraction = (long)(range * aRandom.nextDouble());
        int randomNumber =  (int)(fraction + aStart);    
        log("Jusu metienu rezultati : " + randomNumber);
      }
      
      private static void log(String aMessage){
        System.out.println(aMessage);
      }
    }
    I know that in it there is still a lot of work but there is nothing working out for me!
    Thank you!
    Last edited by Norm; 11-20-2011 at 09:11 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    there is nothing working out for me!
    Please explain. Show the program's output and explain what is wrong with what it printed.
    If you get errors copy and paste here the full text.

  5. #5
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    that far everything is okay but i cant understand how can i make it to stop on 1 or 6 ??? :?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    how can i make it to stop on 1 or 6
    What are you trying to stop?

    Can you show the programs output and add comments to that output showing where the problem is?

  7. #7
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    --------------------Configuration: <Default>--------------------
    Jusu metieni:
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 1
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 4
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 2
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 5
    Jusu metienu rezultati : 6
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 3
    Jusu metienu rezultati : 5
    Pabeigts.

    Process completed.



    Its like a Dice with that random number generator it generates the dice throws and i have to stop when comes one or six but i dont know how!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    i have to stop when comes one or six but i dont know how!
    Use an if statement with a condition that compares the value to 1 or 6.
    When the condition is true (the value is 1 or 6) then do what you want.

    What do you mean by "stop"?
    The System.exit(0) method will stop the execution of the program.
    A break statement will stop the execution of a loop by exiting from the loop

  9. #9
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    Java Code:
    import java.util.Random;
    
    //genere nejausi izveletus skaitljus
    public final class Kaulins23 {
      
      public static final void main(String... aArgs){
        log("Jusu metieni:");
        
        int   START = 1;
        int  END = 6;
        Random random = new Random();
        for (int  idx = 1; idx <= 100; ++idx){
          showRandomInteger(START, END, random);
         
          
        if (idx == 6, 1) // and if it is six or one it has to stop rolling the dice
        	System.out.println("");
        else
        	System.out.println("");
        	
        	System.out.println("Rezultats ir" +sum+= idx ); // so here i need to count all the numbers
        
          
          
        }
        
        log("Pabeigts.");
      }
      
      private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
        if ( aStart > aEnd ) {
          throw new IllegalArgumentException("Nevar sasniegt beigas.");
        }
        
        long range = (long)aEnd - (long)aStart + 1;
        
        
        
        long fraction = (long)(range * aRandom.nextDouble());
        int randomNumber =  (int)(fraction + aStart);    
        log("Jusu metiena rezultats : " + randomNumber);
      }
      
      private static void log(String aMessage){
        System.out.println(aMessage);
      }
    }
    So i know i am silly but i cant get it! So i have to write code who simulates dice rolling and rolling have to stop when it rolls 1 or 6 and count all result!
    Thanks all
    Last edited by Norm; 11-22-2011 at 01:21 AM. Reason: added code tags

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    Can you write the steps you want the program to do in pseudo code: Simple statements in English instead of Java?

    if (idx == 6, 1)
    What computer programming languages do you know that you copied this statement from?
    Do you have a tutorial or text book you can read that defines how to create conditional expressions?
    This statement is not valid java.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: dice game code creating help!

    Java Code:
    if (idx == 6, 1)
    Is that valid Java syntax? Have you seen any examples like that in a book or in an online tutorial? I bet there are many examples that show you how to make 2 or more comparisons in an if statement.

  12. #12
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    at first it has to generate random numbers from 1 to 6 (like dice).
    then it has to stop when it comes 1 or 6
    and at the end it have to count all the numbers what comes out.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: dice game code creating help!

    We understand what the program is. DO YOU understand what is wrong with your if statement?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    That is the big view, now break each of those statement up into smaller steps.
    For example you need to show where the numbers are added up. It must be done as each number is generated,
    not at the end after a 1 or 6.
    Show where the begin and end of the loop is. for example:
    begin loop
    get number
    test number
    do something with number
    end loop >> here the logic goes back to the begin loop
    do something here

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: dice game code creating help!

    Oh wow I just realised you are using the loop index in the if statement and not the random number. Another issue to resolve.

  16. #16
    singingglue is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: dice game code creating help!

    import java.util.Random;

    class kaulins {
    public static void main (String[] args) {
    Random kaulinss = new Random();
    int skaitlis;


    for(int counter=1; counter<=20; counter++){
    skaitlis = 1+kaulinss.nextInt(6);
    System.out.println(skaitlis+" - metiena rezultats");

    if (skaitlis == 6)
    System.out.println ("");

    if (skaitlis == 1)
    System.out.print("");

    else
    System.out.println ("");





    }


    }



    }



    Like this is on right way?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: dice game code creating help!

    No. I suggest that you work on the logic first. After you have the logic, then you can write the code.
    See post #14.
    You need to think what the steps are your program is to do first.
    Then you write code to do those steps.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: dice game code creating help!

    Sure but you can put the check for 1 and 6 in the same if statement. Hence my earlier comment about looking for examples with 2 or more conditions in the if statement.

Similar Threads

  1. Pig dice game
    By dangyounoobs in forum New To Java
    Replies: 2
    Last Post: 12-09-2013, 04:21 AM
  2. Pig Dice Game Help
    By pvccstudent in forum New To Java
    Replies: 3
    Last Post: 07-10-2011, 02:21 AM
  3. Dice game help and arrays
    By SnarkKnuckle in forum New To Java
    Replies: 4
    Last Post: 03-10-2011, 01:57 AM
  4. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM
  5. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 01:01 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
  •