/** Reset the simulation to a starting position. */ public void reset() { step = 0; populate(); // Show the starting state in the view. view.showStatus(step, field); }
/** * Run the simulation from its current state for a single step. Iterate over the whole field * updating the state of each fox and rabbit. */ public void simulateOneStep() { step++; // Provide space for newborn rabbits. List<Rabbit> newRabbits = new ArrayList<Rabbit>(); // Let all rabbits act. for (Iterator<Rabbit> it = rabbits.iterator(); it.hasNext(); ) { Rabbit rabbit = it.next(); rabbit.run(newRabbits); if (!rabbit.isAlive()) { it.remove(); } } // Provide space for newborn foxes. List<Fox> newFoxes = new ArrayList<Fox>(); // Let all foxes act. for (Iterator<Fox> it = foxes.iterator(); it.hasNext(); ) { Fox fox = it.next(); fox.hunt(newFoxes); if (!fox.isAlive()) { it.remove(); } } // Add the newly born foxes and rabbits to the main lists. rabbits.addAll(newRabbits); foxes.addAll(newFoxes); view.showStatus(step, field); }
/** Reset the simulation to a starting position. */ public void reset() { step = 0; rabbits.clear(); foxes.clear(); populate(); // Show the starting state in the view. view.showStatus(step, field); }
/** Initialize game. */ public void init() { // // Create characters // Human h1 = new Human("Human 1", HP_HUMANS); // Human h2 = new Human("Human 2", HP_HUMANS); // Vampire v1 = new Vampire("Vampire 1", HP_VAMPIRES); // Vampire v2 = new Vampire("Vampire 2", HP_VAMPIRES); // Zombie z1 = new Zombie("Zombie 1", HP_ZOMBIES); // MadZombie mz1 = new MadZombie("MadZombie 1", HP_ZOMBIES); // Add characters to the list characterList = new ArrayList<Character>(); // characterList.add(h1); // characterList.add(h2); // characterList.add(v1); // characterList.add(v2); // characterList.add(z1); // characterList.add(mz1); /*humans = new ArrayList<Human>(); zombies = new ArrayList<Zombie>(); vampires = new ArrayList<Vampire>(); madZombies = new ArrayList<MadZombie>();*/ // humans.add(h1); // humans.add(h2); // vampires.add(v1); // vampires.add(v2); // zombies.add(z1); // madZombies.add(mz1); shotguns = new ArrayList<ShotGun>(); nitrogens = new ArrayList<LiquidNitrogen>(); stakes = new ArrayList<WoodenStake>(); field = new Field(DEFAULT_DEPTH, DEFAULT_WIDTH); // Create a view of the state of each location in the field. view = new SimulatorView(DEFAULT_DEPTH, DEFAULT_WIDTH); view.setColor(Human.class, Color.orange); view.setColor(Zombie.class, Color.green); view.setColor(Vampire.class, Color.black); view.setColor(MadZombie.class, Color.red); view.setColor(BaseObject.class, Color.cyan); // Setup a valid starting point. reset(); }
/** * Create a simulation field with the given size. * * @param depth Depth of the field. Must be greater than zero. * @param width Width of the field. Must be greater than zero. */ public Simulator(int depth, int width) { if (width <= 0 || depth <= 0) { System.out.println("The dimensions must be greater than zero."); System.out.println("Using default values."); depth = DEFAULT_DEPTH; width = DEFAULT_WIDTH; } rabbits = new ArrayList<Rabbit>(); foxes = new ArrayList<Fox>(); field = new Field(depth, width); // Create a view of the state of each location in the field. view = new SimulatorView(depth, width); view.setColor(Rabbit.class, Color.orange); view.setColor(Fox.class, Color.blue); // Setup a valid starting point. reset(); }
/** * Run the simulation from its current state for a single step. Iterate over the whole field * updating the state of each fox and rabbit. */ public void simulateOneStep() { step++; // Provide space for newborn humans. List<Human> newHumans = new ArrayList<Human>(); // Provide space for newborn vampires. List<Vampire> newVampires = new ArrayList<Vampire>(); // Provide space for newborn wolfs. List<Zombie> newZombies = new ArrayList<Zombie>(); // Let all characters act. for (Iterator<Character> it = characterList.iterator(); it.hasNext(); ) { Character c = it.next(); if (c.getCharacter() == TypeCharacter.HUMAN) { Human h = (Human) c; h.run(newHumans); } if (c.getCharacter() == TypeCharacter.VAMPIRE) { Vampire v = (Vampire) c; v.hunt(newVampires); } if (c.getCharacter() == TypeCharacter.ZOMBIE) { Zombie z = (Zombie) c; z.hunt(newZombies); } if (c.getCharacter() == TypeCharacter.MADZOMBIE) { MadZombie mz = (MadZombie) c; mz.hunt(newZombies); } } // Add the newly born humans, vampires and zombies to the main lists. characterList.addAll(newHumans); characterList.addAll(newVampires); characterList.addAll(newZombies); view.showStatus(step, field); }
public void notifyViews() { if (views != null) for (SimulatorView view : views) view.updateView(this); }
public void addView(SimulatorView view) { views.add(view); if (view != null) view.updateView(this); }
/** * Run the simulation from its current state for the given number of steps. Stop before the given * number of steps if it ceases to be viable. * * @param numSteps The number of steps to run for. */ public void simulate(int numSteps) { for (int step = 1; step <= numSteps && view.isViable(field); step++) { simulateOneStep(); } }