Guest User

Untitled

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×