dice simulator

I am very very new to all this..

What I have to do is make a dice simulator thing where you ask the user how many dice they want to roll and how many sides they want and how many times they want to roll them. The result would be the total of the numbers rolled. If you dont understand what I'm saying is that for example it would go like this: User chooses to roll two dice and both have 11 sides. One side gets lets say 9 and the other 7 so the output would be 16.

So far I got a variables for side count, dice count, and # of rolls.

Message was edited by:

awhiteguy

[601 byte] By [awhiteguya] at [2007-11-27 7:57:04]
# 1
Take a look at the Random class, it has a method you can use to generate a random number. Then all you need is loop that repeatedly pulls a random number from your Random object.
floundera at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 2
Ok. Will write back after I look over it.Message was edited by: awhiteguy
awhiteguya at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 3

If this is not what you are looking for, let me know.

/*--

// Author List:

//deAppel<Creator>

//

// Description:

//Rolling a dice.

//

// Environment:

//This software was developed with Eclipse and Java 1.6

//

// Copyright Information:

//Copyright (C) 2007<Institution><None>

//Ark de Appel www.engineeringserver.com

//

//-*/

import java.*;

public class DiceRoll {

int rollTimes;

int sides;

public static void main(String[] args) {

DiceRoll DR = new DiceRoll();

DR.rollDice(3, 6); // roll 3 dices each with 6 sides.

}

public void rollDice(int roll, int side){

rollTimes = roll;

sides = side;

int total = 0;

System.out.println("Rolling the dice " + roll + " times with " + side + " sides");

for (int i = 0; i < roll; i++){

int dice = (int) (1+ Math.random()*side);

System.out.println("Dice(s) rolled: " + dice);

total = total + dice;

}

System.out.println("Total amount rolled: " + total);

}

}

Output:

Rolling the dice 3 times with 6 sides

Dice(s) rolled: 2

Dice(s) rolled: 6

Dice(s) rolled: 3

Total amount rolled: 11

deAppela at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 4
> If this is not what you are looking for, let me> know.We are all very impressed with your superb coding ability. You are probably the next Einstein. But do you think that you are doing the OP any good by just handing him ready-made code?
petes1234a at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 5
I do not expect him to "take the code" but rather to show him how it COULD be done. There are lots of ways to code an application with the same end result. I'm sure he can think of another way coding his application.Message was edited by: deAppel
deAppela at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 6

> I do not expect him to "take the code"

then don't give it to him. Present an example that does something similar but does not exactly reproduce what he needs. Then he can extract ideas. This is what teachers call, giving examples.

> I'm sure he can think of another way coding his

> application.

But now he is motivated not to look for these. Thanks to you.

petes1234a at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 7
I'll keep this in mind when i'm going to reply.Thanks.
deAppela at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 8

deappel that was great. some people understand more better with an example rather than explanation. i would have done the same thing what deappel did. you dont know pete how hard it is for a newbie to complete the assignment.

My english was not very good so terms like refrence and stuff like that was way too far for me. i understand by examples more than explanation.

lrngjavaa at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 9

> deappel that was great. some people understand more

> better with an example rather than explanation.

That wasn't an example. It was the answer.

> i

> would have done the same thing what deappel did. you

> dont know pete how hard it is for a newbie to

> complete the assignment.

I would guess he does, as we all do. We were all newbies at one point.

jverda at 2007-7-12 19:38:52 > top of Java-index,Java Essentials,New To Java...
# 10

I found out a number of things..

That is some impressive code though, and I read it over a couple of times and can see myself using some parts of it. I'm going to have to do more than what you wrote of course because my dice sides and rolls are all going to be inputed by the user but I know how to do that. Thank you, I think I'm ready to write my program. Will post it later here.

So nice to see people going through all that work to help me though

Message was edited by:

awhiteguy

awhiteguya at 2007-7-12 19:38:53 > top of Java-index,Java Essentials,New To Java...
# 11
Since we are offering "constructive criticism", what the heck are these lines for?rollTimes = roll;sides = side;
floundera at 2007-7-12 19:38:53 > top of Java-index,Java Essentials,New To Java...
# 12

public void rollDice(int roll, int side){

rollTimes = roll;

sides = side;

}

You can remove them if you want.

The only thing that it does is to make the global variable the same value when you create the dice in case you wantto do something with it.

DR.rollDice(3, 6); // roll 3 dices each with 6 sides.

Message was edited by:

deAppel

deAppela at 2007-7-12 19:38:53 > top of Java-index,Java Essentials,New To Java...
# 13
But your code doesn't make use of them at all. Getting rid of them is not an option in my opinion, it is a must. Why clutter your code with superfluous variables and lines of code?
floundera at 2007-7-12 19:38:53 > top of Java-index,Java Essentials,New To Java...
# 14

> But your code doesn't make use of them at all.

> Getting rid of them is not an option in my opinion,

> it is a must. Why clutter your code with superfluous

> variables and lines of code?

i actually used it for another method i made but I didn't included it in the code I posted. When i posted the example that he wanted I forgot to remove those lines. My bad.

deAppela at 2007-7-12 19:38:53 > top of Java-index,Java Essentials,New To Java...
# 15
Hence the folly of posting code rather than guiding someone to learn on their own.
floundera at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...
# 16

Now that I actually tried it I guess I did rely too much on his code...

I ended up with this which I guess is almost the exact same as his

import java.util.Scanner;

public class Main {

int roll;

int sides;

int x;

int y;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int x;

int y;

System.out.println("How many dice do you want to roll?");

x = input.nextInt();

System.out.println("How many sides do you want the dice to have?");

y = input.nextInt();

Main dr = new Main();

dr.Dice(x,y);

}

public void Dice(int roll, int side){

int total = 0;

for (int i = 0; i < roll; i++){

int dice = (int) (1+ Math.random()*side);

total = total + dice;

}

System.out.println("Total amount rolled is" + total);

}

}

But I have this whole time been studying how he has done it and learned a few things. Thanks a lot for the help, next time I'll try to rely on myself more.

But now that its done, is there anything wrong with the things I did with it? When I compiled it a couple of times with different settings it did seem right.

awhiteguya at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...
# 17

Criticisms

Personally, I would never call a class Main. You topic seems like a good candidate.

Method names should begin with lowercase.

If you want to use OO, then all the logic you have in main method (apart from object creation and method calls) could be moved into another method.eg

public static void main(String[] args) {

Foo f = new Foo();

f.methodA();

f.methodB();

}

// methods

Some believe that using the Random class, like I first suggested, is better than using the Math class.

You could get rid of the dice variable completely asnd simply add the random number to total.

floundera at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...
# 18
does it have to be a fair dice?Far easier to simulate loaded dice instead:public int roll() { return 1; }:)
jwentinga at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...
# 19

Even better.

public int roll(boolean cheat) {

if(cheat) {

return 6;

} else {

return 1;

}

}

....

if(myTurn)

die = roll(true);

} else {

die = roll(false);

}

floundera at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...
# 20

> Even better.

> > public int roll(boolean cheat) {

>if(cheat) {

>return 6;

> } else {

> return 1;

return cheat ? 6 : 1; // though isn't it cheating either way?

> if(myTurn)

>die = roll(true);

> se {

>die = roll(false);

die = roll(myTurn);

jverda at 2007-7-21 22:25:11 > top of Java-index,Java Essentials,New To Java...