HW05
import java.lang.System;
import java.util.*;
/**
* Created by Sahil Pattni on 03-Oct-16.
*/
public class Pokemon {
private String name;
private String type = "Fire";
private int healthPower = 0;
private double baseAttackPower = 1;
private static int NUM_POKEMONS = 0;
public int id;
public Pokemon(String name, String type, int healthPower, double baseAttackPower) {
this.name = name;
this.healthPower = healthPower;
this.baseAttackPower = baseAttackPower;
this.type = type;
id = NUM_POKEMONS;
NUM_POKEMONS++;
}
public String getName() {
return this.name;
}
public int getId() {
return this.id;
}
public int getHealthPower() {
return healthPower;
}
public double getBaseAttackPower() {
return baseAttackPower;
}
public boolean setType(String type) {
if (type.equalsIgnoreCase("Water")) {
this.type = "Water";
return true;
}
else if (type.equalsIgnoreCase("Grass")) {
this.type = "Grass";
return true;
}
else if (type.equalsIgnoreCase("Fire")) {
this.type = "Fire";
return true;
}
else if (type.equalsIgnoreCase("Electric")) {
this.type = "Electric";
return true;
}
else {
return false;
}
}
public String getType() {
return this.type;
}
public boolean setHealthPower(int healthPower) {
if (healthPower >= 0) {
this.healthPower = healthPower;
return true;
}
else {
return false;
}
}
public boolean setBaseAttackPower(double baseAttackPower) {
if (baseAttackPower > 0) {
this.baseAttackPower = baseAttackPower;
return true;
}
else {
return false;
}
}
public String toString() {
return "Name: " + getName() + "\nID: " + getId() + "\nType: " + getType() + "\nHealth Power (HP): " + getHealthPower() + "\nBase attack power: " + getBaseAttackPower();
}
public boolean isDead() {
if (healthPower == 0) {
return true;
}
else {
return false;
}
}
public void boostHealthPower(int healthPower) {
this.healthPower += healthPower;
}
public void reduceHealthPower(int healthPower) {
if (this.healthPower - healthPower < 0) {
this.healthPower = 0;
}
else {
this.healthPower -= healthPower;
}
}
public static double getAttackMultiplier(Pokemon attacker, Pokemon defender) {
double factor = 0;
if (attacker.getType().equals("Grass")) {
switch (defender.getType()) {
case "Electric": factor = 1;
break;
case "Water": factor = 2;
break;
case "Fire": factor = 0.5;
break;
case "Grass": factor = 0.5;
default:
}
}
else if (attacker.getType().equals("Electric")) {
switch (defender.getType()) {
case "Water": factor = 2;
break;
case "Fire": factor = 1;
break;
case "Grass": factor = 0.5;
break;
case "Electric": factor = 0.5;
default:
}
}
else if(attacker.getType().equals("Water")) {
switch (defender.getType()) {
case "Electric": factor = 1;
break;
case "Fire": factor = 2;
break;
case "Grass": factor = 0.5;
break;
case "Water": factor = 0.5;
default:
}
}
else {
switch (defender.getType()) {
case "Electric": factor = 1;
break;
case "Water": factor = 0.5;
break;
case "Grass": factor = 2;
break;
case "Fire": factor = 0.5;
default:
}
}
return factor;
}
public static int battle(Pokemon p1, Pokemon p2) {
int winner;
System.out.println("\nInitialising battle..\n");
while (p1.healthPower > 0 && p2.healthPower > 0) {
p2.reduceHealthPower((int)(p1.baseAttackPower * getAttackMultiplier(p1,p2)));
p1.reduceHealthPower((int)(p2.baseAttackPower * getAttackMultiplier(p2,p1)));
}
if (p1.isDead()) {
winner = 2;
}
else if (p2.isDead()) {
winner = 1;
}
else {
winner = 0;
}
return winner;
}
public static int battleOracle(Pokemon p1, Pokemon p2) {
int winner;
int p1_health = p1.healthPower;
int p2_health = p2.healthPower;
double p1_attack = p1.baseAttackPower;
double p2_attack = p2.baseAttackPower;
while (p1_health > 0 || p2_health > 0) {
p1_health -= (p2_attack * getAttackMultiplier(p2,p1));
p2_health -= (p1_attack * getAttackMultiplier(p1,p2));
}
if (p1_health == 0) {
winner = 2;
}
else if (p2_health == 0) {
winner = 1;
}
else {
winner = 0;
}
return winner;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("----- CS180 Pokemon Gym -----");
//First Pokemon
System.out.println("Enter the first Pokemon's name:");
String first_name = s.next();
System.out.println("Enter the first Pokemon's type:");
String first_type = s.next();
first_type = MyUtils.formatStr(first_type);
System.out.println("Enter the first Pokemon's Health Power (HP):");
String first_hp = s.next();
while (!MyUtils.isNumeric(first_hp)) {
System.out.println("Invalid Health Power (HP) entered. Please re-enter");
first_hp = s.next();
}
int first_hp_int = Integer.parseInt(first_hp);
System.out.println("Enter the first Pokemon's Base Attack Power:");
String first_attack = s.next();
while (!MyUtils.isNumeric(first_attack)) {
System.out.println("Invalid Base Attack Power. Please re-enter.");
first_attack = s.next();
}
double first_attack_double = Double.parseDouble(first_attack);
Pokemon p1 = new Pokemon(first_name, "Fire", 0, 1);
p1.setType(first_type);
p1.setHealthPower(first_hp_int);
p1.setBaseAttackPower(first_attack_double);
//Second Pokemon
System.out.println("Enter the second Pokemon's name:");
String second_name = s.next();
System.out.println("Enter the second Pokemon's type:");
String second_type = s.next();
second_type = MyUtils.formatStr(second_type);
System.out.println("Enter the second Pokemon's Health Power (HP):");
String second_hp = s.next();
while (!MyUtils.isNumeric(second_hp)) {
System.out.println("Invalid Health Power (HP) entered. Please re-enter");
second_hp = s.next();
}
int second_hp_int = Integer.parseInt(second_hp);
System.out.println("Enter the second Pokemon's Base Attack Power:");
String second_attack = s.next();
while (!MyUtils.isNumeric(second_attack)) {
System.out.println("Invalid Base Attack Power. Please re-enter.");
second_attack = s.next();
}
double second_attack_double = Double.parseDouble(second_attack);
Pokemon p2 = new Pokemon(second_name, "Fire" , 0, 1);
p2.setType(second_type);
p2.setHealthPower(second_hp_int);
p2.setBaseAttackPower(second_attack_double);
System.out.println(p1.battle(p1,p2));
System.out.println("\nFirst Pokemon's stats after the battle: ");
System.out.println(p1.toString());
System.out.println("\nSecond Pokemon's stats after the battle: ");
System.out.println(p2.toString());
if (p1.battleOracle(p1,p2) == 1) {
System.out.println("The winner of the battle is " + p1.name);
}
else {
System.out.println("The winner of the battle is " + p2.name);
}
}
}