Text-Based Game in Java. Need advice with Inventory Implementation and general coding advice -


i'm having trouble figuring out how deal inventory text based java game. finished data structures , algorithms, figured project resume.

currently, create inventory in constructor of player class. instantiate inventory 3 potion items, item class. try in player constructor, can't figure out why not working.

my question is, how should go instantiating every character 3 potions in his/her inventory?


player class:

package projectmoria;   import java.util.arraylist; import java.util.list;   public class player {      private final string name;     private final string description;     private final int maxhitpoints;     private int hitpoints;     private final int mindamage;     private final int maxdamage;     private final int defense;     private double critchance;     private int currx;     private int curry;     private room currroom;     private list<item> inventory;      public player(string name, string description, int maxhitpoints,             int mindamage, int maxdamage, int defense, double critchance) {         this.name = name;         this.description = description;         this.maxhitpoints = maxhitpoints;         this.hitpoints = maxhitpoints;         this.mindamage = mindamage;         this.maxdamage = maxdamage;         this.defense = defense;         this.critchance = critchance;         this.currx = 14;         this.curry = 14;         inventory = new arraylist<>();         inventory.add(item.addpotion(3, this.player)); //this line need     }      public int attack() {         return projectmoria.rand.nextint(maxdamage - mindamage + 1);     }      public int defend(monster monster) {         int incomingattack = monster.attack();         int random = projectmoria.rand.nextint(99) + 1;         if (random <= monster.getcritchance()) {             incomingattack = incomingattack * 2;             io.monstercrit(); //todo - move different spot         }         io.playerhitpointsmessage(incomingattack, monster);         hitpoints = (hitpoints * defense > incomingattack)                 ? hitpoints - incomingattack : 0;         return hitpoints;     }      public void heal(item potion){         this.hitpoints =+ 20;         inventory.remove(potion);         io.heal(this.hitpoints);     }      public static player newwarrior() {         return new player("warrior", "a tough, well-rounded fighter with"                 + " balanced skillset.", 100, 20, 30, 3, 10);     }      public static player newduelist() {         return new player("duelist", "a quick, nimble duelist an"                 + " aptitude landing critical attacks.", 8000, 10, 50, 2,                  18);     }       public string getdescription() {         return description;     }      public int gethitpoints() {         return hitpoints;     }      public boolean isalive() {         return hitpoints > 0;     }      public string getname() {         return name;     }      public int getmaxhitpoints() {         return maxhitpoints;     }      public int getmindamage() {         return mindamage;     }      public int getmaxdamage() {         return maxdamage;     }      public int getdefense() {         return defense;     }      public double getcritchance() {         return critchance;     }      public int getcurrx() {         return currx;     }      public int getcurry() {         return curry;     }      public list<item> getinventory() {         return inventory;     }       public room getcurrroom() {         return currroom;     }      public void setcurrroom(room room) {         currroom = room;     }      public void setcurrx(int currx) {         this.currx = currx;     }      public void setcurry(int curry) {         this.curry = curry;     } } 

item class:

package projectmoria;   public class item {      private final string name;     private final string type;     private final string description;      public item(string name, string type, string description){         this.name = name;         this.type = type;         this.description = description;     }      public void use(player player, item item){         if(item.type.equals("potion")){             player.heal(item);         }     }      public void addpotion(int numofpotions, player player){         for(int = 0; < numofpotions; ++){             player.getinventory().add(potion());         }     }      public item potion(){         return new item ("potion", "potion", " small vial filled "                 + "translucent red liquid");     } } 

actually in player constructor :

inventory = new arraylist<>(); inventory.add(item.addpotion(3, this.player));  

you invoking :

public void addpotion(int numofpotions, player player){ 

this instance method.
can invoke instance methods on instance.

your addpotion() method looks factory method create potion items :

public void addpotion(int numofpotions, player player){     for(int = 0; < numofpotions; ++){         player.getinventory().add(potion());     } }  public item potion(){     return new item ("potion", "potion", " small vial filled "             + "translucent red liquid"); } 

so, should static addpotion() , potion() , should solve problem.
besides, potion() invoked addpotion(), can reduce level access making private.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -