Virtual Life App with Carnivore
Feature
Specification
- Add carnivores to the world, to keep the animal population
in check!
- Carnivores will have a chance to move, reproduce or die.
- If a carnivore moves to a cell with an animal, that
animal will die.
- A carnivore cannot move to a cell occupied by a wall,
plant or another carnivore.
Code Design
- Create a CarnivoreWorldItem prototyped object that
derives from AnimalWorldItem.
- Change the WorldItemFactory to include the
carnivore.
- Make the symbol used for each world item configurable:
Use an itemOptions object
that will have a key for the world item type. See below for an example.
- The type will be wall, plant, animal, carnivore,
or empty.
- Add a symbol for each world item type.
- Add a buildChance to configure the chance for the
world plan to build that world item.
- What other configurable options may exist for the items,
which you could include here?
Do not use hard-coded symbols in the app,
only use the symbol from the itemOptions object!
- Pass the options object to the world plan builder and
world item factory.
- Beforehand you may have only used a single build function
in your WorldItemFactory which built WorldItems based on a given
item symbol, you will probably want to restructure this a bit so
that it allows building based on item type as well. However, you
will also want to retain the existing function which builds WorldItems
based on the symbol, since it will be useful in other contexts. For
example:
function WorldItemFactory(itemOptions) {
this.itemOptions = itemOptions;
// create symbol table for reverse lookups
this.symbolTable = {};
for (let type in itemOptions) {
let symbol = this.itemOptions[type].symbol;
this.symbolTable[symbol] = type;
}
}
WorldItemFactory.prototype.buildFromSymbol =
function(symbol,latitude,longitude) {
let type = this.symbolTable[symbol];
return this.build(type,latitude,longitude);
}
WorldItemFactory.prototype.build =
function(type,latitude,longitude) {
<snip… your implementation goes here>
}
- When you are finished, the load function in app.js should
look like the following. Notice that this will likely require some
refactoring of your code. The factory-style pattern used here is very
common in programming, and serves to separate concerns. The exact
structure of your options object may vary depending on your
implementation.
app.load = function() {
const ROWS = 40;
const COLS = 60;
var itemOptions = {
wall: { symbol: "#",
buildChance: 10 },
<snip… what else goes here?>
};
var worldPlan = buildWorldPlan(ROWS, COLS, itemOptions);
var worldItemFactory = new WorldItemFactory(itemOptions);
var world = new World(worldPlan,
worldItemFactory);
animateWorld(world)
}