0

My object randomised spawn code always sets a weapons damage to the same number every time I run the code. Although it sometimes changes between 3 (for daggers), 4, 6 and 8, but every item's damage on the map is the same. I thought it might be because there were no breaks back when I had returns at the end of each case however this wasn't the 'case' and still only outputs the same few damages.

Example screenshot of all the sword damages as 4: http://puu.sh/hevSP/7974257751.png

It's not the hard coded damage numbers because I added 'random.nextInt(5)' to each of them and they were all still the same number.

The commented sword code at the end sets the damage to whatever I put in, so I know it's one of the switch statements causing the problem. Thanks.

    public static Object itemRoulette(String quality, int X, int Y) {

    Object returnItem = null;

    //Rolls a random item based on the input quality between 1-3.
    switch(quality)
        {
        //First tier of quality.
        case "1":
            {
            switch (random.nextInt(5) + 1)
                {
                case 1: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(2); World[X][Y].treasureName = "dagger"; returnItem = dagger; break;}
                case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(4); returnItem = sword; break;}
                case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 5: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(3); returnItem = dagger; break;}
                }
             break;
            }

        //Second tier of quality
        case "2":
            {
            switch (random.nextInt(5) + 1)
                {
                case 1: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(4); returnItem = dagger; break;}
                case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(5); sword.setDefence(1); returnItem = sword; break;}
                case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 5: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(6); sword.setDefence(1); returnItem = sword; break;}
                }
             break;
            }

        //Third tier of quality.
        case "3":
            {
            switch (random.nextInt(5) + 1)
                {
                case 1: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(6); sword.setDefence(1); returnItem = sword; break;}
                case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(7); sword.setDefence(1); returnItem = sword; break;}
                case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
                case 5: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(8); sword.setDefence(2); returnItem = sword; break;}
                }
             break;
            }

        case "dagger":
            {
            World[X][Y].treasureName = "dagger";
            Dagger dagger = new Dagger();
            dagger.setDamage(3);
            dagger.setDefence(0);
            returnItem = dagger;
            break;
            }

        case "sword":
            {
            World[X][Y].treasureName = "sword";
            Sword sword = new Sword();
            sword.setDamage(6);
            sword.setDefence(1);
            returnItem = sword;
            break;
            }
        }

    //Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(40); returnItem = sword;

    return returnItem;
    }
  • 5
    Please step through your code using the debugger. This should be a 2 minute fix for you. – Tim Biegeleisen Apr 15 '15 at 03:41
  • Your damage setting for each weapon is not random. So, what do you expect? – PM 77-1 Apr 15 '15 at 03:42
  • what are you passing with `String quality` ?? (1,2,3 or "dagger" etc.) or both ? – Anjula Ranasinghe Apr 15 '15 at 04:00
  • Yes each weapon has a hardcoded damage but it should select a random item from the nested switch statements. The String quality is where I can pass in strings from numbers for a random item of a set quality or a specific item. Currently i'm calling them method with itemRoulette("1", x, y); – Fluidic Ice Apr 15 '15 at 04:09
  • I tried this on my pc. there is no problem with switch case – Md. Nasir Uddin Bhuiyan Apr 15 '15 at 04:25
  • 1
    **Currently i'm calling them method with itemRoulette("1", x, y);** This means only `case "1"` will get called. Sothe wepon damage **will be the same** since you change the damage inside `case "sword":` etc. – Anjula Ranasinghe Apr 15 '15 at 04:31
  • I don't see how it's a 2 minute fix still, when i've spent 2 hours on this and i've been through the debugger. Also Nasir said it worked on his. Over each square on the map i'm calling "1" through "3" for different squares, so either way it should random one of 5 items for each quality and there should be SOME damage difference between the weapons. It's creating the various objects correctly, potions daggers and swords. – Fluidic Ice Apr 15 '15 at 04:56
  • Here's a pic of the items and stats it's generating: What are the chances that the 22 swords it 'randomed' from each tier quality all have 6 damage? http://puu.sh/hev21/4017a7ef4b.png – Fluidic Ice Apr 15 '15 at 05:02
  • I suspect it is something else. Have you made a simple routine to call this method multiple times? Do you find it returning same thing every time? If not, then it is something else. Symptoms looks a lot like the classic beginner mistakes of `Foo foo = new Foo(); for (i = 0; i < 100; i++) { foo.setValue(i); myList.add(foo);}` and at last you found every `Foo` in the list having same value. – Adrian Shum Apr 15 '15 at 05:53
  • My 'new' is called inside the for loop when populating the array of game squares. After some loop testing it appears the switch statement is fine, i'll have a look at anything else it could be... – Fluidic Ice Apr 15 '15 at 06:47

1 Answers1

-2

you must define this class and use its random function instead of calling random.nextInt

public class Numbers {
    Random randnum;

    public Numbers() {
        randnum = new Random();
        randnum.setSeed(123456789);
    }

    public int random(int i){
        return randnum.nextInt(i);
    }
}
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
Mohsen Bahaloo
  • 257
  • 2
  • 2