Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************
- * Summoner.java
- * Written by: Bridget Ratcliffe
- * Last modified: 2014-04-15
- *
- *
- *******************************/
- import java.util.Scanner;
- import java.util.Random;
- public class Homework7 {
- public static void main(String[] args) {
- //SummonerList sList = new SummonerList();
- SuperList sList = new SuperList();
- Scanner kb = new Scanner(System.in);
- char choice = '6';
- do { //menu
- System.out.println("Enter a choice from the menu:");
- System.out.println("1) Add a summoner");
- System.out.println("2) Add an elemental");
- System.out.println("3) Show summoners");
- System.out.println("4) Display a summoners elementals");
- System.out.println("5) Have a battle!");
- System.out.println("6) Exit the Program");
- String line = kb.nextLine();
- if (line.length() >= 1) {
- choice = line.charAt(0);
- switch (choice) {
- case '1': //add summoners
- //String s = null;
- String summonerName = null;
- do{
- System.out.print("Enter the name of the summoner you wish to add: ");
- summonerName = kb.nextLine();
- //s = sList.findSummoner(summonerName);
- }while (sList.containsObject(summonerName));
- Summoner newSummoner = new Summoner();
- newSummoner.setSumName(summonerName);
- sList.addObject(newSummoner);
- break;
- case '2': //add elemental
- //s = null;
- //Elemental e = null;
- String elementalName = null;
- System.out.print("Enter the name of the summoner: ");
- summonerName = kb.nextLine();
- //s = sList.findSummoner(summonerName);
- if (getSummoner(sList, summonerName) == null)
- break;
- else{
- Summoner s = getSummoner(sList, summonerName);
- do{
- System.out.print("Enter the name of the elemental: ");
- elementalName = kb.nextLine();
- }while(s.hasElemental(elementalName));
- String type = "";
- int level = 0;
- int attack = 0;
- int defense = 0;
- do{
- System.out.print("Enter type level attack defense ");
- type = kb.next();
- level = kb.nextInt();
- attack = kb.nextInt();
- defense = kb.nextInt();
- }while(!Elemental.validType(type) || !Elemental.validLevel(level) || !Elemental.validAttack(attack) || !Elemental.validDefense(defense));
- Elemental e = new Elemental();
- e.setName(elementalName);
- e.setLevel(level);
- e.setAttack(attack);
- e.setDefense(defense);
- e.setHealth(level);
- s.addElemental(e);
- break;
- }
- case '3': //display summoners in alphabetic order with amount of elementals and highest level
- sortSum(sList);
- for (int i=0;i<sList.getLength(); i++){
- Summoner disSum = (Summoner) sList.getObject(i);
- if (disSum != null){
- disSum.displaySum();
- }
- }
- break;
- case '4': //display elementals
- System.out.print("Name of Summoner: ");
- String enterSum = kb.nextLine();
- Summoner ent = getSummoner(sList, enterSum);
- while(ent == null){
- System.out.print("Error. Not a valid Summoner name. Please try again: ");
- enterSum = kb.nextLine();
- ent = getSummoner(sList, enterSum);
- }
- ent.sortElem();
- ent.printElementals();
- break;
- case '5': //battle
- System.out.print("Enter the name of the first summoner: ");
- String sumAttack = kb.nextLine();
- Summoner att = getSummoner(sList, sumAttack);
- System.out.print("Enter the elemental they wish to use: ");
- String elementAttack = kb.nextLine();
- if(!att.hasElemental(elementAttack)){
- System.out.println("Error. No elemental of that name.");
- break;
- }
- Elemental a = new Elemental();
- a = att.getElementalName(elementAttack);
- System.out.print("Enter the name of the second summoner: ");
- String sumDef = kb.nextLine();
- Summoner def = getSummoner(sList, sumDef);
- System.out.print("Enter the elemental they wish to use: ");
- String elementDef = kb.nextLine();
- if(!def.hasElemental(elementDef)){
- System.out.println("Error. No elemental of that name.");
- break;
- }
- Elemental d = new Elemental();
- d = def.getElementalName(elementDef);
- Random rand = new Random();
- while (a.isAlive() && d.isAlive()) {
- // set base to 1.0
- double base = 1.0;
- // critical occurs with 20% liklihood
- double critical = rand.nextInt() % 5 == 0 ? 2.0 : 1.0;
- double typeModifier = getModifier(a, d);
- double random = rand.nextDouble() * 0.75 + 0.75;
- a.attack(d, base, critical, typeModifier, random);
- // if b has taken enough damage to be dispersed, end the loop here
- if (!d.isAlive()) {
- break;
- }
- // calculate new values for second attack
- critical = rand.nextInt() % 5 == 0 ? 2.0 : 1.0;
- typeModifier = getModifier(d, a);
- random = rand.nextDouble() * 0.75 + 0.75;
- d.attack(a, base, critical, typeModifier, random);
- } // end while
- break;
- case '6': //quit
- break;
- default:
- System.out.println("Invalid menu choice");
- }
- }
- } while (choice != '6');
- System.out.println("Goodbye!");
- }
- public static Summoner getSummoner(SuperList input, String name){
- int length = input.getLength();
- for (int i=0; i<length; i++){
- Summoner summoner = (Summoner) input.getObject(i);
- if (summoner != null){
- if (summoner.getSumName().equals(name)){
- return summoner;
- }
- }
- }return null;
- }
- /** re-order the list of students so they appear in
- * alphabetic order based on their names (case insensitive);
- * uses Insertion Sort
- * adapted from Course.Java written by Professor Mitchell
- */
- public static void sortSum(SuperList input){
- int length = input.getLength();
- for (int i=0; i<length;i++){
- for (int j=i-1; j>=0; j--){
- Summoner summoner1 = (Summoner) input.getObject(j);
- Summoner summoner2 = (Summoner) input.getObject(j+1);
- if(summoner1.getSumName().charAt(0) > summoner2.getSumName().charAt(0))
- input.swapObject(j, j+1);
- }
- }
- }
- public static void sortElem(SuperList input){
- int length = input.getLength();
- for (int i=0; i<length-1; i++){
- Elemental elemental1 = (Elemental) input.getObject(i);
- Elemental elemental2 = (Elemental) input.getObject(i+1);
- if(elemental1.getName().charAt(0) > elemental2.getName().charAt(0))
- input.swapObject(i, i+1);
- }
- }
- public static Elemental createElemental(String name){
- Elemental elemenOPEY = new Elemental();
- elemenOPEY.setName(name);
- return elemenOPEY;
- }
- /******************************************************************************
- * Get type-modifier for Elemental battle
- * @return value in the range [0.5,2.0] based on relative strength of Element types
- *****************************************************************************/
- public static double getModifier(Elemental a, Elemental b) {
- String attackType = a.getType();
- String defenseType = b.getType();
- System.out.println(a + " " + b);
- if (attackType.equalsIgnoreCase("FIRE")) {
- if (defenseType.equalsIgnoreCase("FIRE")) {
- return 0.75;
- } else if (defenseType.equalsIgnoreCase("WATER")) {
- return 1.5;
- } else if (defenseType.equalsIgnoreCase("EARTH")) {
- return 0.5;
- } else { // defenseType == type.AIR
- return 1.25;
- }
- } else if (attackType.equalsIgnoreCase("WATER")) {
- if (defenseType.equalsIgnoreCase("FIRE")) {
- return 2.00;
- } else if (defenseType.equalsIgnoreCase("WATER")) {
- return 0.5;
- } else if (defenseType.equalsIgnoreCase("EARTH")) {
- return 1.5;
- } else { // defenseType == type.AIR
- return 0.5;
- }
- } else if (attackType.equalsIgnoreCase("EARTH")) {
- if (defenseType.equalsIgnoreCase("FIRE")) {
- return 1.00;
- } else if (defenseType.equalsIgnoreCase("WATER")) {
- return 1.5;
- } else if (defenseType.equalsIgnoreCase("EARTH")) {
- return 1.00;
- } else { // defenseType == type.AIR
- return 0.5;
- }
- } else { // attackType == Type.AIR
- if (defenseType.equalsIgnoreCase("FIRE")) {
- return 1.25;
- } else if (defenseType.equalsIgnoreCase("WATER")) {
- return 1.25;
- } else if (defenseType.equalsIgnoreCase("EARTH")) {
- return 0.75;
- } else { // defenseType == type.AIR
- return 0.75;
- }
- }
- } // end getModifier
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement