java - Exiting method and main method do not work -
im making text based adventure game , i'm having trouble exiting game when player's health reaches 0. want exit method , main method , give game on message cant exit method without main looping. here's main method , code combat:
i have stats , running static variables
boolean running = true; printprogramdescription(); if(running != false) { combat(); } done(); //displays game-over , thank playing } //end of main method public static int[] combat() throws exception { random rand = new random(); string[] enemies = {"skeleton", "zombie", "ghost", "ghoul"}; string[] attackoptions = {"\t1. attack", "\t2. drink health potion", "\t3.run away!"}; boolean run = false; int healthpotiondropchance = 30; //percentage string enemy, strcombat, lowerstr; int enemyhealth, damagetaken, damagedealt; int maxenemyhealth = 60; int enemyattackdamage = 40; system.out.println(""); system.out.println("============================"); enemyhealth = rand.nextint(maxenemyhealth) + 5; enemy = enemies[rand.nextint(enemies.length)]; system.out.println(""); system.out.println("\t# " + enemy + " has appeared! #\n"); while(enemyhealth > 0) { system.out.println("\tyour hp: " + health); system.out.println("\t" + enemy + "'s hp: " + enemyhealth); system.out.println("\twhat do?"); for(int = 0; < attackoptions.length; ++) { system.out.println(attackoptions[i]); } strcombat = strinput.nextline(); lowerstr = strcombat.tolowercase(); if(lowerstr.equals("1") || lowerstr.equals("attack")) { damagedealt = rand.nextint(attackdamage); damagetaken = rand.nextint(enemyattackdamage); enemyhealth -= damagedealt; health -= (damagetaken - armor); system.out.println("\tyou attack " + enemy + " " + damagedealt + " damage."); system.out.println("\tyou recieve " + damagetaken + " damage in retaliation!"); system.out.println(""); if(health < 1) { break; } } else if(lowerstr.equals("2") || lowerstr.equals("drink health potion")) { healthpotion(); } else if(lowerstr.equals("3") || lowerstr.equals("run away")) { damagetaken = rand.nextint(enemyattackdamage); system.out.println("\tyou run away while doing so, take " + damagetaken + " damage!"); run = true; break; } else { printwithdelays("invalid command!", timeunit.milliseconds, 50); system.out.println(""); } } if(health < 1) { printwithdelays("\tyou have taken damage... cannot sustain wounds... drift off unconsciousness...", timeunit.milliseconds, 50); running = false; } else if(run == true) { system.out.println("\tthe " + enemy + " looses sight of , continue on (boring) adventure"); } else { system.out.println("===================="); system.out.println(enemy + " defeated!"); system.out.println("you have " + health + " hp left."); stats[0] = health; if(rand.nextint(100) < healthpotiondropchance ) { numhealthpotions ++; system.out.println(enemy + " has dropped health potion!"); system.out.println("you have " + numhealthpotions + " health potion(s) in inventory"); system.out.println(""); stats[1] = numhealthpotions; } } return stats;
Comments
Post a Comment