/* AUTHOR: Dr. Sandra Bartlett TA: Nasir Mohammad COURSE: EECS/CS 285 PROJECT: Object Oriented Design and Programming DUE DATE: Oct. 1, 2001 SUBMISSION DATE: Oct. 1, 2001 LOCATION: /net/www/y/u/b/bartlett/public_html/Person SUMMARY This class is the base class for all the little people in the strategy game. INPUT None OUTPUT None CLASS HIERARCHY Object - Person ASSUMPTIONS None */ import java.util.Random; import java.awt.*; abstract class Person { public static final String DEFAULT_NAME = "Fred"; public static final char MALE = 'M'; public static final char FEMALE = 'F'; public static final int AVERAGE_INTELLIGENCE = 100; public static final int MAX_ATTACK = 100; public static final int MAX_DEFENSE = 100; public static final int PUMP_COST = 1000; // cost to increase defense private static final Random random = new Random(); private static int population; String name; char gender; int age; String ethnicity; int intelligence; // expressed as IQ int attackStrength; int defenseStrength; int health = 100; float money; Point location = new Point(); // upper left corner of the bounding box public Person() { this(DEFAULT_NAME, FEMALE, null, AVERAGE_INTELLIGENCE); } // default constructor // newborn person public Person(String name, char gender, String ethnicity, int intelligence) { this(name, gender, ethnicity, intelligence, 0, 0, 0, (float)0.0); } // constructor - newborn // random adult public Person(String name, char gender, String ethnicity) { this(name, gender, ethnicity, (int)(random.nextGaussian() * 5 + AVERAGE_INTELLIGENCE), (int)(random.nextGaussian() * 15 + 40), // age (int)(random.nextGaussian() * 25 + 25), // attack (int)(random.nextGaussian() * 25 + 25), // defense (float)(random.nextFloat() * 10000)); // money } // constructor - random adult protected Person(String n, char g, String e, int i, int a, int att, int def, float m) { name = n; gender = g; ethnicity = e; intelligence = i; age = a; attackStrength = att; defenseStrength = def; money = m; population++; } // constructor public static int getPopulation() { return population; } // getPopulation public String getName() { return name; } // getName // for women who change their last name when they marry, or whatever public void setName(String newName) { name = newName; } // setName public char getGender() { return gender; } // getGender public String getGenderString() { switch(gender) { case MALE: return "male"; case FEMALE: return "female"; default: return "undetermined"; } // switch } // getGenderString // for people who have a sex change operation public void setGender(char sex) { switch(sex) { case 'M': case 'm': gender = MALE; break; case 'F': case 'f': gender = FEMALE; break; default: gender = sex; } // switch } // setGender public int getAge() { return age; } // getAge public void hasBirthday() { age++; } // hasBirthday public String getEthnicity() { if (ethnicity == null) return "not specified"; return ethnicity; } // getEthnicity public int getIntelligence() { return intelligence; } // getIntelligence public void studies() { if (intelligence <= AVERAGE_INTELLIGENCE) intelligence++; else intelligence += (intelligence - AVERAGE_INTELLIGENCE) / 5 + 1; } // studies public int getAttackStrength() { return attackStrength; } // getAttackStrength public int getDefenseStrength() { return defenseStrength; } // getDefenseStrength public void pumpIron() { if (money > PUMP_COST) { defenseStrength++; money -= PUMP_COST; } // if } // pumpIron // approximation of the algorithm used in Evernight. // I added a factor of intelligence - a smart person gets a bonus // on attack or defense and a dumb person gets a penalty. // also, a good attacker does more damage if he makes a "critical hit" // (I got this idea from Pokemon) public void attack(Person defender) { int attackPower = (int)(random.nextDouble() * 101); // if attacker hits if (attackPower <= attackStrength + (intelligence - AVERAGE_INTELLIGENCE) / 5) // and if defender doesn't defend if (random.nextDouble() * 101 > defender.getDefenseStrength() + (intelligence - AVERAGE_INTELLIGENCE) / 5) defender.takesHit(attackPower > MAX_ATTACK / 2 && attackPower == attackStrength ? 5 : 1); if (defender.getHealth() <= 0) attackStrength++; } // attack public int getHealth() { return health; } // getHealth // make this protected in case a sub class has some other way // of getting hurt - like a car accident or something protected void takesHit(int damage) { health -= damage; } // takesHit public void heal(int amount) { if (amount > 0) health += amount; } // heal public float getMoney() { return money; } // getMoney public void earnMoney(float amount) { if (amount > 0) money += amount; } // earnMoney public Point getLocation() { return location; } // getLocation abstract public void draw (Graphics g); // move the Person to the specified location protected void moveTo(int toX, int toY) { location.translate(toX - location.x, toY - location.y); } // moveTo protected void moveTo(Point p) { location.x = p.x; location.y = p.y; } // moveTo Point // move the Person the specified number of pixels in each direction protected void move(int deltaX, int deltaY) { location.translate(deltaX, deltaY); } // move } // Person