My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Darwin_Description  

Darwin

Our assignment was to write a program to simulate Darwin's World. Darwin's World contains a two-dimensional grid. In this grid, creatures roam around, executing their program. The order of the creatures turns is based on their position in the grid, top down left to right. The program the creatures execute is based on their species. The action commands for the creatures are hop, infect, left, and right. The control commands are if_empty, if_wall, if_enemy, if_random, and go. The goal in Darwin's World is to design a species capable of infecting every of the species using the action and control commands.

Implementation

Overview of primary object design and data structures are as follows;

The primary classes are World, Grid, Species and Creatures. World is the outer most container holding members of Grid, Species and Creatures. There is only one world and the world has a single grid. The grid is a 2D array containing Creatures (pointers) in those coordinates where a creature exists. So a grid will hold typically many creatures. Each creature is of a particular species and holds a pointer to it's particular flavor of species.

The World calls methods to initialize and populate the grid with creatures of various species. Then the World tells each creature in turn to execute it's instruction set up to one action at a time. Each creature object holds the interpreter for the simple creature language and executes it's program instructions. Creatures ask the grid about their environment with commands like if_enemy, if_empty, if_wall. The grid responds accordingly to such requests by direct examination. Similarly when a creature wants to hop it asks the grid to perform that action. For infect a creature invokes the infectMe method on an enemy creature directly by via the grid.

Each Creature instance is held in an stl deque. deque was chosen specifically so that Creatures remain persistent throughout the life of the program and allow us to use pointers to Creatures in other places. Namely, the turnlist and the Grid.

The Grid is a 2D array (vectors) of Creature pointers. If a grid element is null then it is empty. Otherwise it's a pointer to the creature that lives in that one and only place.

The turnlist is simply a vector of pointers to all the creatures. This choice allowed the use of the stl sort function to sort the turnlist before each turn.

Species (programs) are maintained in an stl map keyed by species name.

World

The World class runs the simulation, prints the state of the simulation, and tracks the grid, species, and creatures in the simulation.

  • The createWorld method generates a new world for the simulation. The grid is recreated using the row and column parameters and the creatures deque and turnlist vector are cleared.
  • The speciation method generates a new species from the species instruction data.
  • The giveBirth method either generates a specified number of random creatures or a creature with a specific state in the world
  • The run method executes the simulation for a specified number of turns and prints out the state of the simulation at a user-defined interval. In this method, the turn order of the creatures is determined by sorting the turnlist, a vector of creature pointers. We decided to maintain a sorted turnlist of creature pointers to reduce the time and space necessary to find the turn order of the creatures.
  • The census method calculates the number of creatures for each species in the simulation. In this way we can readily determine which creature won.

Grid

The Grid class is the interaction layer between the creatures in the simulation. The Grid class uses a two dimensional vector of Creature pointers to represent the simulation. This setup allows us to determine if a wall, an empty space, or an enemy is in front of a creature and to execute the hop and infect action moves quickly.

  • The createGrid method generates a new two dimensional grid using the row and column parameters.
  • The assign method places a Creature in a specified location in Grid.
  • The isOccupied method determines if the space in the Grid is empty.
  • The aheadIsWall method determines if the space in front of the creature is a wall.
  • The aheadIsEmpty method determines if the space in front of the creature is empty.
  • The aheadIsEnemy method determines if the space in front of the creature is an enemy.
  • The ahead method determines if the space in front of the creature is a valid move.
  • The hop method moves the creature forward in the grid and changes the creature's internal state.
  • The print method outputs the current state of the grid.

Species

The Species class is a container for the species instruction program. The Species class parses the species instruction program from an input string. Each Creature contains a species pointer that allows the Creature to get the correct instruction in the species program using the program counter.

Statement

A struct that represents the individual instructions in the species instruction program.

Creature

The Creature class represents the different creatures in the simulation. The creature's state holds the program counter, the creature's row and col position in the grid, the creature's direction, and the creature's species. Every turn the creature executes an action move based on the species instruction program. The interpreter for species instruction program is found in this class. The interpreter receives a statement from the species class based on the creature's program counter. The interpreter then executes the corresponding instruction by interpreting the statement.

Powered by Google Project Hosting