import java.util.ArrayList; import java.util.Random; public class Lab03 { public static void main(String[] args) { Random r = new Random(); /* Create a list of 10 fish. Don't worry about what this actually is right now. */ ArrayList fish = new ArrayList(10); /* Add all of our fishies to the "tank" */ fish.add(new Shark("Sharky")); fish.add(new Blowfish("Fuji")); fish.add(new Fish("Nemo", r.nextInt(10) + 5)); fish.add(new Fish("Berty", r.nextInt(10) + 5)); fish.add(new Fish("Babel", r.nextInt(10) + 5)); fish.add(new Fish("Eiji", r.nextInt(10) + 5)); fish.add(new Fish("Gilbert", r.nextInt(10) + 5)); fish.add(new Fish("Remy", r.nextInt(10) + 5)); fish.add(new Fish("Louie", r.nextInt(10) + 5)); fish.add(new Fish("Otto", r.nextInt(10) + 5)); /* Keep looping through rounds until there's only one fish left. */ for (int round = 1; fish.size() > 1; round++) { System.out.println("Round " + round); for (int i = 0; i < fish.size(); i++) { /* Pick a random fish to try and eat */ int idx = r.nextInt(fish.size()); Fish eater = fish.get(i); Fish eatee = fish.get(idx); /* Try to eat the fish if it's not itself */ if (idx != i) { boolean eaten = eater.eat(eatee); if (eaten && eater.getIsAlive()) { System.out.println(eater + " consumed " + eatee + ". How tasty!"); } else if (eaten) { System.out.println(eater + " ate " + eatee + ", but died of food poisoning."); } else { System.out.println(eater + " just tried to eat " + eatee + ", who was too big!"); } } else { System.out.println(eater + " just tried to eat itself. How weird!"); } } for (int i = 0; i < fish.size(); i++) { if (!fish.get(i).getIsAlive()) { fish.remove(i); i--; } } System.out.println(); } } }