2
\$\begingroup\$

I am making a console based strategy game like Genghis Khan: Legend of the White Wolf, although it is in console. How can I implement the Random Calculation in there? First off, the game goes like this, for example you have 34 Soldiers and the enemy has 45 Archers. Say you attack the Archers, normally it is not good to subtract the number of archers from the number of Soldiers. How can I implement a random difference using Random Method? I made it this way though:

int result = 1+r.nextInt(20);
        for (int j = 1; j <= 1; j++) {
            System.out.println(result);
        }
}
attacker -= result;

Is that okay? How about for the Defender? Should I make another random result for the damage to them? Is there an easy coding here rather than this lengthy one I did?

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

i made this dice roller. it may be of interest.

import java.util.Random;

public class DiceRoller {
    DiceRoller(final int sides) {
        this(1, sides, 0, 0);
    }

    DiceRoller(final int dice, final int sides) {
        this(dice, sides, 0, 0);
    }

    DiceRoller(final int dice, final int sides, final int netAdjustment, final int sidesAdjustment) {
        this.sides = sides;
        this.dice = dice;
        this.netAdjustment = netAdjustment;
        this.sidesAdjustment = sidesAdjustment;
    }

    int roll() {
        int sum=netAdjustment+sidesAdjustment*dice;
        for(int i=0;i<dice;i++)
            sum+=random.nextInt(sides)+1;
        return sum;
    }

    final Random random = new Random();
    final int dice;
    final int sides;
    final int netAdjustment;
    final int sidesAdjustment;
}
\$\endgroup\$
0
1
\$\begingroup\$

You could take the real difference, and then add/subtract a random amount or percentage from it.

Pardon the pseudocode, I barely know Java:

int difference = Math.abs(enemy.units - player.units);

int thirtyPercentDifference = Math.round(difference * 0.3f);
int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);

What the above code would do is take that actual difference and then add +/- 0-30% to it. You should be able to use randomDifference as your final result. Let's plug in the numbers in your example as see what we get.

//int difference = Math.abs(enemy.units - player.units);
abs(enemy.units - player.units) = abs(45 - 34) = 9

//int thirtyPercentDifference = Math.round(difference * 0.3f);
9 * 0.3f = 2.7f, rounded to nearest int = 3

//int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
Then we randomly get a number either 0 or 1, let's just say it is zero. In that case our randomNegative is -1.

//int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);
9 + ((random number between 0 and 3) * -1) = A number between 6 and 9.

The full range of what we could have gotten is 6 - 12 with the above algorithm. And if you wanted it even more random you could randomize the percentage used in place of the hard-coded 0.3f.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks this is the one I needed, great! You helped me on this one.. <3 \$\endgroup\$
    – Elven X
    Commented Feb 28, 2012 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .