Advertisement
Guest User

Untitled

a guest
Nov 18th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1.  
  2.  
  3. import javax.swing.*;
  4. // need to fix, potion is going double when walking, sometimes printing you went x direction twice
  5.  
  6.  
  7. public class lel {
  8.  
  9. static int health = 100;
  10. static int gold = 0;
  11. static int exp = 0;
  12. static int playerChosenDirection;
  13. static int battleChoice;
  14. static int monsterAt;
  15. static int chestAt;
  16. static int enemyHealth;
  17. static int potion = 3;
  18. static int amount;
  19. static char userInput = 0;
  20. static String uncheckedInput;
  21. static boolean die = false;
  22. static boolean chest;
  23.  
  24. public static void main(String[] args) {
  25.  
  26.  
  27. JOptionPane.showMessageDialog(null, "WELCOME.");
  28. JOptionPane.showMessageDialog(null, "You are in a dungeon and slowly bleeding out every step. You have 3 health potions.");
  29. JOptionPane.showMessageDialog(null, "You have the choice of going (N)orth, (E)ast, (S)outh, (W)est or (P)otion");
  30. 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.");
  31.  
  32. while(health > 0){
  33.  
  34.  
  35. do{
  36.  
  37. uncheckedInput = (JOptionPane.showInputDialog("Which way do you go? " + "You have "+ health + " health."));
  38. if(!uncheckedInput.equals("")) {
  39. userInput = Character.toUpperCase(uncheckedInput.charAt(0));
  40. }
  41. }
  42. while(userInput != 'N' && userInput != 'S'&& userInput != 'E'&& userInput != 'W' && userInput != 'P');
  43. health--;
  44. System.out.println("health = "+health);
  45.  
  46. playerChosenDirection = Move (userInput);
  47. monsterAt = (int)Math.floor(Math.random() * 4); //generates the monster at one of the random directions therefore 25% chance at encountering it.
  48.  
  49. if (playerChosenDirection == monsterAt) {
  50. //battle
  51. JOptionPane.showMessageDialog(null, "You have encountered an enemy!");
  52. System.out.println("Battle!");
  53. //JOptionPane.showMessageDialog(null, "It has " +enemyHealth+ " health."); // again, i need to make enemyhealth usable from multiple parts of code
  54. enemyHealth = (int)((Math.random()*10) + 10); //generate monster spawn
  55. battle(); //start battle
  56. }
  57. {
  58.  
  59. playerChosenDirection = Move (userInput);
  60. chestAt = (int)Math.floor(Math.random() * 5); //generates the chest at one of the random directions therefore 20% chance at encountering it.
  61.  
  62. if (playerChosenDirection == chestAt) {
  63. JOptionPane.showMessageDialog(null, "You have found a chest!");
  64. JOptionPane.showInputDialog(null, "It looks like it could be trapped, do you want to (o)pen it or (l)eave it?");
  65. //if(chest == false){
  66. //////where i ended need to make it continue
  67. //else if(chest == true){
  68. //////25% chance nothing 25%chance trap 25% gold 25% potion math*4 and cases
  69.  
  70. }
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77. if (monsterAt == chestAt) {
  78. chestAt += 1;
  79. }
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. public void chest(){
  90.  
  91.  
  92. uncheckedInput = JOptionPane.showInputDialog("What do you do?");
  93. if (!uncheckedInput.equals("")) {
  94. userInput = Character.toUpperCase(uncheckedInput.charAt(0));
  95. }
  96.  
  97. switch(userInput) {
  98. case 'O':
  99. chest = true;
  100. break;
  101. case 'L':
  102. chest = false;
  103. break;
  104. default:
  105. chest();
  106. break;}
  107. }
  108.  
  109.  
  110.  
  111. public static int Move (char direction) // receives user input and gives you an integer coord in exchange
  112.  
  113. {
  114.  
  115. switch (direction){
  116. case 'N':
  117. System.out.println("You went north.");
  118. return 0;
  119. case 'E':
  120. System.out.println("You went east.");
  121. return 1;
  122. case 'S':
  123. System.out.println("You went south.");
  124. return 2;
  125. case 'W':
  126. System.out.println("You went west.");
  127. return 3;
  128. case 'P':
  129. if (potion > 0) {
  130. potion--; //remove 1 potion
  131. amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
  132.  
  133. health += amount; //heal from potion
  134. if (health > 100) {
  135. health = 100;
  136. }
  137. System.out.println("You drink a potion.");
  138. System.out.println("You healed " + amount + " health! you now have " + health + " health.");
  139. System.out.println("You have " + potion + " potions left.");
  140.  
  141. } else {
  142. System.out.println("You have 0 potions to drink.");
  143. }
  144. return 5;
  145.  
  146.  
  147. default:
  148. return 6;
  149. }
  150.  
  151.  
  152. }
  153.  
  154. public static void battle ()
  155.  
  156. {
  157. if (health > 0) { //check you're alive.
  158. uncheckedInput = JOptionPane.showInputDialog(null, "What do you do, (A)ttack (D)efend (P)otion or (R)un?"); //take input string
  159. if (!uncheckedInput.equals("")) { //check there actually is a character at (0) to stop game crashing
  160. battleChoice = Character.toUpperCase(uncheckedInput.charAt(0)); //set the battlechoice variable to the Char at 0, and capitalise it.
  161. } else {
  162. battle(); //re-call the battle method to ask question again because user entered nothing
  163. }
  164.  
  165.  
  166. int hit;
  167. int runChance;
  168.  
  169. switch (battleChoice){
  170. case 'A':
  171. hit = (int)(Math.random() * 6); //generate random hit between 0 and 5.
  172. enemyHealth -= hit;
  173.  
  174. if(hit >= enemyHealth) { //hit high enough to kill the enemy
  175. System.out.println("You hit " + hit + " damage and kill the enemy!!");
  176. gold += (int)((Math.random() * 9) + 1); //gives player gold between 1-9.
  177. exp += (int) ((Math.random() * 10) + 1); //gives player exp;
  178. System.out.println("You now have " +exp+ " exp and " +gold+ " gold.");
  179. int potionDrop = (int) (Math.random() * 10);
  180. if (potionDrop == 0) {
  181. potion ++;
  182. }
  183. } else {
  184. System.out.println("you hit " + hit + " damage. The enemy now has " + enemyHealth + ".");
  185. hit = (int)((Math.random() * 7) + 5); //enemy hit on you between 10-20
  186. health -= hit; //enemy hits you back
  187. System.out.println("enemy retaliates with a " + hit + ". Your health is now " + health);
  188. battle(); //battle isn't over cos both alive so make it ask you what to do next in battle.
  189.  
  190. }
  191. break;
  192. case 'D':
  193. hit = (int)((Math.random() * 3) + 2); //enemy hits you between 5-10, half of normal because defending mode.
  194. health -= hit;
  195. System.out.println("You took " +hit+ " damage! Your health is now " + health);
  196. battle(); // battle continues
  197. break;
  198. case 'P':
  199. if (potion > 0) {
  200. potion--; //remove 1 potion
  201. amount = (int)((Math.random() * 11) + 10); //generate random heal amount between 10-20
  202.  
  203. health += amount; //heal from potion
  204. if (health > 100) {
  205. health = 100;
  206. }
  207. System.out.println("You healed " + amount + " health! you now have " + health + " health.");
  208. System.out.println("You have " + potion + " potions left.");
  209.  
  210. hit = (int)((Math.random() * 11) + 10); //enemy hit on you between 10-20
  211. health -= hit; //enemy hits you back
  212. System.out.println("enemy hits you with a " + hit + ". Your health is now " + health);
  213.  
  214. } else {
  215. System.out.println("You have 0 potions to drink.");
  216. }
  217. battle();
  218. break;
  219. case 'R':
  220. runChance = (int) (Math.random() * 2); //50/50 chance of escape
  221. if (runChance == 0) {
  222. //successful escape, battle over and continues adventure
  223. System.out.println("You successfully escape!");
  224. break;
  225. } else {
  226. //unsuccessful escape
  227. hit = (int)((Math.random() * 11) + 6); //enemy hit between 10-20
  228. health -= hit; //so you get hit
  229. System.out.println("You try to escape but are too slow and the enemy hits you for a " + hit +" . You now have " + health + "health.");
  230. battle(); //battle continues
  231. }
  232. default:
  233. battle(); //user didn't enter any of the battle options, so we ask them again
  234. break;
  235. }
  236.  
  237.  
  238.  
  239. } else {
  240. die = true; //dead
  241. if (health < 0) {
  242. health = 0;
  243. }
  244. System.out.println("Oh dear, you are dead!");
  245. System.out.println("You die with " +exp+ " exp and " +gold+ " gold.");
  246. }
  247.  
  248.  
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement