Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- public class lel {
- static int health = 100;
- static int gold = 0;
- static int exp = 0;
- static int playerChosenDirection;
- static int battleChoice;
- static int monsterAt;
- static int chestAt;
- static int enemyHealth;
- static int potion = 3;
- static int amount;
- static char userInput = 0;
- static String uncheckedInput;
- static boolean die = false;
- static boolean chest;
- static int chestLoot;
- static int chestGold;
- static int chestPotion;
- static int chestTrap;
- static int chestNothing;
- static int level = 1;
- static int amountNorth = 0;
- static int amountEast = 0;
- static int amountStep = 0;
- static boolean leave;
- static int shopAt;
- public static void main(String[] args) {
- JOptionPane.showMessageDialog(null, "WELCOME.");
- JOptionPane.showMessageDialog(null, "You are in a dungeon and slowly bleeding out every step. You have 3 health potions.");
- JOptionPane.showMessageDialog(null, "You have the choice of going (N)orth, (E)ast, (S)outh, (W)est or (P)otion");
- JOptionPane.showMessageDialog(null, "Good luck! Monsters have a chance of spawning and a chance of dropping a health potion and give exp and gold. You may also find a chest with loot or a shop.");
- while(health > 0){
- do{
- uncheckedInput = (JOptionPane.showInputDialog("Which way do you go, (N) (E) (S) (W) or (P)? " + "You have "+ health + " health."));
- if(!uncheckedInput.equals("")) {
- userInput = Character.toUpperCase(uncheckedInput.charAt(0));
- }
- }
- while(userInput != 'N' && userInput != 'S'&& userInput != 'E'&& userInput != 'W' && userInput != 'P');
- health--;
- System.out.println("You have "+health+ " left.");
- playerChosenDirection = Move (userInput);
- monsterAt = (int)Math.floor(Math.random() * 4); //generates the monster at one of the random directions therefore 25% chance at encountering it.
- if (playerChosenDirection == monsterAt) {
- //battle
- System.out.println("Battle!");
- JOptionPane.showMessageDialog(null, "You have encountered an enemy!");
- //JOptionPane.showMessageDialog(null, "It has " +enemyHealth+ " health."); // again, i need to make enemyhealth usable from multiple parts of code
- enemyHealth = (int)((Math.random()*10) + 10); //generate monster spawn
- battle(); //start battle
- }
- else
- {
- // WOW THERE WAS A LINE HERE, playerChosenDirection = Move (userInput); THAT I DIDNT NEED THAT WAS THE DOUBLE PRINTING BUG WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
- chestAt = (int)Math.floor(Math.random() * 8); //generates the chest at one of the random directions therefore 20% chance at encountering it.
- if (playerChosenDirection == chestAt) {
- System.out.println("You found a chest.");
- JOptionPane.showMessageDialog(null, "You have found a chest!");
- JOptionPane.showMessageDialog(null, "It looks like it could be trapped, do you want to (o)pen it or (l)eave it?");
- chest();
- }
- else if(amountStep>25){
- int exit = (int)Math.floor(Math.random() * 16); //exit 1/16 chance after 25 moves
- if (playerChosenDirection == exit) {
- System.out.println("You have found an exit!");
- JOptionPane.showMessageDialog(null, "You have found an exit!");
- JOptionPane.showMessageDialog(null, "Do you want to leave now?");
- leave();
- }
- else if(gold>10)
- {
- shopAt = (int)Math.floor(Math.random() * 10);
- if (playerChosenDirection == shopAt) {
- System.out.println("You found a shop.");
- JOptionPane.showMessageDialog(null, "You have found a shop!");
- JOptionPane.showMessageDialog(null, "Do you buy a potion for 10 gold?");
- shop();
- }
- else{
- }
- }
- }
- }
- }
- }
- public static void chest(){
- uncheckedInput = JOptionPane.showInputDialog("What do you do?");
- if (!uncheckedInput.equals("")) {
- userInput = Character.toUpperCase(uncheckedInput.charAt(0));
- } else{
- }
- switch(userInput) {
- case 'O':
- chest = true;
- break;
- case 'L':
- chest = false;
- break;
- default:
- //chest(); fixed by removing
- break;
- }
- if(chest == false){
- }
- else if(chest == true){
- int loot = (int) (Math.random() * 4);
- switch(loot) {
- case 1:
- if(loot == 0){
- gold += (int)(Math.random()*6 + 5);
- System.out.println("You found some gold! You now have " + gold + " gold.");
- JOptionPane.showMessageDialog(null, "You found some gold! You now have " + gold + " gold.");
- break;
- }
- case 2:
- if (loot == 1){
- potion++;
- System.out.println("You found a potion!");
- JOptionPane.showMessageDialog(null, "You found a potion! You now have " + potion + " potions.");
- break; }
- case 3:
- if (loot == 2){
- health -= (int)(Math.random()*16 + 10);
- System.out.println("Oh no! The chest was trapped. You took some damage...");
- JOptionPane.showMessageDialog(null, "Oh no! The chest was trapped. You now have " + health + " health.");
- break;
- }
- case 4:
- if (loot ==3) {
- System.out.println("No loot from that chest.");
- JOptionPane.showMessageDialog(null, "You found nothing...");
- break;
- }
- case 5:
- if (loot == 4 ) {
- System.out.println("No loot from that chest.");
- JOptionPane.showMessageDialog(null, "You found nothing...");
- break;
- }
- }
- }
- }
- public static int Move (char direction) // receives user input and gives you an integer coord in exchange
- {
- switch (direction){
- case 'N':
- System.out.println("You went north.");
- amountStep++;
- amountNorth++;
- if(amountNorth>0 && amountEast>0){
- System.out.println("You are at North " + amountNorth + " and East " + amountEast);
- } else if(amountNorth<0 && amountEast<0) {
- System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
- }
- else if(amountNorth>0 && amountEast<0){
- System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast>0){
- System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
- }
- else if(amountNorth==0 && amountEast==0){
- System.out.println("You are in between North and South and East and West");
- }
- else if(amountNorth==0 && amountEast<0){
- System.out.println("You are inbetween North and South and at West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast==0){
- System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
- }
- else if(amountNorth==0 && amountEast>0){
- System.out.println("You are inbetween North and South and at East " + amountEast);
- }
- else if(amountNorth>0 && amountEast==0){
- System.out.println("You are at North " + amountNorth + " and in between East and West");
- }
- return 0;
- case 'E':
- System.out.println("You went east.");
- amountStep++;
- amountEast++;
- if(amountNorth>0 && amountEast>0){
- System.out.println("You are at North " + amountNorth + " and East " + amountEast);
- } else if(amountNorth<0 && amountEast<0) {
- System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
- }
- else if(amountNorth>0 && amountEast<0){
- System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast>0){
- System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
- }
- else if(amountNorth==0 && amountEast==0){
- System.out.println("You are in between North and South and East and West");
- }
- else if(amountNorth==0 && amountEast<0){
- System.out.println("You are inbetween North and South and at West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast==0){
- System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
- }
- else if(amountNorth==0 && amountEast>0){
- System.out.println("You are inbetween North and South and at East " + amountEast);
- }
- else if(amountNorth>0 && amountEast==0){
- System.out.println("You are at North " + amountNorth + " and in between East and West");
- }
- return 1;
- case 'S':
- System.out.println("You went south.");
- amountStep++;
- amountNorth--;
- if(amountNorth>0 && amountEast>0){
- System.out.println("You are at North " + amountNorth + " and East " + amountEast);
- } else if(amountNorth<0 && amountEast<0) {
- System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
- }
- else if(amountNorth>0 && amountEast<0){
- System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast>0){
- System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
- }
- else if(amountNorth==0 && amountEast==0){
- System.out.println("You are in between North and South and East and West");
- }
- else if(amountNorth==0 && amountEast<0){
- System.out.println("You are inbetween North and South and at West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast==0){
- System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
- }
- else if(amountNorth==0 && amountEast>0){
- System.out.println("You are inbetween North and South and at East " + amountEast);
- }
- else if(amountNorth>0 && amountEast==0){
- System.out.println("You are at North " + amountNorth + " and in between East and West");
- }
- return 2;
- case 'W':
- System.out.println("You went west.");
- amountStep++;
- amountEast--;
- if(amountNorth>0 && amountEast>0){
- System.out.println("You are at North " + amountNorth + " and East " + amountEast);
- } else if(amountNorth<0 && amountEast<0) {
- System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
- }
- else if(amountNorth>0 && amountEast<0){
- System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast>0){
- System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
- }
- else if(amountNorth==0 && amountEast==0){
- System.out.println("You are in between North and South and East and West");
- }
- else if(amountNorth==0 && amountEast<0){
- System.out.println("You are inbetween North and South and at West " + amountEast*-1);
- }
- else if(amountNorth<0 && amountEast==0){
- System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
- }
- else if(amountNorth==0 && amountEast>0){
- System.out.println("You are inbetween North and South and at East " + amountEast);
- }
- else if(amountNorth>0 && amountEast==0){
- System.out.println("You are at North " + amountNorth + " and in between East and West");
- }
- return 3;
- case 'P':
- if (potion > 0) {
- potion--; //remove 1 potion
- health++;
- amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
- health += amount; //heal from potion
- if (health > 100) {
- health = 100;
- System.out.println("You drink a potion.");
- System.out.println("You healed " + amount + " health! you now have " + health + " health.");
- System.out.println("You have " + potion + " potions left.");
- }
- } else {
- System.out.println("You have 0 potions to drink.");
- JOptionPane.showMessageDialog(null, "You have no potions to drink.");
- }
- return 5;
- default:
- return 6;
- }
- }
- public static void battle ()
- {
- if (health > 0) { //check you're alive.
- uncheckedInput = JOptionPane.showInputDialog(null, "What do you do, (A)ttack (D)efend (P)otion or (R)un?"); //take input string
- if (!uncheckedInput.equals("")) { //check there actually is a character at (0) to stop game crashing
- battleChoice = Character.toUpperCase(uncheckedInput.charAt(0)); //set the battlechoice variable to the Char at 0, and capitalise it.
- } else {
- //FDNAFUIGRUI THIS LINE WAS THE PROBLEM >> //battle(); re-call the battle method to ask question again because user entered nothing
- }
- int hit;
- int runChance;
- switch (battleChoice){
- case 'A':
- hit = (int)(Math.random() * 6 *.5 * level + 2); //your damage
- enemyHealth -= hit;
- if(hit >= enemyHealth) { //hit high enough to kill the enemy
- System.out.println("You hit " + hit + " damage and kill the enemy!");
- JOptionPane.showMessageDialog(null, "You hit " + hit + " damage and kill the enemy!");
- gold += (int)((Math.random() * 10) + 1); //gives player gold between 1-10.
- exp += (int) ((Math.random() * 10) + 1); //gives player exp;
- System.out.println("You now have " +exp+ " exp and " +gold+ " gold.");
- JOptionPane.showMessageDialog(null, "You now have " +exp+ " exp and " +gold+ " gold.");
- int potionDrop = (int) (Math.random() * 10);
- if (potionDrop == 0) {
- potion ++;
- System.out.println("You have gained a potion! You now have " + potion + " potions.");
- JOptionPane.showMessageDialog(null, "You have gained a potion! You now have " + potion + " potions.");
- }
- level();
- break;
- } else {
- System.out.println("you hit " + hit + " damage. The enemy now has " + enemyHealth + " health.");
- JOptionPane.showMessageDialog(null, "You hit " + hit + " damage. The enemy now has " + enemyHealth + " health.");
- hit = (int)((Math.random() * 8) + 2);
- health -= hit; //enemy hits you back
- System.out.println("The enemy retaliates with " + hit + " damage. Your health is now " + health + ".");
- JOptionPane.showMessageDialog(null, "The enemy retaliates with a " + hit + ". Your health is now " + health + ".");
- battle(); //battle isn't over cos both alive so make it ask you what to do next in battle.
- break;
- }
- case 'D':
- hit = (int)((Math.random() * 5)); //enemy hits half of normal because defending mode.
- health -= hit;
- System.out.println("You took " +hit+ " damage! Your health is now " + health);
- JOptionPane.showMessageDialog(null, "You took " +hit+ " damage! Your health is now " + health);
- battle(); // battle continues
- break;
- case 'P':
- if (potion > 0) {
- potion--; //remove 1 potion
- amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
- health += amount; //heal from potion
- if (health > 100) {
- health = 100;
- }
- System.out.println("You healed " + amount + " health! you now have " + health + " health.");
- System.out.println("You have " + potion + " potions left.");
- JOptionPane.showMessageDialog(null, "You drink a potion.");
- JOptionPane.showMessageDialog(null, "You healed " + amount + " health! you now have " + health + " health.");
- JOptionPane.showMessageDialog(null, "You have " + potion + " potions left.");
- hit = (int)((Math.random() * 6 + 2)); //enemy hit on you after potion
- health -= hit; //enemy hits you back
- System.out.println("enemy hits you with a " + hit + ". Your health is now " + health);
- JOptionPane.showMessageDialog(null, "enemy hits you with a " + hit + ". Your health is now " + health);
- } else {
- System.out.println("You have 0 potions to drink.");
- JOptionPane.showMessageDialog(null, "You have no potions to drink.");
- }
- battle();
- break;
- case 'R':
- runChance = (int) (Math.random() * 2); //50/50 chance of escape
- if (runChance == 0) {
- //successful escape, battle over and continues adventure
- System.out.println("You successfully escape!");
- JOptionPane.showMessageDialog(null, "You successfully escape!");
- break;
- } else {
- //unsuccessful escape
- hit = (int)((Math.random() * 11) + 6); //enemy hit between 10-20
- health -= hit; //so you get hit
- System.out.println("You try to escape but are too slow and the enemy hits you for a " + hit +" . You now have " + health + " health.");
- JOptionPane.showMessageDialog(null, "You try to escape but are too slow and the enemy hits you for a " + hit +" . You now have " + health + " health.");
- battle(); //battle continues
- break;
- }
- default:
- battle(); //user didn't enter any of the battle options, so we ask them again
- break;
- }
- } else if(health < 1) {
- die = true; //dead
- if (health < 0) {
- health = 0;
- }
- System.out.println("Oh dear, you are dead!");
- JOptionPane.showMessageDialog(null, "Oh dear, you are dead!");
- System.out.println("You die with " +exp+ " exp and " +gold+ " gold.");
- JOptionPane.showMessageDialog(null, "You die with " +exp+ " exp and " +gold+ " gold.");
- System.out.println("You die at " + amountNorth +"and " +amountEast+ " East.");
- JOptionPane.showMessageDialog(null, "You die at North " + amountNorth + " and East " + amountEast);
- System.exit(0);
- }
- }
- public static void level ()
- {
- switch (level)
- {
- case 0:
- if(exp<20)
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- case 1:
- if(exp>=20)
- level++;
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- case 2:
- if(exp>=40)
- level++;
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- case 3:
- if(exp>=80)
- level++;
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- case 4:
- if(exp>=160)
- level++;
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- case 5:
- if(exp>=320)
- level++;
- JOptionPane.showMessageDialog(null, "You are level " + level + ".");
- System.out.println("Level "+ level+ ".");
- break;
- default:
- break;
- }
- }
- public static void leave(){
- uncheckedInput = JOptionPane.showInputDialog("Do you leave, (Y)es or (N)o?");
- if (!uncheckedInput.equals("")) {
- userInput = Character.toUpperCase(uncheckedInput.charAt(0));
- }
- switch(userInput) {
- case 'Y':
- leave = true;
- System.out.println("You leave with " +exp+ " exp and " +gold+ " gold.");
- JOptionPane.showMessageDialog(null, "You leave with " +exp+ " exp and " +gold+ " gold.");
- System.out.println("Your exit was at " + amountNorth + amountEast);
- JOptionPane.showMessageDialog(null, "Your exit was at at North " + amountNorth + " and East " + amountEast);
- System.exit(0);
- case 'N':
- leave = false;
- break;
- default:
- leave();
- break;
- }
- }
- public static void shop(){
- @SuppressWarnings("unused")
- boolean potionBuy;
- uncheckedInput = JOptionPane.showInputDialog("Do you buy a potion, (Y)es or (N)o?");
- if (!uncheckedInput.equals("")) {
- userInput = Character.toUpperCase(uncheckedInput.charAt(0));
- }
- switch(userInput){
- case 'Y':
- potionBuy = true;
- gold -= 10;
- potion++;
- System.out.println("You buy a potion.");
- JOptionPane.showMessageDialog(null, "You buy a potion; you now have "+potion+ " potions and "+gold+ " gold left.");
- case 'N':
- potionBuy = false;
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement