# dice game code creating help!

Printable View

• 11-17-2011, 12:04 PM
singingglue
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!:(angel):
• 11-17-2011, 02:26 PM
Norm
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.
• 11-20-2011, 09:52 PM
singingglue
Re: dice game code creating help!
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!
• 11-20-2011, 09:56 PM
Norm
Re: dice game code creating help!
Quote:

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.
• 11-20-2011, 10:08 PM
singingglue
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 ??? :?
• 11-20-2011, 10:10 PM
Norm
Re: dice game code creating help!
Quote:

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?
• 11-20-2011, 10:16 PM
singingglue
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!
• 11-20-2011, 10:20 PM
Norm
Re: dice game code creating help!
Quote:

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
• 11-22-2011, 02:13 AM
singingglue
Re: dice game code creating help!
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 :(sweat):
• 11-22-2011, 02:24 AM
Norm
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?

Quote:

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-22-2011, 02:26 AM
Junky
Re: dice game code creating help!
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.
• 11-22-2011, 02:32 AM
singingglue
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.
• 11-22-2011, 02:36 AM
Junky
Re: dice game code creating help!
We understand what the program is. DO YOU understand what is wrong with your if statement?
• 11-22-2011, 02:36 AM
Norm
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
• 11-22-2011, 02:37 AM
Junky
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.
• 11-22-2011, 03:28 AM
singingglue
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?
• 11-22-2011, 03:33 AM
Norm
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.
• 11-22-2011, 03:34 AM
Junky
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.