Advertisement
Guest User

Untitled

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