Guest User

Untitled

a guest
Nov 19th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.33 KB | None | 0 0
  1. //Fishing/hunting for consumables
  2.  
  3. import java.util.Scanner;
  4. import java.util.HashMap;
  5.  
  6. public class MyProgram extends ConsoleProgram{
  7. public String ind = "-----";
  8. public String header = "+++++++++++++++++";
  9. Scanner s = new Scanner(System.in);
  10.  
  11. public void run(){
  12. String userName = prompt("What is your name: ");
  13. Player user = new Player(userName);
  14. Weapon starter1 = new Weapon(2);
  15. Weapon starter2 = new Weapon(3);
  16.  
  17. Consumable chicken = new Consumable("Chicken Breast", 20, 1);
  18. user.addToInventory(starter1, 1);
  19. user.addToInventory(starter2, 1);
  20. user.addToInventory(chicken, 3);
  21.  
  22. String userInput = prompt("nWhat would you like to do (Type Commands to see a list): ");
  23. while(!userInput.equalsIgnoreCase("DONE")){
  24.  
  25. switch(userInput.toUpperCase()){
  26. case "COMMANDS" :
  27. System.out.println(header);
  28. System.out.println("Commands: Battle, Attributes, Check Inventory, Stats");
  29. break;
  30. case "BATTLE":
  31. System.out.println(header);
  32. battle(user);
  33. break;
  34. case "ATTRIBUTES":
  35. System.out.println(header);
  36. attribute(user);
  37. break;
  38. case "CHECK INVENTORY":
  39. System.out.println(header);
  40. inventory(user);
  41. break;
  42. case "STATS":
  43. System.out.println(header);
  44. stats(user);
  45. break;
  46.  
  47. default:
  48. System.out.println("Choose a correct command (Type Commands to see a list)");
  49. break;
  50. }
  51. if(user.isDead()){
  52. user.deathPenalty();
  53. System.out.println("nYou are revived, at the cost of half your gold and your current weapon" );
  54. user.incrementCurHp(user.getMaxHp());
  55. }
  56. if(user.isDead()){
  57. break;
  58. }else{
  59. userInput = prompt("nWhat would you like to do: ");
  60. }
  61. }
  62. }
  63. public String equip(Player player, Weapon item){
  64. if(player.equipWeapon(item)){
  65. return ind + player.getName() + " successfully equipped " + item.getName();
  66. }else{
  67. return ind + player.getName() + " failed to equip " + item.getName();
  68. }
  69. }
  70. public String consume(Player player, Consumable item){
  71. if(player.useConsumable(item)){
  72. return ind + player.getName() + " consumed a " + item.getName() + " and gained " + item.getHeal() + " Hp";
  73. }else{
  74. return ind + player.getName() + " failed to consume a " + item.getName();
  75. }
  76. }
  77. public String prompt(String question){
  78. System.out.print(question);
  79. String input = s.nextLine();
  80. return input;
  81. }
  82.  
  83. public int promptInt(String question){
  84. while(true){
  85. String input = prompt(question);
  86. try{
  87. int n = Integer.parseInt(input);
  88. return n;
  89. }catch(NumberFormatException e){
  90.  
  91. }
  92. }
  93. }
  94.  
  95. public boolean rollAgility(Creature attacker, int high){
  96. int roll = GameRandom.range(1, high);
  97. if(roll <= attacker.agilityBonus()){
  98. return true;
  99. }else{
  100. return false;
  101. }
  102. }
  103.  
  104. public String attack(Creature one, Creature two){
  105. boolean crit = rollAgility(one, 100);
  106. int a = one.attack(two, crit);
  107. if(crit){
  108. return one.getName() + " CRITICAL hit " + two.getName() + " for " + a + " damage.";
  109. }else{
  110. return one.getName() + " hit " + two.getName() + " for " + a + " damage.";
  111. }
  112. }
  113.  
  114. public void battle(Player player){
  115.  
  116. //Generation of a random Monster with a random Weapon of the monster's level
  117. Monster encounter = new Monster(player.getLevel() + 1);
  118. Weapon randomWeapon = new Weapon(encounter.getLevel());
  119. encounter.equipWeapon(randomWeapon);
  120. encounter.incrementCurHp(encounter.getMaxHp());
  121.  
  122. System.out.println(ind + "You encounter " + encounter.getName());
  123. System.out.println(ind + encounter.getWeapon());
  124. String input = prompt("n" + ind + "What do you do (Type Choices to see a list): ");
  125.  
  126. while(!player.isDead() && !encounter.isDead()){
  127. boolean run = false;
  128. switch(input.toUpperCase()){
  129. case "CHOICES":
  130. System.out.println(ind + "Attack, Run, Inventory(code this)");
  131. input = prompt(ind + "What do you do: ");
  132. continue;
  133. case "ATTACK":
  134. System.out.println(ind + attack(player, encounter));
  135. System.out.println(ind + encounter.status());
  136. break;
  137. case "RUN":
  138. run = rollAgility(player, 25);
  139. break;
  140. default:
  141. System.out.println(ind + "Choose a correct choice (Type Choices to see a list)");
  142. input = prompt(ind + "What do you do: ");
  143. continue;
  144. }
  145.  
  146. if(input.equalsIgnoreCase("run")){
  147. if(run){
  148. System.out.println(ind + "You succeed at running");
  149. break;
  150. }else{
  151. System.out.println(ind + "You fail at running");
  152. }
  153. }
  154.  
  155. if(encounter.isDead()){
  156. System.out.println("n" + ind + "You killed " + encounter.getName());
  157. System.out.println(ind + "You gain " + player.gainBattleXp(encounter) + " exp");
  158. if(player.checkXp()){
  159. System.out.println(ind + "You leveled up, your health is restored and you gain 1 attribute point!");
  160. }
  161. System.out.println(ind + "You gained " + player.gainBattleGold(encounter) + " gold");
  162. System.out.println(ind + "You have " + player.getXp() + " exp");
  163.  
  164. int roll = GameRandom.range(1, 20);
  165. if(roll <= 5){
  166. System.out.println(ind + "You find a " + randomWeapon.getName() + "!");
  167. player.addToInventory(randomWeapon, 1);
  168. }
  169.  
  170. break;
  171. }
  172. System.out.println(ind + attack(encounter, player));
  173. System.out.println(ind + player.status());
  174.  
  175. if(player.isDead()){
  176. System.out.println("n" + ind + "You fainted!");
  177. break;
  178. }
  179. input = prompt("n" + ind + "What do you do: ");
  180. }
  181. }
  182.  
  183. public void attribute(Player player){
  184. System.out.println(ind + "Your attribute points: " + player.getAtPoints() + "n" +
  185. ind + "Strength: " + player.getStrength() + "n" +
  186. ind + "Intellect: " + player.getIntellect() + "n" +
  187. ind + "Agility: " + player.getAgility() + "n" +
  188. ind + "Vitality: " + player.getVitality() + "n");
  189.  
  190. if(player.getAtPoints() > 0){
  191. String use = prompt(ind + "Do you want to use your " + player.getAtPoints() + " attribute points (Yes or No): ");
  192. if(use.equalsIgnoreCase("Yes")){
  193. String choice = prompt(ind + "What attribute do you want to be increased: ");
  194. int amount = promptInt(ind + "How many attribute points do you want to spend on it: ");
  195.  
  196. while(player.getAtPoints() > 0){
  197. if(amount <= player.getAtPoints()){
  198. switch(choice.toUpperCase()){
  199. case "STRENGTH":
  200. player.getAt().increaseStrength(amount);
  201. player.incrementAtPoints(-amount);
  202. System.out.println(ind + "Your base Strength is now: " + player.getAt().getStrength());
  203. break;
  204. case "INTELLECT":
  205. player.getAt().increaseIntellect(amount);
  206. player.incrementAtPoints(-amount);
  207. System.out.println(ind + "Your base Intellect is now: " + player.getAt().getIntellect());
  208. break;
  209. case "AGILITY":
  210. player.getAt().increaseAgility(amount);
  211. player.incrementAtPoints(-amount);
  212. System.out.println(ind + "Your base Agility is now: " + player.getAt().getAgility());
  213. break;
  214. case "VITALITY":
  215. player.getAt().increaseVitality(amount);
  216. player.incrementAtPoints(-amount);
  217. System.out.println(ind + "Your base Vitality is now: " + player.getAt().getVitality());
  218. player.applyVitality();
  219. break;
  220. case "DONE":
  221. amount = -1;
  222. break;
  223. default:
  224. System.out.println(ind + "Choose a correct attribute.");
  225. break;
  226. }
  227. if(amount == -1){
  228. break;
  229. }
  230. System.out.println(ind + "You have " + player.getAtPoints() + " attribute points left");
  231. if(player.getAtPoints() == 0){
  232. break;
  233. }
  234. choice = prompt(ind + "What attribute do you want to be increased: ");
  235. amount = promptInt(ind + "How many attribute points do you want to spend on it: ");
  236.  
  237. }else{
  238. System.out.println(ind + "You do not have that many attribute points");
  239. choice = prompt(ind + "What attribute do you want to be increased: ");
  240. amount = promptInt(ind + "How many attribute points do you want to spend on it: ");
  241. }
  242. }
  243. }
  244. }
  245. }
  246.  
  247. public void inventory(Player player){
  248. System.out.println(player.checkInventory(player));
  249. String ask = prompt(ind + "Do you want to equip a weapon or use a consumable: ");
  250. if(ask.equalsIgnoreCase("Equip a Weapon")){
  251. String equip = prompt(ind + "What do you want to equip: ");
  252. try{
  253. Item test = player.stringToItem(equip);
  254. if(player.stringToItem(equip).getType().equalsIgnoreCase("Weapon")){
  255. test = (Weapon)test;
  256. System.out.println(equip(player, (Weapon)player.stringToItem(equip)));
  257. }else{
  258. System.out.println(ind + "You cannot equip that");
  259. }
  260. }catch(NullPointerException e){
  261. System.out.println(ind + "Invalid weapon");
  262. }
  263. }else if(ask.equalsIgnoreCase("Use a Consumable")){
  264. String consumable = prompt(ind + "What do you want to use: ");
  265. try{
  266. Item test = player.stringToItem(consumable);
  267. if(test.getType().equalsIgnoreCase("Consumable")){
  268. test = (Consumable)test;
  269. System.out.println(consume(player, (Consumable)player.stringToItem(consumable)));
  270. }else{
  271. System.out.println(ind + "You cannot consume that");
  272. }
  273. }catch(NullPointerException e){
  274. System.out.println(ind + "Invalid consumable");
  275. }
  276. }
  277. }
  278.  
  279. public void stats(Player player){
  280. System.out.println(ind + player.getWeapon() + ind);
  281. System.out.println(ind + player.status());
  282. System.out.println(ind + "Level: " + player.getLevel());
  283. System.out.println(ind + "Exp: " + player.getXp());
  284. }
  285. }
  286.  
  287. public class GameRandom{
  288.  
  289. //Handles all random events such as pulling a number from a range or generating a name/stats
  290. public static int roll(int sides){
  291. int aRoll = (int)(Math.random() * sides + 1);
  292. return aRoll;
  293. }
  294. public static int range(double min, double max){
  295. return (int)(Math.random() * (max - min + 1) + min);
  296. }
  297.  
  298.  
  299. public static String monsterName(){
  300. String[] names = {"Spooky", "Scary", "Yup", "Yessir", "Grumblegore", "Minotaur"};
  301. int index = (int)(Math.random() * names.length);
  302.  
  303. return names[index];
  304. }
  305. public static Attributes monsterAttributes(int level){
  306. int str = (int)(level * Math.random() + 2);
  307. int intel = (int)(level * Math.random() + 2);
  308. int agil = (int)(level * Math.random() + 2);
  309. int vit = (int)(level * Math.random() + 2.5);
  310.  
  311. Attributes x = new Attributes(str, intel, agil, vit);
  312. return x;
  313. }
  314.  
  315. public static String weaponName(int level){
  316. String output = "";
  317. output += "Lv" + level + " ";
  318. String[] prefix;
  319. String[] base;
  320. String[] suffix;
  321. String suf = "";
  322. if(level <= 5){
  323. prefix = new String[]{"Broken" , "Bronze", "Copper", "Weak", "Training", "Fragile"};
  324. }else if(level <= 10){
  325. prefix = new String[]{"Iron", "Silver", "Cold Iron", "Darkwood", "Bone", "Reinforced"};
  326. }else{
  327. prefix = new String[]{"Gilded", "Warrior's", "Absolute", "Golden", "Mithril", "Holy", "Demonic"};
  328. suffix = new String[]{"Of Destruction", "Of Fear", "Of Spooks", "Of Serenity", "Of Hope", "Of Despair", "Of Depression"};
  329.  
  330. int sIndex = (int)(Math.random() * suffix.length);
  331. suf += " " + suffix[sIndex];
  332. }
  333. base = new String[]{"Lance", "Sword", "Dagger", "Staff", "Mace", "Pole", "Spear", "Halberd", "Scythe", "Knife", "Shiv", "Nunchucks", "Gauntlets", "Flail", "Morning Star" };
  334. int bIndex = (int)(Math.random() * base.length);
  335. int pIndex = (int)(Math.random() * prefix.length);
  336.  
  337. output += prefix[pIndex] + " ";
  338. output += base[bIndex];
  339. if(level > 10){
  340. output += suf;
  341. }
  342.  
  343. return output;
  344.  
  345. }
  346.  
  347. public static Attributes weaponAttributes(int level){
  348. int str = (int)(level * Math.random());
  349. int intel = (int)(level * Math.random());
  350. int agil = (int)(level * Math.random());
  351. int vit = (int)(level * Math.random());
  352.  
  353. Attributes x = new Attributes(str, intel, agil, vit);
  354. return x;
  355. }
  356.  
  357.  
  358. }
  359.  
  360. public abstract class Creature{
  361.  
  362. public String name;
  363. public int baseHp;
  364. public int maxHp;
  365. public int curHp;
  366. public int maxAtt;
  367. public int level;
  368. public Attributes attr;
  369. public Weapon equippedWeapon;
  370. public Weapon fists = new Weapon("Fists" , 0, 0, 0 ,0, 0);
  371.  
  372. public Creature(String name, int baseHp, int maxAtt, int level){
  373. this.name = name;
  374. this.level = level;
  375. this.maxAtt = maxAtt;
  376. this.attr = new Attributes(0, 0, 0 ,0);
  377. this.equippedWeapon = fists;
  378. this.baseHp = baseHp;
  379. this.applyVitality();
  380. this.curHp = maxHp;
  381. }
  382. //Names and Statuses
  383. public String getName(){
  384. return name;
  385. }
  386. public String getWeapon(){
  387. return name + " is wielding " + equippedWeapon.getName();
  388. }
  389.  
  390. public String status(){
  391. this.applyVitality();
  392. return name + " has " + curHp + "/" + maxHp + " Hp.";
  393. }
  394. public int getLevel(){
  395. return level;
  396. }
  397. public int getMaxHp(){
  398. return maxHp;
  399. }
  400. //Weapons
  401.  
  402.  
  403. //Battle Methods
  404. public int attack(Creature other, boolean crit){
  405. int att = maxAtt + (int)(this.strengthBonus() * level)/3;
  406. int roll = GameRandom.range(1, 100);
  407. //Pulls a boolean from the input that way crits can be announced in MyProgram
  408. if(crit){
  409. att *= 1.75;
  410. }
  411. int minAtt = (int)(att * .7);
  412. att = GameRandom.range(minAtt, att);
  413. other.incrementCurHp(-att);
  414.  
  415. return att;
  416. }
  417. public void incrementCurHp(int amount){
  418. applyVitality();
  419. curHp += amount;
  420. if(curHp >= maxHp){
  421. curHp = maxHp;
  422. }else if(curHp <= 0){
  423. curHp = 0;
  424. }
  425. }
  426. public int getCurHp(){
  427. return curHp;
  428. }
  429.  
  430. public int getMaxAtt(){
  431. return maxAtt;
  432. }
  433.  
  434. public boolean isDead(){
  435. return curHp <= 0;
  436. }
  437.  
  438. //Attributes
  439. public Attributes getAt(){
  440. return attr;
  441. }
  442.  
  443. public String getStrength(){
  444. return this.getAt().getStrength() + " + " + this.equippedWeapon.getAt().getStrength();
  445. }
  446.  
  447. public int strengthBonus(){
  448. return this.getAt().getStrength() + this.equippedWeapon.getAt().getStrength();
  449. }
  450.  
  451. public String getIntellect(){
  452. return this.getAt().getIntellect() +" + " + this.equippedWeapon.getAt().getIntellect();
  453. }
  454. public int intellectBonus(){
  455. return this.getAt().getIntellect() + this.equippedWeapon.getAt().getIntellect();
  456. }
  457.  
  458. public String getAgility(){
  459. return this.getAt().getAgility() + " + " + this.equippedWeapon.getAt().getAgility();
  460. }
  461.  
  462. public int agilityBonus(){
  463. return this.getAt().getAgility() + this.equippedWeapon.getAt().getAgility();
  464. }
  465.  
  466. public String getVitality(){
  467. return this.getAt().getVitality() + " + " +this.equippedWeapon.getAt().getVitality();
  468. }
  469.  
  470. public int vitalityBonus(){
  471. return this.getAt().getVitality() + this.equippedWeapon.getAt().getVitality();
  472. }
  473. public void applyVitality(){
  474. this.maxHp = (this.vitalityBonus() * level) + baseHp;
  475. }
  476. }
  477.  
  478. public class Attributes{
  479. public int strength;
  480. public int intellect;
  481. public int agility;
  482. public int vitality;
  483.  
  484. public Attributes(int strength, int intellect, int agility, int vitality){
  485. this.strength = strength;
  486. this.intellect = intellect;
  487. this.agility = agility;
  488. this.vitality = vitality;
  489. }
  490. public int getStrength(){
  491. return strength;
  492. }
  493. public int getIntellect(){
  494. return intellect;
  495. }
  496. public int getAgility(){
  497. return agility;
  498. }
  499. public int getVitality(){
  500. return vitality;
  501. }
  502. public void increaseStrength(int amount){
  503. strength += amount;
  504. }
  505. public void increaseIntellect(int amount){
  506. intellect += amount;
  507. }
  508. public void increaseAgility(int amount){
  509. agility += amount;
  510. }
  511. public void increaseVitality(int amount){
  512. vitality += amount;
  513. }
  514. }
  515.  
  516. import java.util.HashMap;
  517.  
  518. public class Player extends Creature{
  519.  
  520. public int gold;
  521. Attributes playerAttr;
  522. public int xp = 0;
  523. public int xpThreshold = 80;
  524. public int attributePoints;
  525. Inventory playerInv;
  526.  
  527. public Player(String name){
  528. super(name, 25, 15, 1);
  529. this.maxHp = baseHp;
  530. this.attr = new Attributes(0, 0 , 1, 0);
  531. this.playerInv = new Inventory(this);
  532. this.attributePoints = 5;
  533. this.gold = 0;
  534. }
  535. //equipping/unequipping weapons as it pertains to the player's inventory
  536. public boolean equipWeapon(Weapon weapon){
  537. if(weapon.getLevel() <= this.level){
  538. if(this.getHashMap().containsKey(weapon)){
  539. if(playerInv.incrementAmount(weapon, -1)){
  540. this.unequipWeapon();
  541. this.equippedWeapon = weapon;
  542. this.applyVitality();
  543. this.incrementCurHp(0);
  544. return true;
  545. }else{
  546. return false;
  547. }
  548. }
  549.  
  550. }else{
  551. this.incrementCurHp(0);
  552. return false;
  553. }
  554. return false;
  555. }
  556. public void unequipWeapon(){
  557. this.addToInventory(this.equippedWeapon, 1);
  558. this.equippedWeapon = fists;
  559. this.incrementCurHp(0);
  560. }
  561. //XP and Level ups
  562. public void levelUp(){
  563. level++;
  564. this.baseHp += level * 3;
  565. this.maxAtt += (int)(level * 1.5);
  566. this.applyVitality();
  567. this.curHp = this.maxHp;
  568.  
  569. attributePoints++;
  570. }
  571. public void setXpThreshold(){
  572. if(this.level <=2){
  573. xpThreshold = this.level * 80;
  574. }else{
  575. xpThreshold = this.level * (this.level - 2) * 120;
  576. }
  577. }
  578.  
  579. public int gainBattleXp(Creature other){
  580. int base = other.level * 40;
  581. int gain = GameRandom.range(base * 1.3, (base * .7));
  582.  
  583. xp += gain;
  584. return gain;
  585. }
  586. public boolean checkXp(){
  587. this.setXpThreshold();
  588.  
  589. if(xp >= xpThreshold){
  590. xp -= xpThreshold;
  591. this.levelUp();
  592. this.setXpThreshold();
  593.  
  594. return true;
  595. }else{
  596. return false;
  597. }
  598. }
  599. public int gainBattleGold(Creature other){
  600. int money = other.level * 40;
  601. int gain = GameRandom.range(money * 1.3, money * .7);
  602.  
  603. this.gold += gain;
  604. return gain;
  605. }
  606. public String getXp(){
  607. this.setXpThreshold();
  608. return xp + "/" + xpThreshold;
  609. }
  610.  
  611. //Attribute Points
  612. public int getAtPoints(){
  613. return attributePoints;
  614. }
  615. public void incrementAtPoints(int amount){
  616. attributePoints += amount;
  617. }
  618. //Inventory, HashMap with item keys and amount values
  619. public String checkInventory(Player player){
  620. return "-----Your Gold: " + this.gold + "n" + playerInv.checkInventory(player);
  621. }
  622. public void addToInventory(Item item, int amount){
  623. if(this.getHashMap().containsKey(item)){
  624. playerInv.incrementAmount(item, amount);
  625. }else{
  626. this.getHashMap().put(item, amount);
  627. }
  628. }
  629. public boolean removeFromInventory(Item item, int amount){
  630. if(this.getHashMap().containsKey(item)){
  631. if(playerInv.incrementAmount(item, amount)){
  632. return true;
  633. }else{
  634. return false;
  635. }
  636. }else{
  637. return false;
  638. }
  639. }
  640. public Inventory getInventory(){
  641. return playerInv;
  642. }
  643. public HashMap<Item, Integer> getHashMap(){
  644. return playerInv.inventory;
  645. }
  646. public boolean checkInventoryFor(String input){
  647. for(Item key: getHashMap().keySet()){
  648. if(input.equalsIgnoreCase(key.getName())){
  649. return true;
  650. }else{
  651. return false;
  652. }
  653.  
  654. }
  655. return true;
  656. }
  657. public Item stringToItem(String input){
  658. for(Item key: this.getHashMap().keySet()){
  659. if(key.getName().equalsIgnoreCase(input)){
  660. return key;
  661. }
  662. }
  663. return null;
  664. }
  665. public boolean useConsumable(Consumable item){
  666. if(this.getHashMap().containsKey(item)){
  667. if(playerInv.incrementAmount(item, -1)){
  668. this.applyVitality();
  669. this.incrementCurHp(item.getHeal());
  670. return true;
  671. }else{
  672. return false;
  673. }
  674. }
  675. return false;
  676. }
  677. public void deathPenalty(){
  678. this.gold = (int)(this.gold * .5);
  679. this.equippedWeapon = fists;
  680. }
  681. }
  682.  
  683. public class Monster extends Creature{
  684.  
  685.  
  686. //Hand created Monster
  687. public Monster(String name, int maxHp, int maxAtt, int level){
  688. super(name, maxHp, maxAtt, level);
  689. this.attr = GameRandom.monsterAttributes(level);
  690. this.applyVitality();
  691. this.curHp = this.maxHp;
  692. }
  693. //Randomly genereated Monster of a certain level
  694. public Monster(int level){
  695. super(GameRandom.monsterName(), 20, 10, level);
  696. this.attr = GameRandom.monsterAttributes(level);
  697. this.applyVitality();
  698. this.curHp = this.maxHp;
  699. }
  700.  
  701. public void equipWeapon(Weapon weapon){
  702. this.equippedWeapon = weapon;
  703. this.applyVitality();
  704. }
  705.  
  706.  
  707. }
  708.  
  709. public class Item{
  710.  
  711. public String name;
  712. public Attributes attr;
  713.  
  714. public String type;
  715.  
  716. public Item(String name){
  717. this.name = name;
  718. this.attr = new Attributes(0, 0 ,0 ,0);
  719. this.type = "item";
  720. }
  721.  
  722. public String getName(){
  723. return name;
  724. }
  725. public String toString(){
  726. return name;
  727. }
  728. public Attributes getAt(){
  729. return attr;
  730. }
  731.  
  732. public String getType(){
  733. return type;
  734. }
  735.  
  736. public class Weapon extends Item{
  737.  
  738. Attributes weaponAttr;
  739. public int level;
  740.  
  741. public Weapon(String name, int strength, int magic, int agility, int vitality, int level){
  742. super(name);
  743. this.weaponAttr = new Attributes(strength, magic, agility, vitality);
  744. this.level = level;
  745. this.type = "Weapon";
  746. }
  747. public Weapon(int level){
  748. super(GameRandom.weaponName(level));
  749. this.weaponAttr = GameRandom.weaponAttributes(level);
  750. this.level = level;
  751. this.type = "Weapon";
  752. }
  753. public Attributes getAt(){
  754. return weaponAttr;
  755. }
  756. public int getLevel(){
  757. return level;
  758. }
  759. }
  760.  
  761. public class Consumable extends Item{
  762.  
  763. public int heal;
  764. public int level;
  765.  
  766. public Consumable(String name, int heal, int level){
  767. super(name);
  768. this.heal = heal;
  769. this.level = level;
  770. this.type = "Consumable";
  771. }
  772. //Create a second constructor to generate a random consumable from a level
  773. //Create name databases within GameRandom for fishing/hunting
  774. public int getHeal(){
  775. return heal;
  776. }
  777. }
  778.  
  779. import java.util.HashMap;
  780.  
  781. public class Inventory{
  782. public Creature owner;
  783. public HashMap<Item , Integer> inventory = new HashMap<Item , Integer>();
  784.  
  785. public Inventory(Creature owner){
  786. this.owner = owner;
  787. }
  788. public boolean incrementAmount(Item item, int change){
  789. if(change > 0){
  790. int oldValue = inventory.get(item);
  791. int newValue = oldValue + change;
  792. inventory.put(item, newValue);
  793. return true;
  794. }else{
  795. if(inventory.get(item) >= change){
  796. int oldValue = inventory.get(item);
  797. int newValue = oldValue + change;
  798. inventory.put(item, newValue);
  799. return true;
  800. }else{
  801. return false;
  802. }
  803. }
  804.  
  805.  
  806. }
  807. public HashMap<Item, Integer> inventory(){
  808. return inventory;
  809. }
  810. //Consider splitting inventory into two/three hashmaps (Weapon, Consumable, collectible etc)
  811. public String checkInventory(Player player){
  812. String output = "";
  813. for(Item key: inventory.keySet()){
  814. if(!(key.getName().equalsIgnoreCase("Fists"))){
  815. Integer amount = inventory.get(key);
  816. if(amount > 0){
  817. String name = key.getName();
  818. output += "-----" + name;
  819. if(key.getType().equalsIgnoreCase("Weapon")){
  820. key = (Weapon)key;
  821. int str = key.getAt().getStrength();
  822. int inte = key.getAt().getIntellect();
  823. int agi = key.getAt().getAgility();
  824. int vit = key.getAt().getVitality();
  825. output += "(" + str + ", " + inte + ", " + agi + ", " + vit + ")";
  826. }
  827. output += ": " + amount + "n";
  828. }
  829. }
  830.  
  831. }
  832. return output;
  833. }
  834. }
Add Comment
Please, Sign In to add comment