Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- // need to fix, potion is going double when walking, sometimes printing you went x direction twice
- 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;
- 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 25% chance of spawning and 10% chance of dropping a health potion and give 1-10 exp and gold.");
- while(health > 0){
- do{
- uncheckedInput = (JOptionPane.showInputDialog("Which way do you go? " + "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("health = "+health);
- 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
- JOptionPane.showMessageDialog(null, "You have encountered an enemy!");
- System.out.println("Battle!");
- //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
- }
- {
- playerChosenDirection = Move (userInput);
- chestAt = (int)Math.floor(Math.random() * 5); //generates the chest at one of the random directions therefore 20% chance at encountering it.
- if (playerChosenDirection == chestAt) {
- JOptionPane.showMessageDialog(null, "You have found a chest!");
- JOptionPane.showInputDialog(null, "It looks like it could be trapped, do you want to (o)pen it or (l)eave it?");
- //if(chest == false){
- //////where i ended need to make it continue
- //else if(chest == true){
- //////25% chance nothing 25%chance trap 25% gold 25% potion math*4 and cases
- }
- }
- if (monsterAt == chestAt) {
- chestAt += 1;
- }
- }
- }
- public void chest(){
- uncheckedInput = JOptionPane.showInputDialog("What do you do?");
- if (!uncheckedInput.equals("")) {
- userInput = Character.toUpperCase(uncheckedInput.charAt(0));
- }
- switch(userInput) {
- case 'O':
- chest = true;
- break;
- case 'L':
- chest = false;
- break;
- default:
- chest();
- 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.");
- return 0;
- case 'E':
- System.out.println("You went east.");
- return 1;
- case 'S':
- System.out.println("You went south.");
- return 2;
- case 'W':
- System.out.println("You went west.");
- return 3;
- 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 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.");
- }
- 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 {
- 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); //generate random hit between 0 and 5.
- enemyHealth -= hit;
- if(hit >= enemyHealth) { //hit high enough to kill the enemy
- System.out.println("You hit " + hit + " damage and kill the enemy!!");
- gold += (int)((Math.random() * 9) + 1); //gives player gold between 1-9.
- exp += (int) ((Math.random() * 10) + 1); //gives player exp;
- System.out.println("You now have " +exp+ " exp and " +gold+ " gold.");
- int potionDrop = (int) (Math.random() * 10);
- if (potionDrop == 0) {
- potion ++;
- }
- } else {
- System.out.println("you hit " + hit + " damage. The enemy now has " + enemyHealth + ".");
- hit = (int)((Math.random() * 7) + 5); //enemy hit on you between 10-20
- health -= hit; //enemy hits you back
- System.out.println("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() * 3) + 2); //enemy hits you between 5-10, half of normal because defending mode.
- health -= hit;
- System.out.println("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.");
- hit = (int)((Math.random() * 11) + 10); //enemy hit on you between 10-20
- health -= hit; //enemy hits you back
- System.out.println("enemy hits you with a " + hit + ". Your health is now " + health);
- } else {
- System.out.println("You have 0 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!");
- 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.");
- battle(); //battle continues
- }
- default:
- battle(); //user didn't enter any of the battle options, so we ask them again
- break;
- }
- } else {
- die = true; //dead
- if (health < 0) {
- health = 0;
- }
- System.out.println("Oh dear, you are dead!");
- System.out.println("You die with " +exp+ " exp and " +gold+ " gold.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement