-4

With the given code:

    import java.util.Scanner;
import java.util.Random;

public class Scenario1{
    public static void line(){
        System.out.println("----------");
    }


    public static int[] CharacterSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMg;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static int[] CreatureSheet(int HP, int MaxHP,int MaxDMG, int MinDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMG;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static String[] NPC(String race){ // [0] = race; [1] = surname; [2] = lastname;
        Random r = new Random();
        String surname = "";
        String lastname1 = "";
        String lastname2 = "";
        String lastname = "";
        if(race == "dwarf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Ugbor";
                break;
            case 1:
                surname = "Hegbor";
                break;
            case 2:
                surname = "Kragen";
                break;
            case 3:
                surname = "Ogar";
                break;
            case 4:
                surname = "Nguur";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname1 = "Iron";
                break;
            case 1:
                lastname1 = "Oak";
                break;
            case 2:
                lastname1 = "Steel";
                break;
            case 3:
                lastname1 = "Brave";
                break;
            case 4:
                lastname1 = "Coward";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname2 = "hammer";
                break;
            case 1:
                lastname2 = "heart";
                break;
            case 2:
                lastname2 = "sword";
                break;
            case 3:
                lastname2 = "shield";
                break;
            case 4:
                lastname2 = "might";
                break;
            }
            lastname = lastname1 + lastname2;
        }else if(race == "elf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Zevvin";
                break;
            case 1:
                surname = "Lukias";
                break;
            case 2:
                surname = "Biinlas";
                break;
            case 3:
                surname = "Lasthandir";
                break;
            case 4:
                surname = "Iskaal";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname = "Huulrash";
                break;
            case 1:
                lastname = "Bluurkas";
                break;
            case 2:
                lastname = "Hraataas";
                break;
            case 3:
                lastname = "Lektar";
                break;
            case 4:
                lastname = "Kuulskar";
                break;
            }
        }else{
            race = "dwarf";
            surname = "Ugbor";
            lastname = "Braveshield";
        }
        String[] code;
        code = new String[3];
        code[0] = race;
        code[1] = surname;
        code[2] = lastname;
        return code;
    }

    public static void attack(int[] atkd, int[] atkr){
        Random r = new Random();
        int rand = (r.nextInt(atkr[3] - atkr[2] + 1) + 1);
        atkd[0] -= rand;
    }

    public static void main(String[] args){
        int option2;
        String option;
        int turn = 0;
        turn = 0;
        int[] atkr;
        int[] atkd;
        String temp;
        int[] nobody;
        nobody = new int[1];
        nobody[0] = 0;
        int[] Char1Sheet = CharacterSheet(8, 8, 1, 4);
        int[] NPC1Sheet = CharacterSheet(15, 15, 1, 4);
        int[] Creature1Sheet = CreatureSheet(15, 15, 1, 2);
        Scanner sc = new Scanner(System.in);
        System.out.println("We kindly ask you to choose only what the program tells you to.");
        System.out.println("Press ENTER to proceed...");
        temp = sc.nextLine();
        System.out.println("Hello, player! Please tell me your character's name:");
        String Char1Name = sc.nextLine();
        System.out.println("Say hello to " + Char1Name + "! What village is it that he lives in?");
        String Village = sc.nextLine();
        String NPC1race = "";
        Random r = new Random();
        switch(r.nextInt(2)){
        case 0:
            NPC1race = "dwarf";
            break;
        case 1:
            NPC1race = "elf";
        }
        String[] NPC1 = NPC(NPC1race);
        System.out.println(Village + ", I see! So, " + Char1Name +" knows a fellow " + NPC1[0] + ", his name is " + NPC1[1] + " " + NPC1[2] + ". They know each other very well.");
        temp = sc.nextLine();
        System.out.println("Today, they both go out into the woods next to " + Village + ", in hopes of getting some wood.");
        temp = sc.nextLine();
        System.out.println("Night falls and " + Char1Name + " and " + NPC1[1] + " are still in the woods. Sadly, they got lost, so they start searching for a shelter.");
        temp = sc.nextLine();
        System.out.println("A grey creature comes out of nowhere and attacks " + Char1Name + "!");
        line();
        attack(Char1Sheet, Creature1Sheet);
        String Creature1Name = "GreyCreature";
        while((NPC1Sheet[0] > 0 && Creature1Sheet[0] > 0) || (Char1Sheet[0] > 0 && Creature1Sheet[0] > 0)){
            System.out.println("Stats:");
            System.out.println(Char1Name + ": " + Char1Sheet[0] + "/" + Char1Sheet[1] + "HP");
            System.out.println(NPC1[1] + ": " + NPC1Sheet[0] + "/" + NPC1Sheet[1] + "HP");
            System.out.println(Creature1Name + ": " + Creature1Sheet[0] + "/" + Creature1Sheet[1] + "HP");
            if(turn == 0){
                atkr = Char1Sheet;
                atkd = nobody;
                System.out.println("It is " + Char1Name + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option=="a"){
                    System.out.println(Char1Name + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                turn = 1;
                temp = sc.nextLine();
            }else if(turn == 1){
                atkr = NPC1Sheet;
                atkd = nobody;
                System.out.println("It is " + NPC1[1] + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option=="a"){
                    System.out.println(NPC1[1] + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                turn = 2;
                temp = sc.nextLine();
            }else if(turn == 2){
                atkr = Creature1Sheet;
                atkd=nobody;
                switch(r.nextInt(2)){
                case 0:
                    atkd = Char1Sheet;
                    break;
                case 1:
                    atkd = NPC1Sheet;
                }
                if(atkd == Char1Sheet){
                    System.out.println(Creature1Name + " attacks " + Char1Name + ".");
                }else if(atkd == NPC1Sheet){
                    System.out.println(Creature1Name + " attacks " + NPC1[1] + ".");
                }
                attack(atkd, atkr);
                turn = 0;
                temp = sc.nextLine();
            }
        }
        if(Char1Sheet[0] <= 0 && NPC1Sheet[0] <= 0){
            System.out.println("Sadly, the two friends did not survive the wildernees... This was the end of our heroes!");
        }else if(Creature1Sheet[0] <= 0){
            System.out.println("The two friends continue their search for shelter.");
        }
        System.out.println("Thank you so much for playing Virtual Sample Session 1 (VSS1) of my game, currently V.Alpha0.0.0!");
        System.out.print(temp);
        temp = sc.nextLine();
        sc.close();
    }
}

The attack method won't work! It says "bound must be positive". ??? The way it works is that it takes the attacker's MaxDMG and MinDMG, then use them to give a random number between the values, then substract that number from the attacked's HP.

  • 5
    You need to compare strings using `equals()` method. – Pradeep Simha Dec 16 '16 at 08:56
  • 4
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Dec 16 '16 at 08:58
  • But they're not String; they're int[]s. I'm not worried about the rest of the code, just at the fact that attack method won't work! The rest is just fine... – Vladimir Antofi Dec 16 '16 at 09:06
  • Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Unknown Source) at Scenario1.attack(Scenario1.java:139) int rand = (r.nextInt(atkr[3] - atkr[2] + 1) + 1); at Scenario1.main(Scenario1.java:183) attack(Char1Sheet, Creature1Sheet); – Vladimir Antofi Dec 16 '16 at 09:09
  • The exception message is clear: you are calling `r.nextInt(int bound)` with a value for bound that is 0 or negative. Therefore `atkr[2]` must be greater than `atkr[3]`. I would suggest that you print out the actual values of `atkr[2]` and `atkr[3]` just before you try to call `r.nextInt(..)` – Thomas Kläger Dec 16 '16 at 09:55
  • But here's the thing: I am substracting `atkr[2]` from `atkr[3]` . And if you look at my code, `atkr[2]= 1` and `atkr[3]=2`. Therefore, `atkr[3] - atkr[2] + 1 > 0`! So, everything is supposed to be positive... Now, I've done what you said, and the console says `1=atkr[3]` and `2=atkr[2]`, which is dumb, considering that obviously with the given code the following variables are `2=atkr[3]` and `1=atkr[2]`, as you can see in the main method where I have `int[] Creature1Sheet = CreatureSheet(15, 15, 1, 2); `! So, whaat? – Vladimir Antofi Dec 16 '16 at 20:09
  • Now, I;ve just realized that I switched MinDMG with MaxDMG. Sorry I am idiot, and that .equals() solved a problem I was about to have! Thanks! I have done a few changes to the program and now I love it! – Vladimir Antofi Dec 17 '16 at 12:19

1 Answers1

0

Now, I;ve just realized that I switched MinDMG with MaxDMG. Sorry I am idiot, and that .equals() solved a problem I was about to have! Thanks! I have done a few changes to the program and now I love it! If anybody wishes to play it, I'm glad you do! Thank you again!

import java.util.Scanner;
import java.util.Random;

public class Scenario1{
    public static void line(){
        System.out.println("----------");
    }


    public static int[] CharacterSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMg;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static int[] CreatureSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMG;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static String[] NPC(String race){ // [0] = race; [1] = surname; [2] = lastname;
        Random r = new Random();
        String surname = "";
        String lastname1 = "";
        String lastname2 = "";
        String lastname = "";
        if(race == "dwarf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Ugbor";
                break;
            case 1:
                surname = "Hegbor";
                break;
            case 2:
                surname = "Kragen";
                break;
            case 3:
                surname = "Ogar";
                break;
            case 4:
                surname = "Nguur";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname1 = "Iron";
                break;
            case 1:
                lastname1 = "Oak";
                break;
            case 2:
                lastname1 = "Steel";
                break;
            case 3:
                lastname1 = "Brave";
                break;
            case 4:
                lastname1 = "Coward";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname2 = "hammer";
                break;
            case 1:
                lastname2 = "heart";
                break;
            case 2:
                lastname2 = "sword";
                break;
            case 3:
                lastname2 = "shield";
                break;
            case 4:
                lastname2 = "might";
                break;
            }
            lastname = lastname1 + lastname2;
        }else if(race == "elf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Zevvin";
                break;
            case 1:
                surname = "Lukias";
                break;
            case 2:
                surname = "Biinlas";
                break;
            case 3:
                surname = "Lasthandir";
                break;
            case 4:
                surname = "Iskaal";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname = "Huulrash";
                break;
            case 1:
                lastname = "Bluurkas";
                break;
            case 2:
                lastname = "Hraataas";
                break;
            case 3:
                lastname = "Lektar";
                break;
            case 4:
                lastname = "Kuulskar";
                break;
            }
        }else{
            race = "dwarf";
            surname = "Ugbor";
            lastname = "Braveshield";
        }
        String[] code;
        code = new String[3];
        code[0] = race;
        code[1] = surname;
        code[2] = lastname;
        return code;
    }

    public static void attack(int[] atkd, int[] atkr){
        Random r = new Random();
        int rand = 2;
        rand = atkr[3] - atkr[2] + 1;
        atkd[0] -= r.nextInt(rand) + 1;
    }

    public static void main(String[] args){
        int option2;
        String option;
        int turn = 0;
        turn = 0;
        int[] atkr;
        int[] atkd;
        String temp;
        int[] nobody;
        nobody = new int[1];
        nobody[0] = 0;
        int[] Char1Sheet = CharacterSheet(8, 8, 1, 4);
        int[] NPC1Sheet = CharacterSheet(15, 15, 1, 4);
        int[] Creature1Sheet = CreatureSheet(40, 40, 1, 4);
        Scanner sc = new Scanner(System.in);
        System.out.println("We kindly ask you to choose only what the program tells you to.");
        System.out.println("Press ENTER to proceed...");
        temp = sc.nextLine();
        System.out.println("Hello, player! Please tell me your character's name:");
        String Char1Name = sc.nextLine();
        System.out.println("Say hello to " + Char1Name + "! What village is it that he lives in?");
        String Village = sc.nextLine();
        String NPC1race = "";
        Random r = new Random();
        switch(r.nextInt(2)){
        case 0:
            NPC1race = "dwarf";
            break;
        case 1:
            NPC1race = "elf";
        }
        String[] NPC1 = NPC(NPC1race);
        System.out.println(Village + ", I see! So, " + Char1Name +" knows a fellow " + NPC1[0] + ", his name is " + NPC1[1] + " " + NPC1[2] + ". They know each other very well.");
        temp = sc.nextLine();
        System.out.println("Today, they both go out into the woods next to " + Village + ", in hopes of getting some wood.");
        temp = sc.nextLine();
        System.out.println("Night falls and " + Char1Name + " and " + NPC1[1] + " are still in the woods. Sadly, they got lost, so they start searching for a shelter.");
        temp = sc.nextLine();
        System.out.println("A grey creature comes out of nowhere and attacks " + Char1Name + "!");
        line();
        attack(Char1Sheet, Creature1Sheet);
        String Creature1Name = "GreyCreature";
        while((NPC1Sheet[0] > 0 && Creature1Sheet[0] > 0) || (Char1Sheet[0] > 0 && Creature1Sheet[0] > 0)){
            System.out.println("Stats:");
            System.out.println(Char1Name + ": " + Char1Sheet[0] + "/" + Char1Sheet[1] + "HP");
            System.out.println(NPC1[1] + ": " + NPC1Sheet[0] + "/" + NPC1Sheet[1] + "HP");
            System.out.println(Creature1Name + ": " + Creature1Sheet[0] + "/" + Creature1Sheet[1] + "HP");
            if(turn % 3 == 0 && Char1Sheet[0] > 0){
                atkr = Char1Sheet;
                atkd = nobody;
                System.out.println("It is " + Char1Name + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option.equals("a")){
                    System.out.println(Char1Name + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                temp = sc.nextLine();
            }else if(turn % 3 == 1 && NPC1Sheet[0] > 0){
                atkr = NPC1Sheet;
                atkd = nobody;
                System.out.println("It is " + NPC1[1] + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option.equals("a")){
                    System.out.println(NPC1[1] + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                temp = sc.nextLine();
            }else if(turn % 3 == 2 && Creature1Sheet[0] > 0){
                atkr = Creature1Sheet;
                atkd=nobody;
                switch(r.nextInt(2)){
                case 0:
                    atkd = Char1Sheet;
                    break;
                case 1:
                    atkd = NPC1Sheet;
                }
                if(atkd == Char1Sheet && Char1Sheet[0] > 0){
                    System.out.println(Creature1Name + " attacks " + Char1Name + ".");
                }else{
                    atkd = NPC1Sheet;
                    System.out.println(Creature1Name + " attacks " + NPC1[1] + ".");
                }
                attack(atkd, atkr);
                temp = sc.nextLine();
            }
            turn++;
        }
        if(Char1Sheet[0] <= 0 && NPC1Sheet[0] <= 0){
            System.out.println("Sadly, the two friends did not survive the wildernees... This was the end of our heroes!");
        }else if(Creature1Sheet[0] <= 0){
            System.out.println("The two friends continue their search for shelter.");
        }
        System.out.println("Thank you so much for playing Virtual Sample Session 1 (VSS1) of my game, currently V.Alpha0.0.0!");
        System.out.print(temp);
        temp = sc.nextLine();
        sc.close();
    }
}