Need Java help

Grab your favourite IDE and tinker with the innards of game engines

Need Java help

Postby Garrador on Sat Apr 28, 2007 4:03 pm

I'm (for training) making a text based war game, and i need to get a random number (for friendly soldiers killed) within a limit that the user defines.

The user can choose how many soldiers he want to take with him from the camp, and that amount is stored in a "int footSoldAmount". Then i need to generate a random number that will not go over the footSoldAmount that the user defined. And i cant do that with a

Code: Select all
friendlySoldKilled = killedFriendlyRandom.nextInt(the random nuber i need);


so i wonder if anyone could help me generate a random nuber that will stop at the user defined value?

I cant get the Math.random() to work correctly if that is the way out.

Help? :P
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Postby jaddorii on Sat Apr 28, 2007 4:51 pm

Hmm dont know in java but in C++ it's something like srand (0); then rand() % [highest amount of kills] or something like that. Try googling Make random dice in java or something like that.
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby Garrador on Sat Apr 28, 2007 5:13 pm

ok... i don't think srand(0) works in java, but ill try some stuff. I'm playing with some if statements, but their not accutrate enough kind of. Im working on a

Code: Select all
if ((friendlySoldKilled > footSoldAmount) && (friendlyTanksKilled > tanksAmount))
                {
                 out.print("You lost your whole army. Want to continue? Y/N");
                 
                 int ContiAnswer;
                    ContiAnswer = choiceScanner.findInLine(".").charAt(0);
               
                    if ((ContiAnswer == 'y') || (ContiAnswer == 'Y'))
                    {
                       how to restart the game           
         }
}
               


but that is if i cant generate a good enough number...

But it still is a possibility.

I have another question. look at what's bold in the code. if the answer is yes, i want the program to kind of restart.
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Postby zombie@computer on Sat Apr 28, 2007 6:08 pm

math random all the way. it generates a random number 0 < n < 1, so to get a number between 0 and 1569 you would use

int r = (int)(1569*Math.random());
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Postby Garrador on Sat Apr 28, 2007 9:33 pm

ah cool. Thanks. I'll test when i get home.
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Postby Forceflow on Sun Apr 29, 2007 11:53 am

User avatar
Forceflow
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 12:13 pm
Location: Belgium

Return to Programming

Who is online

Users browsing this forum: No registered users