Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1.  
  2. /*******************************
  3. * Summoner.java
  4. * Written by: Bridget Ratcliffe
  5. * Last modified: 2014-04-15
  6. *
  7. *
  8. *******************************/
  9. import java.util.Scanner;
  10. import java.util.Random;
  11. public class Homework7 {
  12. public static void main(String[] args) {
  13.  
  14. //SummonerList sList = new SummonerList();
  15. SuperList sList = new SuperList();
  16. Scanner kb = new Scanner(System.in);
  17.  
  18. char choice = '6';
  19.  
  20.  
  21. do { //menu
  22. System.out.println("Enter a choice from the menu:");
  23. System.out.println("1) Add a summoner");
  24. System.out.println("2) Add an elemental");
  25. System.out.println("3) Show summoners");
  26. System.out.println("4) Display a summoners elementals");
  27. System.out.println("5) Have a battle!");
  28. System.out.println("6) Exit the Program");
  29.  
  30. String line = kb.nextLine();
  31. if (line.length() >= 1) {
  32. choice = line.charAt(0);
  33.  
  34.  
  35. switch (choice) {
  36.  
  37. case '1': //add summoners
  38.  
  39. //String s = null;
  40. String summonerName = null;
  41. do{
  42.  
  43. System.out.print("Enter the name of the summoner you wish to add: ");
  44.  
  45. summonerName = kb.nextLine();
  46. //s = sList.findSummoner(summonerName);
  47.  
  48. }while (sList.containsObject(summonerName));
  49. Summoner newSummoner = new Summoner();
  50. newSummoner.setSumName(summonerName);
  51. sList.addObject(newSummoner);
  52.  
  53. break;
  54.  
  55. case '2': //add elemental
  56. //s = null;
  57. //Elemental e = null;
  58.  
  59. String elementalName = null;
  60.  
  61. System.out.print("Enter the name of the summoner: ");
  62. summonerName = kb.nextLine();
  63.  
  64. //s = sList.findSummoner(summonerName);
  65.  
  66. if (getSummoner(sList, summonerName) == null)
  67. break;
  68. else{
  69. Summoner s = getSummoner(sList, summonerName);
  70.  
  71. do{
  72. System.out.print("Enter the name of the elemental: ");
  73. elementalName = kb.nextLine();
  74. }while(s.hasElemental(elementalName));
  75.  
  76. String type = "";
  77. int level = 0;
  78. int attack = 0;
  79. int defense = 0;
  80. do{
  81. System.out.print("Enter type level attack defense ");
  82. type = kb.next();
  83. level = kb.nextInt();
  84. attack = kb.nextInt();
  85. defense = kb.nextInt();
  86.  
  87.  
  88. }while(!Elemental.validType(type) || !Elemental.validLevel(level) || !Elemental.validAttack(attack) || !Elemental.validDefense(defense));
  89.  
  90. Elemental e = new Elemental();
  91. e.setName(elementalName);
  92. e.setLevel(level);
  93. e.setAttack(attack);
  94. e.setDefense(defense);
  95. e.setHealth(level);
  96. s.addElemental(e);
  97. break;
  98. }
  99.  
  100. case '3': //display summoners in alphabetic order with amount of elementals and highest level
  101. sortSum(sList);
  102. for (int i=0;i<sList.getLength(); i++){
  103. Summoner disSum = (Summoner) sList.getObject(i);
  104. if (disSum != null){
  105. disSum.displaySum();
  106. }
  107.  
  108. }
  109. break;
  110.  
  111.  
  112. case '4': //display elementals
  113. System.out.print("Name of Summoner: ");
  114. String enterSum = kb.nextLine();
  115. Summoner ent = getSummoner(sList, enterSum);
  116. while(ent == null){
  117. System.out.print("Error. Not a valid Summoner name. Please try again: ");
  118. enterSum = kb.nextLine();
  119. ent = getSummoner(sList, enterSum);
  120. }
  121. ent.sortElem();
  122. ent.printElementals();
  123. break;
  124.  
  125. case '5': //battle
  126. System.out.print("Enter the name of the first summoner: ");
  127. String sumAttack = kb.nextLine();
  128. Summoner att = getSummoner(sList, sumAttack);
  129. System.out.print("Enter the elemental they wish to use: ");
  130. String elementAttack = kb.nextLine();
  131. if(!att.hasElemental(elementAttack)){
  132. System.out.println("Error. No elemental of that name.");
  133. break;
  134. }
  135. Elemental a = new Elemental();
  136. a = att.getElementalName(elementAttack);
  137.  
  138. System.out.print("Enter the name of the second summoner: ");
  139. String sumDef = kb.nextLine();
  140. Summoner def = getSummoner(sList, sumDef);
  141. System.out.print("Enter the elemental they wish to use: ");
  142. String elementDef = kb.nextLine();
  143. if(!def.hasElemental(elementDef)){
  144. System.out.println("Error. No elemental of that name.");
  145. break;
  146. }
  147. Elemental d = new Elemental();
  148. d = def.getElementalName(elementDef);
  149.  
  150. Random rand = new Random();
  151.  
  152.  
  153. while (a.isAlive() && d.isAlive()) {
  154. // set base to 1.0
  155. double base = 1.0;
  156. // critical occurs with 20% liklihood
  157. double critical = rand.nextInt() % 5 == 0 ? 2.0 : 1.0;
  158. double typeModifier = getModifier(a, d);
  159. double random = rand.nextDouble() * 0.75 + 0.75;
  160.  
  161. a.attack(d, base, critical, typeModifier, random);
  162.  
  163. // if b has taken enough damage to be dispersed, end the loop here
  164. if (!d.isAlive()) {
  165. break;
  166. }
  167.  
  168. // calculate new values for second attack
  169. critical = rand.nextInt() % 5 == 0 ? 2.0 : 1.0;
  170. typeModifier = getModifier(d, a);
  171. random = rand.nextDouble() * 0.75 + 0.75;
  172.  
  173. d.attack(a, base, critical, typeModifier, random);
  174.  
  175. } // end while
  176. break;
  177.  
  178.  
  179. case '6': //quit
  180. break;
  181.  
  182. default:
  183. System.out.println("Invalid menu choice");
  184. }
  185. }
  186.  
  187.  
  188. } while (choice != '6');
  189.  
  190. System.out.println("Goodbye!");
  191. }
  192.  
  193. public static Summoner getSummoner(SuperList input, String name){
  194. int length = input.getLength();
  195. for (int i=0; i<length; i++){
  196. Summoner summoner = (Summoner) input.getObject(i);
  197. if (summoner != null){
  198.  
  199. if (summoner.getSumName().equals(name)){
  200. return summoner;
  201. }
  202. }
  203. }return null;
  204. }
  205.  
  206. /** re-order the list of students so they appear in
  207. * alphabetic order based on their names (case insensitive);
  208. * uses Insertion Sort
  209. * adapted from Course.Java written by Professor Mitchell
  210. */
  211.  
  212. public static void sortSum(SuperList input){
  213. int length = input.getLength();
  214. for (int i=0; i<length;i++){
  215. for (int j=i-1; j>=0; j--){
  216. Summoner summoner1 = (Summoner) input.getObject(j);
  217. Summoner summoner2 = (Summoner) input.getObject(j+1);
  218.  
  219. if(summoner1.getSumName().charAt(0) > summoner2.getSumName().charAt(0))
  220. input.swapObject(j, j+1);
  221. }
  222. }
  223. }
  224.  
  225.  
  226. public static void sortElem(SuperList input){
  227. int length = input.getLength();
  228. for (int i=0; i<length-1; i++){
  229. Elemental elemental1 = (Elemental) input.getObject(i);
  230. Elemental elemental2 = (Elemental) input.getObject(i+1);
  231.  
  232. if(elemental1.getName().charAt(0) > elemental2.getName().charAt(0))
  233. input.swapObject(i, i+1);
  234. }
  235.  
  236.  
  237. }
  238.  
  239. public static Elemental createElemental(String name){
  240. Elemental elemenOPEY = new Elemental();
  241. elemenOPEY.setName(name);
  242. return elemenOPEY;
  243. }
  244.  
  245. /******************************************************************************
  246. * Get type-modifier for Elemental battle
  247. * @return value in the range [0.5,2.0] based on relative strength of Element types
  248. *****************************************************************************/
  249.  
  250. public static double getModifier(Elemental a, Elemental b) {
  251. String attackType = a.getType();
  252. String defenseType = b.getType();
  253. System.out.println(a + " " + b);
  254. if (attackType.equalsIgnoreCase("FIRE")) {
  255. if (defenseType.equalsIgnoreCase("FIRE")) {
  256. return 0.75;
  257. } else if (defenseType.equalsIgnoreCase("WATER")) {
  258. return 1.5;
  259. } else if (defenseType.equalsIgnoreCase("EARTH")) {
  260. return 0.5;
  261. } else { // defenseType == type.AIR
  262. return 1.25;
  263. }
  264. } else if (attackType.equalsIgnoreCase("WATER")) {
  265. if (defenseType.equalsIgnoreCase("FIRE")) {
  266. return 2.00;
  267. } else if (defenseType.equalsIgnoreCase("WATER")) {
  268. return 0.5;
  269. } else if (defenseType.equalsIgnoreCase("EARTH")) {
  270. return 1.5;
  271. } else { // defenseType == type.AIR
  272. return 0.5;
  273. }
  274. } else if (attackType.equalsIgnoreCase("EARTH")) {
  275. if (defenseType.equalsIgnoreCase("FIRE")) {
  276. return 1.00;
  277. } else if (defenseType.equalsIgnoreCase("WATER")) {
  278. return 1.5;
  279. } else if (defenseType.equalsIgnoreCase("EARTH")) {
  280. return 1.00;
  281. } else { // defenseType == type.AIR
  282. return 0.5;
  283. }
  284. } else { // attackType == Type.AIR
  285. if (defenseType.equalsIgnoreCase("FIRE")) {
  286. return 1.25;
  287. } else if (defenseType.equalsIgnoreCase("WATER")) {
  288. return 1.25;
  289. } else if (defenseType.equalsIgnoreCase("EARTH")) {
  290. return 0.75;
  291. } else { // defenseType == type.AIR
  292. return 0.75;
  293. }
  294. }
  295. } // end getModifier
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement