Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.15 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. if(amountNorth>5 || amountNorth<-5 || amountEast>5 || amountEast <-5){
  88. int exit = (int)Math.floor(Math.random() * 32);
  89. if (exit == 16);
  90. JOptionPane.showMessageDialog(null, "You have found an exit!");
  91. JOptionPane.showMessageDialog(null, "Do you want to leave now?");
  92. leave();
  93. {
  94. }
  95. }
  96. }
  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. amountNorth++;
  195. if(amountNorth>0 && amountEast>0){
  196. System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  197. } else if(amountNorth<0 && amountEast<0) {
  198. System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  199. }
  200. else if(amountNorth>0 && amountEast<0){
  201. System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
  202. }
  203. else if(amountNorth<0 && amountEast>0){
  204. System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
  205. }
  206. else if(amountNorth==0 && amountEast==0){
  207. System.out.println("You are in between North and South and East and West");
  208. }
  209. else if(amountNorth==0 && amountEast<0){
  210. System.out.println("You are inbetween North and South and at West " + amountEast*-1);
  211. }
  212. else if(amountNorth<0 && amountEast==0){
  213. System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  214. }
  215. else if(amountNorth==0 && amountEast>0){
  216. System.out.println("You are inbetween North and South and at East " + amountEast);
  217. }
  218. else if(amountNorth>0 && amountEast==0){
  219. System.out.println("You are at North " + amountNorth + " and in between East and West");
  220. }
  221. return 0;
  222. case 'E':
  223. System.out.println("You went east.");
  224. amountEast++;
  225. if(amountNorth>0 && amountEast>0){
  226. if(amountNorth>0 && amountEast>0){
  227. System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  228. } else if(amountNorth<0 && amountEast<0) {
  229. System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  230. }
  231. else if(amountNorth>0 && amountEast<0){
  232. System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
  233. }
  234. else if(amountNorth<0 && amountEast>0){
  235. System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
  236. }
  237. else if(amountNorth==0 && amountEast==0){
  238. System.out.println("You are in between North and South and East and West");
  239. }
  240. else if(amountNorth==0 && amountEast<0){
  241. System.out.println("You are inbetween North and South and at West " + amountEast*-1);
  242. }
  243. else if(amountNorth<0 && amountEast==0){
  244. System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  245. }
  246. else if(amountNorth==0 && amountEast>0){
  247. System.out.println("You are inbetween North and South and at East " + amountEast);
  248. }
  249. else if(amountNorth>0 && amountEast==0){
  250. System.out.println("You are at North " + amountNorth + " and in between East and West");
  251. }
  252. }
  253. return 1;
  254. case 'S':
  255. System.out.println("You went south.");
  256. amountNorth--;
  257. if(amountNorth>0 && amountEast>0){
  258. System.out.println("You are at North " + amountNorth + " and East " + amountEast);
  259. } else if(amountNorth<0 && amountEast<0) {
  260. System.out.println("You are at South " + amountNorth*-1 + " and West " + amountEast*-1);
  261. }
  262. else if(amountNorth>0 && amountEast<0){
  263. System.out.println("You are at North " + amountNorth + " and West " + amountEast*-1);
  264. }
  265. else if(amountNorth<0 && amountEast>0){
  266. System.out.println("You are at South " + amountNorth*-1 + " and East " + amountEast);
  267. }
  268. else if(amountNorth==0 && amountEast==0){
  269. System.out.println("You are in between North and South and East and West");
  270. }
  271. else if(amountNorth==0 && amountEast<0){
  272. System.out.println("You are inbetween North and South and at West " + amountEast*-1);
  273. }
  274. else if(amountNorth<0 && amountEast==0){
  275. System.out.println("You are at South " + amountNorth*-1 + " and in between East and West");
  276. }
  277. else if(amountNorth==0 && amountEast>0){
  278. System.out.println("You are inbetween North and South and at East " + amountEast);
  279. }
  280. else if(amountNorth>0 && amountEast==0){
  281. System.out.println("You are at North " + amountNorth + " and in between East and West");
  282. }
  283. return 2;
  284. case 'W':
  285. System.out.println("You went west.");
  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. 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.  
  393. }
  394. break;
  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. }
  448. default:
  449. battle(); //user didn't enter any of the battle options, so we ask them again
  450. break;
  451. }
  452.  
  453.  
  454.  
  455. } else if(health < 1) {
  456.  
  457. die = true; //dead
  458. if (health < 0) {
  459. health = 0;
  460. }
  461. System.out.println("Oh dear, you are dead!");
  462. JOptionPane.showMessageDialog(null, "Oh dear, you are dead!");
  463. System.out.println("You die with " +exp+ " exp and " +gold+ " gold.");
  464. JOptionPane.showMessageDialog(null, "You die with " +exp+ " exp and " +gold+ " gold.");
  465. System.out.println("You die at " + amountNorth + amountEast);
  466. JOptionPane.showMessageDialog(null, "You die at North " + amountNorth + " and East " + amountEast);
  467. System.exit(0);
  468.  
  469. }
  470. }
  471.  
  472. public static void level ()
  473. {
  474. switch (level)
  475. {
  476.  
  477. case 0:
  478. if(exp<20)
  479. JOptionPane.showMessageDialog(null, "You are level " + level + ".");
  480. System.out.println("Level "+ level+ ".");
  481. b
  482. reak;
  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