Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 29.01 KB | None | 0 0
  1.    
  2.  
  3.     import javax.swing.*;
  4.     //going to add a shop to spend gold
  5.  
  6.      
  7.      
  8.     public class lel {
  9.            
  10.             static int health = 100;
  11.             static int gold = 0;
  12.             static int exp = 0;
  13.             static int playerChosenDirection;
  14.             static int battleChoice;
  15.             static int monsterAt;
  16.             static int chestAt;
  17.             static int enemyHealth;
  18.             static int potion = 3;
  19.             static int amount;
  20.             static char userInput = 0;
  21.             static String uncheckedInput;
  22.             static boolean die = false;
  23.             static boolean chest;
  24.             static int chestLoot;
  25.             static int chestGold;
  26.             static int chestPotion;
  27.             static int chestTrap;
  28.             static int chestNothing;
  29.             static int level = 1;
  30.             static int amountNorth = 0;
  31.             static int amountEast = 0;
  32.             static int amountStep = 0;
  33.             static boolean leave;
  34.  
  35.             public static void main(String[] args) {
  36.                    
  37.      
  38.                     JOptionPane.showMessageDialog(null, "WELCOME.");
  39.                     JOptionPane.showMessageDialog(null, "You are in a dungeon and slowly bleeding out every step. You have 3 health potions.");
  40.                     JOptionPane.showMessageDialog(null, "You have the choice of going (N)orth, (E)ast, (S)outh, (W)est or (P)otion");
  41.                     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.");
  42.                
  43.                    while(health > 0){
  44.                                    
  45.      
  46.                             do{
  47.                                    
  48.                                     uncheckedInput = (JOptionPane.showInputDialog("Which way do you go, (N) (E) (S) (W) or (P)? " + "You have "+ health + " health."));
  49.                                     if(!uncheckedInput.equals("")) {
  50.                                             userInput = Character.toUpperCase(uncheckedInput.charAt(0));
  51.                                     }
  52.                             }
  53.             while(userInput != 'N' && userInput != 'S'&& userInput != 'E'&& userInput != 'W' && userInput != 'P');
  54.                             health--;
  55.                     System.out.println("You have "+health+ " left.");
  56.            
  57.                     playerChosenDirection = Move (userInput);
  58.                     monsterAt = (int)Math.floor(Math.random() * 4); //generates the monster at one of the random directions therefore 25% chance at encountering it.
  59.            
  60.                     if (playerChosenDirection == monsterAt) {
  61.                             //battle
  62.                          System.out.println("Battle!");
  63.                             JOptionPane.showMessageDialog(null, "You have encountered an enemy!");
  64.                            
  65.                             //JOptionPane.showMessageDialog(null, "It has " +enemyHealth+ " health."); // again, i need to make enemyhealth usable from multiple parts of code
  66.                             enemyHealth = (int)((Math.random()*10) + 10); //generate monster spawn
  67.                             battle(); //start battle
  68.                     }
  69.                     else
  70.                     {
  71.                    
  72.                            // WOW THERE WAS A LINE HERE,  playerChosenDirection = Move (userInput); THAT I DIDNT NEED THAT WAS THE DOUBLE PRINTING BUG WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
  73.                            
  74.                            chestAt = (int)Math.floor(Math.random() * 8); //generates the chest at one of the random directions therefore 20% chance at encountering it.
  75.                    
  76.                             if (playerChosenDirection == chestAt) {
  77.                             System.out.println("You found a chest.");
  78.                             JOptionPane.showMessageDialog(null, "You have found a chest!");                            
  79.                             JOptionPane.showMessageDialog(null, "It looks like it could be trapped, do you want to (o)pen it or (l)eave it?");
  80.                             chest();
  81.                             }
  82.                             else if(amountStep>25){
  83.                                 int exit = (int)Math.floor(Math.random() * 16); //exit 1/16 chance after 25 moves
  84.                                 if (playerChosenDirection == exit)        {  
  85.                                 System.out.println("You have found an exit!"); 
  86.                                 JOptionPane.showMessageDialog(null, "You have found an exit!");
  87.                                 JOptionPane.showMessageDialog(null, "Do you want to leave now?");
  88.                                 leave();
  89.                             }
  90.                             else{
  91.                            
  92.  
  93.                             }
  94.                     }
  95.                             }
  96.                     }
  97.             }
  98.                    
  99.            
  100.             public static void chest(){
  101.                    
  102.                            
  103.                 uncheckedInput = JOptionPane.showInputDialog("What do you do?");
  104.                 if (!uncheckedInput.equals("")) {
  105.                         userInput = Character.toUpperCase(uncheckedInput.charAt(0));
  106.                 } else{
  107.                
  108.                 }
  109.                
  110.                 switch(userInput) {
  111.                         case 'O':
  112.                                 chest = true;
  113.                                 break;
  114.                         case 'L':
  115.                                 chest = false;
  116.                                 break;
  117.                         default:
  118.                             //chest(); fixed by removing
  119.                                 break;
  120.                                 }
  121.                
  122.                     if(chest == false){
  123.                    
  124.                     }
  125.                
  126.                     else if(chest == true){
  127.                            
  128.                    
  129.                     int loot = (int) (Math.random() * 4);
  130.                            
  131.                             switch(loot) {                      
  132.                          
  133.                             case 1:
  134.                             if(loot == 0){
  135.                                     gold +=  (int)(Math.random()*6 + 5);
  136.                                     System.out.println("You found some gold! You now have " + gold + " gold.");
  137.                                     JOptionPane.showMessageDialog(null, "You found some gold! You now have " + gold + " gold.");
  138.                                    
  139.                             break;
  140.                             }
  141.                             case 2:
  142.                             if (loot == 1){
  143.                                     potion++;
  144.                                     System.out.println("You found a potion!");
  145.                                     JOptionPane.showMessageDialog(null, "You found a potion! You now have " + potion + " potions.");
  146.                                                  
  147.                             break;  }
  148.                            
  149.                             case 3:
  150.                         if (loot == 2){
  151.                             health -=  (int)(Math.random()*16 + 10);
  152.                             System.out.println("Oh no! The chest was trapped. You took some damage...");
  153.                             JOptionPane.showMessageDialog(null, "Oh no! The chest was trapped. You now have " + health + " health.");
  154.                            
  155.                         break;
  156.                         }
  157.                        
  158.                             case 4:                  
  159.                       if (loot ==3) {
  160.                             System.out.println("No loot from that chest.");
  161.                         JOptionPane.showMessageDialog(null, "You found nothing...");
  162.      
  163.                         break;  
  164.                         }    
  165.                            
  166.                             case 5:                  
  167.                                 if (loot == 4 ) {
  168.                                     System.out.println("No loot from that chest.");
  169.                                   JOptionPane.showMessageDialog(null, "You found nothing...");
  170.                
  171.                                   break;  
  172.                                   }    
  173.                             }
  174.                             }
  175.                            
  176.                            
  177.                             }
  178.            
  179.                    
  180.              
  181.                    
  182.          
  183.     public static int Move (char direction)  // receives user input and gives you an integer coord in exchange
  184.      
  185.     {
  186.      
  187.     switch (direction){
  188.             case 'N':
  189.                     System.out.println("You went north.");  
  190.                     amountStep++;
  191.                     amountNorth++;
  192.                     if(amountNorth>0 && amountEast>0){
  193.                     System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  194.                     } else if(amountNorth<0 && amountEast<0) {                    
  195.                     System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  196.                     }
  197.                     else if(amountNorth>0 && amountEast<0){
  198.                         System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);  
  199.                     }
  200.                     else if(amountNorth<0 && amountEast>0){
  201.                         System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);  
  202.                     }
  203.                     else if(amountNorth==0 && amountEast==0){
  204.                         System.out.println("You are in between North and South and East and West");    
  205.                     }
  206.                     else if(amountNorth==0 && amountEast<0){
  207.                     System.out.println("You are inbetween North and South and at West " + amountEast*-1);  
  208.                     }
  209.                     else if(amountNorth<0 && amountEast==0){
  210.                         System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  211.                         }
  212.                     else if(amountNorth==0 && amountEast>0){
  213.                         System.out.println("You are inbetween North and South and at East " + amountEast); 
  214.                         }
  215.                     else if(amountNorth>0 && amountEast==0){
  216.                         System.out.println("You are at North " + amountNorth + " and in between East and West");   
  217.                         }
  218.                     return 0;
  219.             case 'E':
  220.                     System.out.println("You went east.");
  221.                     amountStep++;
  222.                     amountEast++;                    
  223.                    
  224.                           if(amountNorth>0 && amountEast>0){
  225.                               System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  226.                             } else if(amountNorth<0 && amountEast<0) {                    
  227.                             System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  228.                             }
  229.                             else if(amountNorth>0 && amountEast<0){
  230.                                 System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);  
  231.                             }
  232.                             else if(amountNorth<0 && amountEast>0){
  233.                                 System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);  
  234.                             }
  235.                             else if(amountNorth==0 && amountEast==0){
  236.                                 System.out.println("You are in between North and South and East and West");    
  237.                             }
  238.                             else if(amountNorth==0 && amountEast<0){
  239.                             System.out.println("You are inbetween North and South and at West " + amountEast*-1);  
  240.                             }
  241.                             else if(amountNorth<0 && amountEast==0){
  242.                                 System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  243.                                 }
  244.                             else if(amountNorth==0 && amountEast>0){
  245.                                 System.out.println("You are inbetween North and South and at East " + amountEast); 
  246.                                 }
  247.                             else if(amountNorth>0 && amountEast==0){
  248.                                 System.out.println("You are at North " + amountNorth + " and in between East and West");   
  249.                                 }
  250.                    
  251.                     return 1;                  
  252.             case 'S':
  253.                     System.out.println("You went south.");
  254.                     amountStep++;
  255.                     amountNorth--;
  256.                     if(amountNorth>0 && amountEast>0){
  257.                         System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  258.                         } else if(amountNorth<0 && amountEast<0) {                    
  259.                         System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  260.                         }
  261.                         else if(amountNorth>0 && amountEast<0){
  262.                             System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);  
  263.                         }
  264.                         else if(amountNorth<0 && amountEast>0){
  265.                             System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);  
  266.                         }
  267.                         else if(amountNorth==0 && amountEast==0){
  268.                             System.out.println("You are in between North and South and East and West");    
  269.                         }
  270.                         else if(amountNorth==0 && amountEast<0){
  271.                         System.out.println("You are inbetween North and South and at West " + amountEast*-1);  
  272.                         }
  273.                         else if(amountNorth<0 && amountEast==0){
  274.                             System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  275.                             }
  276.                         else if(amountNorth==0 && amountEast>0){
  277.                             System.out.println("You are inbetween North and South and at East " + amountEast); 
  278.                             }
  279.                         else if(amountNorth>0 && amountEast==0){
  280.                             System.out.println("You are at North " + amountNorth + " and in between East and West");   
  281.                             }
  282.                     return 2;
  283.             case 'W':
  284.                     System.out.println("You went west.");  
  285.                     amountStep++;
  286.                     amountEast--;
  287.                     if(amountNorth>0 && amountEast>0){
  288.                         System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  289.                         } else if(amountNorth<0 && amountEast<0) {                    
  290.                         System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  291.                         }
  292.                         else if(amountNorth>0 && amountEast<0){
  293.                             System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);  
  294.                         }
  295.                         else if(amountNorth<0 && amountEast>0){
  296.                             System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);  
  297.                         }
  298.                         else if(amountNorth==0 && amountEast==0){
  299.                             System.out.println("You are in between North and South and East and West");    
  300.                         }
  301.                         else if(amountNorth==0 && amountEast<0){
  302.                         System.out.println("You are inbetween North and South and at West " + amountEast*-1);  
  303.                         }
  304.                         else if(amountNorth<0 && amountEast==0){
  305.                             System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  306.                             }
  307.                         else if(amountNorth==0 && amountEast>0){
  308.                             System.out.println("You are inbetween North and South and at East " + amountEast); 
  309.                             }
  310.                         else if(amountNorth>0 && amountEast==0){
  311.                             System.out.println("You are at North " + amountNorth + " and in between East and West");   
  312.                             }
  313.                     return 3;
  314.             case 'P':
  315.                 if (potion > 0) {
  316.                     potion--; //remove 1 potion
  317.                     health++;
  318.                     amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
  319.                
  320.                     health += amount; //heal from potion
  321.                     if (health > 100) {
  322.                         health = 100;
  323.                    
  324.                     System.out.println("You drink a potion.");
  325.                     System.out.println("You healed " + amount + " health! you now have " + health + " health.");
  326.                     System.out.println("You have " + potion + " potions left.");
  327.                     }
  328.                 } else {
  329.                     System.out.println("You have 0 potions to drink.");
  330.                     JOptionPane.showMessageDialog(null, "You have no potions to drink.");
  331.                 }
  332.            
  333.                 return 5;
  334.                
  335.                        
  336.             default:
  337.                
  338.                     return 6;
  339.                    
  340.                    
  341.     }
  342.      
  343.      
  344.     }
  345.      
  346.     public static void battle ()
  347.            
  348.     {
  349.             if (health > 0) { //check you're alive.
  350.                     uncheckedInput = JOptionPane.showInputDialog(null, "What do you do, (A)ttack (D)efend (P)otion or (R)un?"); //take input string
  351.                     if (!uncheckedInput.equals("")) { //check there actually is a character at (0) to stop game crashing
  352.                             battleChoice = Character.toUpperCase(uncheckedInput.charAt(0)); //set the battlechoice variable to the Char at 0, and capitalise it.
  353.                     } else {
  354.                     //FDNAFUIGRUI THIS LINE WAS THE PROBLEM >>  //battle(); re-call the battle method to ask question again because user entered nothing
  355.                     }
  356.  
  357.                  
  358.                     int hit;        
  359.                     int runChance;
  360.                    
  361.                     switch (battleChoice){
  362.                             case 'A':
  363.                                     hit = (int)(Math.random() * 6 *.5 * level + 2); //your damage
  364.                                     enemyHealth -= hit;
  365.                                    
  366.                                     if(hit >= enemyHealth) { //hit high enough to kill the enemy
  367.                                         System.out.println("You hit " + hit + " damage and kill the enemy!");
  368.                                                 JOptionPane.showMessageDialog(null, "You hit " + hit + " damage and kill the enemy!");
  369.                                              
  370.                                             gold += (int)((Math.random() * 10) + 1); //gives player gold between 1-10.
  371.                                             exp += (int) ((Math.random() * 10) + 1); //gives player exp;
  372.                                             System.out.println("You now have " +exp+ " exp and " +gold+ " gold.");
  373.                                             JOptionPane.showMessageDialog(null, "You now have " +exp+ " exp and " +gold+ " gold.");
  374.                                             int potionDrop = (int) (Math.random() * 10);
  375.                                             if (potionDrop == 0) {
  376.                                                 potion ++;
  377.                                                 System.out.println("You have gained a potion! You now have " + potion + " potions.");
  378.                                                 JOptionPane.showMessageDialog(null, "You have gained a potion! You now have " + potion + " potions.");        
  379.                                             }
  380.                                             level();
  381.                                             break;
  382.                                     } else {
  383.                                         System.out.println("you hit " + hit + " damage. The enemy now has " + enemyHealth + " health.");
  384.                                              JOptionPane.showMessageDialog(null, "You hit " + hit + " damage. The enemy now has " + enemyHealth + " health.");    
  385.                                            
  386.                                             hit = (int)((Math.random() * 8) + 2);
  387.                                             health -= hit; //enemy hits you back
  388.                                             System.out.println("The enemy retaliates with " + hit + " damage. Your health is now " + health + ".");
  389.                                             JOptionPane.showMessageDialog(null, "The enemy retaliates with a " + hit + ". Your health is now " + health + ".");
  390.                                            
  391.                                             battle(); //battle isn't over cos both alive so make it ask you what to do next in battle.
  392.                                            break;
  393.                                     }
  394.                                    
  395.                             case 'D':
  396.                                     hit = (int)((Math.random() * 5)); //enemy hits half of normal because defending mode.
  397.                                     health -= hit;
  398.                                     System.out.println("You took " +hit+ " damage! Your health is now " + health);
  399.                                     JOptionPane.showMessageDialog(null, "You took " +hit+ " damage! Your health is now " + health);
  400.                                    
  401.                                     battle(); // battle continues
  402.                                     break;
  403.                             case 'P':
  404.                                     if (potion > 0) {
  405.                                             potion--; //remove 1 potion
  406.                                             amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
  407.                                        
  408.                                             health += amount; //heal from potion
  409.                                             if (health > 100) {
  410.                                                 health = 100;
  411.                                             }
  412.                                             System.out.println("You healed " + amount + " health! you now have " + health + " health.");
  413.                                             System.out.println("You have " + potion + " potions left.");
  414.                                             JOptionPane.showMessageDialog(null, "You drink a potion.");
  415.                                             JOptionPane.showMessageDialog(null, "You healed " + amount + " health! you now have " + health + " health.");
  416.                                             JOptionPane.showMessageDialog(null, "You have " + potion + " potions left.");
  417.                                            
  418.                                             hit = (int)((Math.random() * 6 + 2)); //enemy hit on you after potion
  419.                                             health -= hit; //enemy hits you back
  420.                                             System.out.println("enemy hits you with a " + hit + ". Your health is now " + health);
  421.                                             JOptionPane.showMessageDialog(null, "enemy hits you with a " + hit + ". Your health is now " + health);
  422.                                            
  423.                                            
  424.                                     } else {
  425.                                         System.out.println("You have 0 potions to drink.");
  426.                                             JOptionPane.showMessageDialog(null, "You have no potions to drink.");
  427.                                            
  428.                                     }
  429.                                     battle();
  430.                                     break;
  431.                             case 'R':
  432.                                     runChance = (int) (Math.random() * 2); //50/50 chance of escape
  433.                                     if (runChance == 0) {
  434.                                             //successful escape, battle over and continues adventure
  435.                                         System.out.println("You successfully escape!");
  436.                                             JOptionPane.showMessageDialog(null, "You successfully escape!");
  437.                                            
  438.                                             break;
  439.                                     } else {
  440.                                             //unsuccessful escape
  441.                                             hit = (int)((Math.random() * 11) + 6); //enemy hit between 10-20
  442.                                             health -= hit; //so you get hit
  443.                                             System.out.println("You try to escape but are too slow and the enemy hits you for a " + hit +" . You now have " + health + " health.");
  444.                                             JOptionPane.showMessageDialog(null, "You try to escape but are too slow and the enemy hits you for a " + hit +" . You now have " + health + " health.");
  445.                                            
  446.                                             battle(); //battle continues
  447.                                             break;
  448.                                     }
  449.                             default:
  450.                                     battle(); //user didn't enter any of the battle options, so we ask them again
  451.                                     break;
  452.                     }
  453.                    
  454.                            
  455.                            
  456.             } else if(health < 1) {
  457.                
  458.                     die = true; //dead
  459.                     if (health < 0) {
  460.                         health = 0;
  461.                     }
  462.                     System.out.println("Oh dear, you are dead!");
  463.                     JOptionPane.showMessageDialog(null, "Oh dear, you are dead!");
  464.                     System.out.println("You die with " +exp+ " exp and " +gold+ " gold.");
  465.                     JOptionPane.showMessageDialog(null, "You die with " +exp+ " exp and " +gold+ " gold.");
  466.                     System.out.println("You die at " + amountNorth + amountEast);
  467.                     JOptionPane.showMessageDialog(null, "You die at North " + amountNorth + " and East " + amountEast);
  468.                     System.exit(0);
  469.                    
  470.             }
  471.     }
  472.    
  473.     public static void level ()
  474.     {
  475.                     switch (level)
  476.                     {
  477.      
  478.      case 0:
  479.             if(exp<20)                
  480.             JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  481.             System.out.println("Level "+ level+ ".");  
  482.             break;                    
  483.             case 1:
  484.                     if(exp>=20)                
  485.                     level++;
  486.                     JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  487.                     System.out.println("Level "+ level+ ".");  
  488.                     break;                  
  489.                     case 2:
  490.                         if(exp>=40)                
  491.                         level++;
  492.                         JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  493.                         System.out.println("Level "+ level+ ".");  
  494.                         break;
  495.                         case 3:
  496.                             if(exp>=80)                
  497.                             level++;
  498.                             JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  499.                             System.out.println("Level "+ level+ ".");  
  500.                             break;
  501.                             case 4:
  502.                                 if(exp>=160)              
  503.                                 level++;
  504.                                 JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  505.                                 System.out.println("Level "+ level+ ".");  
  506.                                 break;
  507.                                 case 5:
  508.                                     if(exp>=320)                  
  509.                                     level++;
  510.                                     JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  511.                                     System.out.println("Level "+ level+ ".");      
  512.                                     break;
  513.              
  514.                                 default:
  515.                                     break;
  516.             }
  517.     }
  518.    
  519.    
  520.     public static void leave(){
  521.        
  522.        
  523.         uncheckedInput = JOptionPane.showInputDialog("Do you leave, (Y)es or (N)o?");
  524.         if (!uncheckedInput.equals("")) {
  525.                 userInput = Character.toUpperCase(uncheckedInput.charAt(0));
  526.         }
  527.        
  528.         switch(userInput) {
  529.         case 'Y':
  530.             leave = true;
  531.             System.out.println("You leave with " +exp+ " exp and " +gold+ " gold.");
  532.             JOptionPane.showMessageDialog(null, "You leave with " +exp+ " exp and " +gold+ " gold.");
  533.             System.out.println("Your exit was at " + amountNorth + amountEast);
  534.             JOptionPane.showMessageDialog(null, "Your exit was at at North " + amountNorth + " and East " + amountEast);
  535.             System.exit(0);
  536.     case 'N':
  537.             leave = false;
  538.             break;
  539.     default:
  540.             leave();
  541.             break;
  542.             }
  543.  
  544. }
  545.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement