Guest User

Untitled

a guest
Jun 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.53 KB | None | 0 0
  1. import java.util.*;
  2. public class driver {
  3. static Scanner scan = new Scanner(System.in);
  4. static Random rand = new Random();
  5. static dice die = new dice();
  6. public static String playerName;
  7. public static int playerhp;
  8. public static int maxhp;
  9. public static int maxmana;
  10. public static int mana;
  11. public static int playermeleedmg;
  12. public static int xp;
  13. public static int enemyhp;
  14. public static int enemymeleedmg;
  15. public static int Level;
  16. public static String charclass;
  17. public static boolean fighting = false; //globals for player stats & enemy stats
  18.  
  19. private static void printStats() {
  20. if(charclass.equals("mage")){
  21. System.out.println(playerName + "nhp: " + playerhp + "nmana: " + mana + "ndamage: " + playermeleedmg + "nxp: " + xp + "n");
  22. }else{
  23. System.out.println(playerName + "nhp: " + playerhp + "ndamage: " + playermeleedmg + "nxp: " + xp + "n");
  24. }
  25. }
  26. private static void printEnemyStats() {
  27. System.out.println("Enemy "+"nhp: " + enemyhp + "ndmg: " + enemymeleedmg + "n");
  28. }
  29.  
  30. private static void buildWarrior() {
  31. charclass = "warrior";
  32. maxhp = 20;
  33. playerhp = 20;
  34. playermeleedmg = 4;
  35. xp = 0;
  36. Level = 1;
  37. }
  38. private static void buildArcher() {
  39. charclass = "archer";
  40. maxhp = 14;
  41. playerhp = 14;
  42. playermeleedmg = 6;
  43. xp = 0;
  44. Level = 1;
  45. }
  46. private static void buildMage() {
  47. charclass = "mage";
  48. maxhp = 10;
  49. playerhp = 10;
  50. mana = 20;
  51. maxmana = 20;
  52. playermeleedmg = 2;
  53. xp = 0;
  54. Level = 1; // initializes globals according to class
  55. }
  56. private static void buildEnemy() {
  57. switch(Level){
  58. case 1:
  59. enemyhp = 9;
  60. enemymeleedmg = 1;
  61. break;
  62. case 2:
  63. enemyhp = 19;
  64. enemymeleedmg = 4;
  65. break;
  66. case 3:
  67. enemyhp = 24;
  68. enemymeleedmg = 6;
  69. break;
  70. case 4:
  71. enemyhp = 32;
  72. enemymeleedmg = 7;
  73. break;
  74. case 5:
  75. enemyhp = 40;
  76. enemymeleedmg = 9;
  77. break; //initializes enemy stats based on player level
  78. }
  79. }
  80. private static void fight() {
  81. String action;
  82. String spellAction = null;
  83. System.out.println("An enemy approaches");
  84. buildEnemy();
  85. fighting = true;
  86. while(fighting = true){
  87. System.out.println("Press 'a' to attacknPress 'i' for info");
  88. if(charclass.equals("mage")){
  89. System.out.print("Press 's' for spellsn");
  90. }
  91. action = scan.nextLine();
  92. if(action.charAt(0) == 'a'){
  93. fighting = attack();
  94. if(fighting == false){
  95. switch(Level){
  96. case 1:
  97. xp = xp + 4;
  98. break;
  99. case 2:
  100. xp = xp + 6;
  101. break;
  102. case 3:
  103. xp = xp + 9;
  104. break;
  105. case 4:
  106. xp = xp + 12;
  107. break;
  108. }
  109. System.out.println("You earned :" + xp + " xp");
  110. checkLevelUp();
  111. return;
  112. }
  113. enemyattack();
  114. }
  115.  
  116. if(action.charAt(0) == 'i'){
  117. printStats();
  118. printEnemyStats();
  119.  
  120. }
  121. if(action.charAt(0) == 's'){
  122. System.out.println("Press 'f' for fireballnPress 'h' to healn");
  123. spellAction = scan.nextLine();
  124. if(spellAction.charAt(0) == 'f'){
  125. if(die.roll10() > 2){
  126. mana = mana - 10;
  127. if(mana <0){
  128. System.out.println("You don't have enough mana...");
  129. mana = mana + 10;
  130. }else{
  131. int k = die.roll10(); //randomly hurts 1-10
  132. System.out.println("You hit for " + k + " damage!");
  133. enemyhp = enemyhp - k;
  134. if(enemyhp <= 0){
  135. System.out.println("You Won!");
  136. switch(Level){
  137. case 1:
  138. xp = xp + 4;
  139. break;
  140. case 2:
  141. xp = xp + 6;
  142. break;
  143. case 3:
  144. xp = xp + 9;
  145. break;
  146. case 4:
  147. xp = xp + 12;
  148. break;
  149. }
  150. System.out.println("You earned :" + xp + " xp");
  151. checkLevelUp();
  152. return;
  153. }
  154. enemyattack();
  155. }
  156. }
  157. else{
  158. System.out.println("You miss!");
  159. enemyattack();
  160. }
  161. }else
  162. if(spellAction.charAt(0) == 'h'){
  163. mana = mana - 8;
  164. if(mana <0){
  165. System.out.println("You don't have enough mana...");
  166. mana = mana + 8;
  167. }else{
  168. int x = die.roll10(); //randomly heals 1-8
  169. System.out.println("You heal your wounds...");
  170. System.out.println("+ " + x + " hp");
  171. playerhp = playerhp + x;
  172. if(playerhp>maxhp){
  173. playerhp = maxhp;
  174. }
  175. enemyattack();
  176. }
  177. }
  178.  
  179. }
  180. }
  181. }
  182.  
  183. private static void checkLevelUp() {
  184. if(xp >= 100 && Level == 4){
  185. System.out.println("Level 5!");
  186. Level = Level + 1;
  187. maxhp = maxhp + 25;
  188. playerhp = maxhp;
  189. if(charclass.equals("mage")){
  190. maxmana = maxmana + 7;
  191. mana = maxmana;
  192. }
  193. playermeleedmg = playermeleedmg + 3;
  194. printStats();
  195. }else
  196. if(xp >= 50 && Level == 3){
  197. System.out.println("Level 4!");
  198. Level = Level + 1;
  199. maxhp = maxhp + 20;
  200. playerhp = maxhp;
  201. if(charclass.equals("mage")){
  202. maxmana = maxmana + 7;
  203. mana = maxmana;
  204. }
  205. playermeleedmg = playermeleedmg + 2;
  206. printStats();
  207. }else
  208. if(xp >= 25 && Level == 2){
  209. System.out.println("Level 3!");
  210. Level = Level + 1;
  211. maxhp = maxhp + 10;
  212. playerhp = maxhp;
  213. if(charclass.equals("mage")){
  214. maxmana = maxmana + 7;
  215. mana = maxmana;
  216. }
  217. playermeleedmg = playermeleedmg + 2;
  218. printStats();
  219. }else
  220. if(xp >= 10 && Level == 1){
  221. System.out.println("Level 2!");
  222. Level = Level + 1;
  223. maxhp = maxhp + 5;
  224. playerhp = maxhp;
  225. if(charclass.equals("mage")){
  226. maxmana = maxmana + 7;
  227. mana = maxmana;
  228. }
  229. playermeleedmg = playermeleedmg + 1;
  230. printStats();
  231. }//increments player level and adds to stats with xp
  232.  
  233. }
  234. private static void enemyattack() {
  235. if(die.roll6() > 2){
  236. System.out.println("Enemy hits!");
  237. playerhp = playerhp - enemymeleedmg;
  238. if(playerhp <= 0){
  239. gameover();
  240. System.exit(0);//game over if player health < 0
  241. }
  242. }else{
  243. System.out.println("Enemy Misses!");
  244. }
  245. }
  246. private static boolean attack() {
  247. if(die.roll6() > 2){
  248. System.out.println("You hit!");
  249. enemyhp = enemyhp - playermeleedmg;
  250. if(enemyhp <= 0){
  251. System.out.println("You Won!"); //prints if enemy health < 0
  252. return false;
  253. }
  254. }else{
  255. System.out.println("You miss!");
  256. }
  257. return true;
  258. }
  259.  
  260. private static void gameover() {
  261. System.out.println(playerName + " Died!") ;
  262. System.out.println("GAME OVER!");
  263. System.exit(0); //terminates if lost
  264. return;
  265. }
  266. public static void main(String[] args) {
  267. String charclass;
  268. int num = 2;
  269. while(num > 1){
  270. System.out.println("Enter your Name: ");
  271. playerName = scan.nextLine();
  272. System.out.println("Choose your class: ");
  273. System.out.println("'w' for warrior");
  274. System.out.println("'a' for archer");
  275. System.out.println("'m' for mage");
  276. charclass = scan.nextLine();
  277. while(charclass.charAt(0) != 'w' && charclass.charAt(0) != 'a' && charclass.charAt(0) != 'm'){
  278. System.out.println("'w' for warrior");
  279. System.out.println("'a' for archer");
  280. System.out.println("'m' for mage");
  281. charclass = scan.nextLine();
  282. }
  283. if(charclass.charAt(0) == 'w'){
  284. buildWarrior();
  285. }
  286. if(charclass.charAt(0) == 'a'){
  287. buildArcher();
  288. }
  289. if(charclass.charAt(0) == 'm'){
  290. buildMage();
  291. }
  292. printStats();
  293. while(Level == 1){
  294. fight();
  295. }
  296. System.out.println("This area is clear... time to move onn");
  297. while(Level == 2){
  298. fight();
  299. }
  300. System.out.println("This area is clear... time to move onn");
  301. while(Level == 3){
  302. fight();
  303. }
  304. System.out.println("This area is clear... time to move onn");
  305. while(Level == 4){
  306. fight();
  307. }
  308. System.out.println("This area is clear... time to move onn");
  309. while(Level == 5){
  310. fight();
  311. }//keeps in area until levelUp
  312. }
  313.  
  314. }
  315. }
  316.  
  317.  
  318.  
  319. import java.util.*;
  320. public class dice {
  321.  
  322. public int roll6(){
  323. Random rand = new Random();
  324. int n = rand.nextInt(7);
  325. while(n == 0){
  326. n = rand.nextInt(7);
  327. }//1-6
  328. return n;
  329. }
  330. public int roll10(){
  331. Random rand = new Random();
  332. int n = rand.nextInt(11);
  333. while(n == 0){
  334. n = rand.nextInt(11);
  335. }
  336. return n;
  337. }//1-10
  338. public int roll20(){
  339. Random rand = new Random();
  340. int n = rand.nextInt(21);
  341. while(n == 0){
  342. n = rand.nextInt(21);
  343. }//1-20
  344. return n;
  345. }
  346.  
  347. }
  348.  
  349. public class Dice {
  350. private final Random random = new Random();
  351.  
  352. public int roll(int max) {
  353. return 1 + random.nextInt(max);
  354. }
  355.  
  356. public int roll6() {
  357. return roll(6);
  358. }
  359.  
  360. // and so on ...
  361. }
  362.  
  363. if(charclass.charAt(0) == 'w'){
  364. buildWarrior();
  365. }
  366. if(charclass.charAt(0) == 'a'){
  367. buildArcher();
  368. }
  369. if(charclass.charAt(0) == 'm'){
  370. buildMage();
  371. }
  372.  
  373. switch(Level){
  374. case 1:
  375. enemyhp = 9;
  376. enemymeleedmg = 1;
  377. break;
  378. case 2:
  379. enemyhp = 19;
  380. enemymeleedmg = 4;
  381. break;
  382. case 3:
  383. enemyhp = 24;
  384. enemymeleedmg = 6;
  385. break;
  386. case 4:
  387. enemyhp = 32;
  388. enemymeleedmg = 7;
  389. break;
  390. case 5:
  391. enemyhp = 40;
  392. enemymeleedmg = 9;
  393. break; //initializes enemy stats based on player level
  394. }
  395.  
  396. int[] enemyHpArray = {1, 9, 19, 24, 32, 40};
  397. int[] enemyMeleedmgArray = {1, 1, 4, 6, 7, 9};
  398. enemyhp = enemyHpArray[Level];
  399. enemymeleedmg = enemyMeleedmgArray[Level];
  400.  
  401. enum CharacterClass {
  402. GENERIC_RANDOM_WARRIOR(true, true), SINGER(false, false), BIG_SLEEPER(false, true);
  403. public final boolean canWieldBigAxes;
  404. public final boolean loveToSleep;
  405.  
  406. private CharacterClass(final boolean canWieldBigAxes, final boolean loveToSleep) {
  407. this.canWieldBigAxes = canWieldBigAxes;
  408. this.loveToSleep = loveToSleep;
  409. }
  410. }
  411.  
  412. if (charClass.canWieldBigAxes) {
  413. // things
  414. }
  415. // instead of
  416. if (charClass.charAt(0) = 'w')
  417.  
  418. interface BattleUnit {
  419. int getLevel();
  420. void setLevel(final int level);
  421. int increaseLevel();
  422. boolean isDead();
  423. int getHP();
  424. int getMaxHP();
  425. int setHP(final int newHP);
  426. void damage(final int valueToSubstract);
  427. boolean attack(final BattleUnit target);
  428. int getMinMeleeDamage();
  429. int getMaxMeleeDamage();
  430. }
  431.  
  432. class Hero implements BattleUnit {
  433. private String name;
  434. private CharacterClass charClass;
  435. // some fields
  436.  
  437. public boolean isDead() {
  438. return getHP() <= 0;
  439. }
  440.  
  441. // blablabla moar code
  442. public boolean attack(final BattleUnit target) {
  443. if (die.roll6() > 2) {
  444. System.out.println("You hit!");
  445.  
  446. target.damage(produceDamage());
  447.  
  448. if (target.isDead()) {
  449. System.out.println("You Won!"); // prints if enemy health < 0
  450. return false;
  451. }
  452. } else {
  453. System.out.println("You miss!");
  454. }
  455. return true;
  456. }
  457.  
  458. }
  459. }else
  460. if(..){
  461. ..
  462. if(..){
  463. ..
  464. }else{
  465. ..
  466. ..
  467. if(..){
  468. ..
  469. }
  470. ..
  471. }
  472. }
  473. }
  474.  
  475. public static final int INITIAL_ARCHER_HP = 14;
  476.  
  477. private static void buildArcher() {
  478. ..
  479. playerhp = INITIAL_ARCHER_HP;
  480. ..
  481. }
  482.  
  483. System.out.println("'w' for warrior");
  484. System.out.println("'a' for archer");
  485. System.out.println("'m' for mage");
  486. charclass = scan.nextLine();
  487. while(charclass.charAt(0) != 'w' && charclass.charAt(0) != 'a' && charclass.charAt(0) != 'm'){
  488. System.out.println("'w' for warrior");
  489. System.out.println("'a' for archer");
  490. System.out.println("'m' for mage");
  491. charclass = scan.nextLine();
  492. }
  493.  
  494. char charclass; // changed to char rather than string
  495.  
  496. // ...
  497.  
  498. do {
  499. System.out.println("'w' for warrior");
  500. System.out.println("'a' for archer");
  501. System.out.println("'m' for mage");
  502. charclass = scan.nextLine().charAt(0);
  503.  
  504. } while (charclass != 'w' && charclass != 'a' && charclass != 'm')
  505.  
  506. while(Level == 1){
  507. fight();
  508. }
  509. System.out.println("This area is clear... time to move onn");
  510. while(Level == 2){
  511. fight();
  512. }
  513. System.out.println("This area is clear... time to move onn");
  514. while(Level == 3){
  515. fight();
  516. }
  517. System.out.println("This area is clear... time to move onn");
  518. while(Level == 4){
  519. fight();
  520. }
  521. System.out.println("This area is clear... time to move onn");
  522. while(Level == 5){
  523. fight();
  524. }
  525.  
  526. for (int currentLevel = 1; currentLevel <= 5; currentLevel++) {
  527. while(Level == currentLevel){
  528. fight();
  529. }
  530. System.out.println("This area is clear... time to move onn");
  531. }
  532.  
  533. public MagePlayerClass extends AbstractPlayerClass {
  534. @Override
  535. boolean canCreate(final String arg) {
  536. return "m".equalsIgnoreCase(arg);
  537. }
  538. }
Add Comment
Please, Sign In to add comment