Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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;
}
share|improve this answer
    
Thanks Ray, I will study this too <3 –  Elven X Feb 29 '12 at 5:16

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.

share|improve this answer
    
Thanks this is the one I needed, great! You helped me on this one.. <3 –  Elven X Feb 28 '12 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.