java - How to choose a spesific object from ArrayList -


we making simple rpg game. right have class called battleground, player can fight monsters. use random generator pick out random monster when start fight. in terminal/main method, have commands "attack" , "run", either damage random monster, or make player leave/quit game. right now, trying add command called "attack scariest", let player fight against hardest monster damage(there 3 choose in our main). need method choose specific object arraylist monsters, based on damage. have tips on how that?

this our code in battleground class starts game:

 public void startbattle() {     printwelcomemessage();     boolean finished = false;       this.currentmonster = getrandommonster();       if(this.currentmonster == null) {         return;     }      system.out.println("a random monster chosen you. prepare meet mighty " + this.currentmonster.getname());     system.out.println("\n-------- player stats ---------");     system.out.println(this.player);     while(!finished && monsters.size() > 0) {         arraylist<string> commands = reader.getinput();          if(isvalidcommand(commands)) {              if(commands.contains("quit") && !this.currentmonster.isdead()){                 system.out.println("you can't quit game in middle of fight!");             }else if(commands.contains("quit") && this.currentmonster.isdead()) {                 finished = true;                 system.out.println();                 printfinalstats();                 system.out.println();                 system.out.println("you have left arena, , game has ended.");                 system.out.println();             }               if(commands.contains("run")) {                 finished = true;                 system.out.println("you coward , lose 50 gold pieces...\n");                 this.player.changegold(-50);                 printfinalstats();                 system.out.println("\nthanks playing...");              }else if(commands.contains("drink") && !this.currentmonster.isdead()){                 potion potion = (potion) player.finditem(commands.get(1));                 player.drinkpotion(potion);              }else if(commands.contains("equip") && !this.currentmonster.isdead()){             weapons weapon = (weapons) player.finditem(commands.get(1));             player.useweapon(weapon);             } else if(commands.contains("attack") && !this.currentmonster.isdead()) {                  if(this.player.attack(this.currentmonster)) {                         playerwon();                     if(this.monsters.size() > 0) {                         system.out.println("\nthere " + this.monsters.size() + " more monsters beat\ntype \"attack\" if want attack monster, or \"quit\" if want end game.");                                      } else {                         system.out.println("\n\n#### congratulations ####\nyou have beaten every single monster in game. true champion!");                         printfinalstats();                         finished = true;                     }                  } else if(this.currentmonster.attack(this.player)) {                     printlosingmessage();                     finished = true;                 }              } else if(commands.contains("attack") && this.currentmonster.isdead() && this.monsters.size() > 0) {                 this.currentmonster = getrandommonster();                 printcontinuemessage(this.player, this.currentmonster);                 this.player.changehealth(50);             }          } else {             system.out.println("please write valid command. valid commands are:");             printcommands();         }     } } 

this arraylist of monsters in main class:

 monster beelzebub = new monster("beelzebub", 60);     monster witch = new monster("witch", 40);     monster ogre = new monster("ogre", 80);   arraylist<monster> monsters = new arraylist<>();      monsters.add(beelzebub);     monsters.add(witch);     monsters.add(ogre);    battleground battleground = new battleground(monsters, player, reader);     battleground.startbattle(); 

we appreciate help!

replace arraylist sortedset or priorityqueue. implement comparator monster class. pick first element of monsters collection.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -