Created
February 14, 2014 21:36
-
-
Save GrantSchiller/9009841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class AwkwardCritter extends Critter { | |
public AwkwardCritter() { | |
super(); | |
} | |
public void passDay() { | |
changeAge(showAge() + 1); | |
super.action("Eat", 85, 2, 20); | |
super.action("Sleep", 50, 9, 2); | |
meditate(); | |
yodel(); | |
} | |
public String displayType() { | |
return "Awkward"; | |
} | |
public void meditate() { | |
Random r = new Random(); | |
int randomInt = r.nextInt(100) + 1; | |
if (randomInt > 50) { | |
changeHealth(showHealth() + 2); | |
} else { | |
changeHealth(showHealth() - 7); | |
} | |
} | |
public void yodel () { | |
if(showHealth() > 80) { | |
if (showLuck() > 7) { | |
changeHealth(showHealth() + 3); | |
} else { | |
changeHealth(showHealth() + 2); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public abstract class Critter { | |
int age; | |
int health; | |
int luck; | |
public Critter() { | |
age = 0; | |
health = 100; | |
Random r = new Random(); | |
luck = 1 + r.nextInt(10); | |
} | |
public String toString() { | |
return displayType() + " Critter" + ":\n Age: " + age + "\n Health: " + health + "\n Luck: " + luck + "\n"; | |
} | |
public boolean calculateChances(int percent) { | |
int newChance = percent + ((showLuck() * 3) - 15); | |
Random r = new Random(); | |
int randomInt = r.nextInt(100) + 1; | |
return (randomInt <= newChance); | |
} | |
public abstract void passDay(); | |
public abstract String displayType(); | |
public void action(String action, int chance, int benefit, int damage) { | |
boolean success = calculateChances(chance); | |
if(success == true) { | |
health += benefit; | |
} else { | |
health -= damage; | |
} | |
if (health > 100) { | |
health = 100; | |
} else if (health < 0) { | |
health = 0; | |
} | |
} | |
public int showAge() { | |
return age; | |
} | |
public void changeAge(int a) { | |
age = a; | |
} | |
public int showHealth() { | |
return health; | |
} | |
public void changeHealth(int h) { | |
health = h; | |
} | |
public int showLuck() { | |
return luck; | |
} | |
public void changeLuck(int l) { | |
luck = l; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Environment { | |
private Critter[] critters; | |
public static void main(String[] args) { | |
Environment environment = new Environment(1); | |
environment.gogo(30); | |
} | |
public Environment(int foo) { | |
critters = new Critter[foo*3]; | |
for(int i=0; i<foo; i++) { | |
critters[i] = new AwkwardCritter(); | |
} | |
for(int i=0; i<foo; i++) { | |
critters[foo+i] = new SmellyCritter(); | |
} | |
for(int i=0; i<foo; i++) { | |
critters[(foo*2)+i] = new UglyCritter(); | |
} | |
} | |
public void gogo(int days) { | |
for (int i=0; i<days; i++) { | |
System.out.println("\n***DAY " + (i+1) + "***\n"); | |
for (Critter critter: critters) { | |
critter.passDay(); | |
System.out.println(critter); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SmellyCritter extends Critter { | |
public SmellyCritter() { | |
super(); | |
} | |
public void passDay() { | |
changeAge(showAge() + 1); | |
dance(); | |
super.action("Eat", 40, 6, 10); | |
super.action("Sleep", 80, 2, 1); | |
} | |
public String displayType() { | |
return "Smelly"; | |
} | |
private void dance() { | |
if (showAge() > 15) { | |
changeHealth(showHealth() + 1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UglyCritter extends Critter { | |
public UglyCritter() { | |
super(); | |
} | |
public void passDay() { | |
changeAge(showAge() + 1); | |
super.action("Eat", 70, 4, 15); | |
super.action("Sleep", 50, 9, 2); | |
} | |
public String displayType() { | |
return "Ugly"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment