VisualContext Class Reference

#include <VisualContext.h>

Collaboration diagram for VisualContext:

Collaboration graph
[legend]

Public Member Functions

 VisualContext (World *w, int i, int j)
 Constructor.
virtual ~VisualContext ()
 Empty Destructor.
void setWorshiper (Worshiper *w)
 Set the Worshiper.
bool legal (int i, int j) const
 Determine if a pair of absolute coordinates is legal or note.
bool legal (std::pair< int, int > p) const
 Determine if a pair of absolute coordinates is legal or note.
bool legal (const MoveIterator &i) const
 Determine if a movement is legal or note.
std::vector< unsigned int > getPheromones (move_command cmd) const
 Get the understandable pheromones in a visible cell.
std::vector< unsigned int > getPheromones (const MoveIterator &i) const
 Get the understandable pheromones in a visible cell.
std::list< WorshiperInfogetWorshipers (move_command cmd) const
 Get the info about worshipers present in a cell.
std::list< WorshiperInfogetWorshipers (const MoveIterator &i) const
 Get the info about worshipers present in a cell.
bool haveFood (move_command cmd) const
 Detect food in a cell.
bool haveFood (const MoveIterator &i) const
 Detect food in a cell.
bool reachable (move_command cmd) const
 Detect if a cell is reachable.
bool reachable (const MoveIterator &i) const
 Detect if a cell is reachable.
move_command opposite (move_command cmd) const
 Give the opposite movement.
move_command opposite (const MoveIterator &i) const
 Give the opposite movement.
MoveIterator beginMove () const
 Give the beginning iterator to get the handled movements.
MoveIterator endMove () const
 Give the end iterator to get the handled movements.
unsigned int nbMovements () const
 Give the number of handled movements.

Protected Member Functions

CellgetCell (std::pair< int, int > p) const
 Return the cell given its coordinates.
CellgetCell (move_command cmd) const
 Return the cell from the wanted move.
CellgetCell (const MoveIterator &i) const
 Return the cell from the wanted move.
std::vector< unsigned int > getPheromones (Cell *c) const
 Get the understandable pheromones in a visible cell.
std::list< WorshiperInfogetWorshipers (Cell *c) const
 Get the info about worshipers present in a cell.
bool haveFood (Cell *c) const
 Detect food in a cell.

Protected Attributes

World_world
Worshiper_worshiper
int _i
int _j

Friends

class World

Detailed Description

< The view of the World by the Worshipers

Definition at line 42 of file VisualContext.h.


Constructor & Destructor Documentation

VisualContext::VisualContext ( World w,
int  i,
int  j 
)

Constructor.

Parameters:
w the world
i the vertical coordinate
j the horizontal coordinate

Definition at line 46 of file VisualContext.cpp.

00046                                                     :
00047     _world(w), _i(i), _j(j), _worshiper(NULL) {
00048 
00049 }


Member Function Documentation

Cell * VisualContext::getCell ( std::pair< int, int >  p  )  const [protected]

Return the cell given its coordinates.

Parameters:
p pair of coordinates
Returns:
pointer to the cell at these coordinates

Definition at line 72 of file VisualContext.cpp.

References _world, World::getCell(), and legal().

Referenced by getCell(), getPheromones(), getWorshipers(), haveFood(), and reachable().

00072                                                      {
00073   Cell * res;
00074 
00075   if(legal(p)){
00076     res = _world->getCell(p.first, p.second);
00077   } else {
00078     throw "Illegal cell";
00079   }
00080 
00081   return res;
00082 }

Here is the call graph for this function:

Here is the caller graph for this function:

Cell * VisualContext::getCell ( move_command  cmd  )  const [protected]

Return the cell from the wanted move.

Parameters:
cmd wanted move
Returns:
pointer to the cell at these coordinates

Definition at line 84 of file VisualContext.cpp.

References _i, _j, _world, Move::getAbsCoords(), getCell(), and World::getMove().

00084                                                    {
00085   return getCell(_world->getMove()->getAbsCoords(cmd, _i, _j));
00086 }

Here is the call graph for this function:

Cell * VisualContext::getCell ( const MoveIterator i  )  const [protected]

Return the cell from the wanted move.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
pointer to the cell at these coordinates

Definition at line 88 of file VisualContext.cpp.

References _i, _j, and getCell().

00088                                                         {
00089   return getCell(i->getAbsCoords(_i, _j));
00090 }

Here is the call graph for this function:

vector< unsigned int > VisualContext::getPheromones ( Cell c  )  const [protected]

Get the understandable pheromones in a visible cell.

Return a vector of booleans. The case number i means that the pheromone number i is present in the case.

Parameters:
c pointer to the cell
Returns:
vector of booleans

Definition at line 92 of file VisualContext.cpp.

References _worshiper, Worshiper::getColony(), Cell::getPheromones(), and NUMBER_PHEROMONES.

Referenced by getPheromones(), Warrior::think(), and Explorer::think().

00092                                                                 {
00093   map<Pheromone *, unsigned int>* listPhero = c->getPheromones();
00094   vector<unsigned int> vect = vector<unsigned int>(NUMBER_PHEROMONES);
00095 
00096   Colony * colony = _worshiper->getColony();
00097 
00098   for(int i = 0; i < NUMBER_PHEROMONES; i++){
00099 
00100     map<Pheromone *, unsigned int>::iterator res =
00101         listPhero->find(colony->getPheromone(i));
00102 
00103     vect[i] = (res != listPhero->end() ? res->second : 0);
00104 
00105   }
00106 
00107   return vect;
00108 }

Here is the call graph for this function:

Here is the caller graph for this function:

list< WorshiperInfo > VisualContext::getWorshipers ( Cell c  )  const [protected]

Get the info about worshipers present in a cell.

Return a vector of WorshiperInfo (one per worshiper)

Parameters:
c pointer to the cell
Returns:
list of WorshiperInfo

Definition at line 168 of file VisualContext.cpp.

References _worshiper, Cell::getPeople(), WorshiperInfo::setWorshiperInfo(), and WorshiperInfo::setWorshiperRequest().

Referenced by getWorshipers(), Warrior::think(), and GeneticBehaviour::think().

00168                                                                {
00169   list<Worshiper*> * l = c->getPeople();
00170   list<WorshiperInfo> li;
00171 
00172   list<Worshiper*>::iterator end_l = l->end();
00173 
00174   for(list<Worshiper*>::iterator it_l = l->begin();
00175       it_l != end_l;
00176       ++it_l){
00177 
00178     //We create a WorshiperInfo for this Worshiper and add it to the list
00179     WorshiperInfo w;
00180     w.setWorshiperInfo(*it_l);
00181     w.setWorshiperRequest(_worshiper);
00182     li.push_back(w);
00183   }
00184 
00185   return li;
00186 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool VisualContext::haveFood ( Cell c  )  const [protected]

Detect food in a cell.

Parameters:
c pointer to the cell
Returns:
true if the cell contains food, false else

Definition at line 132 of file VisualContext.cpp.

References Cell::getFood().

Referenced by haveFood(), GeneticBehaviour::think(), and Explorer::think().

00132                                            {
00133   return (c->getFood() > 0);
00134 }

Here is the call graph for this function:

Here is the caller graph for this function:

void VisualContext::setWorshiper ( Worshiper w  ) 

Set the Worshiper.

Parameters:
w worshiper

Definition at line 55 of file VisualContext.cpp.

References _worshiper.

Referenced by Worshiper::setVisualContext().

00055                                               {
00056   _worshiper = w;
00057 }

Here is the caller graph for this function:

bool VisualContext::legal ( int  i,
int  j 
) const

Determine if a pair of absolute coordinates is legal or note.

Parameters:
i vertical coordinate
j horizontal coordinate
Returns:
true if they are legal, false else

Definition at line 59 of file VisualContext.cpp.

Referenced by getCell(), legal(), Warrior::think(), GeneticBehaviour::think(), and Explorer::think().

00059                                             {
00060   return (i > 0 && i < _world->_height - 1 &&
00061           j > 0 && j < _world->_width - 1);
00062 }

Here is the caller graph for this function:

bool VisualContext::legal ( std::pair< int, int >  p  )  const

Determine if a pair of absolute coordinates is legal or note.

Parameters:
p the pair of coordinates
Returns:
true if they are legal, false else

Definition at line 64 of file VisualContext.cpp.

References legal().

00064                                                   {
00065   return legal(p.first, p.second);
00066 }

Here is the call graph for this function:

bool VisualContext::legal ( const MoveIterator i  )  const

Determine if a movement is legal or note.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
true if they are legal, false else

Definition at line 68 of file VisualContext.cpp.

References _i, _j, and legal().

00068                                                      {
00069   return legal(i->getAbsCoords(_i, _j));
00070 }

Here is the call graph for this function:

vector< unsigned int > VisualContext::getPheromones ( move_command  cmd  )  const

Get the understandable pheromones in a visible cell.

Return a vector of booleans. The case number i means that the pheromone number i is present in the case.

Parameters:
cmd wanted move
Returns:
vector of booleans

Definition at line 110 of file VisualContext.cpp.

References _worshiper, getCell(), Worshiper::getColony(), Cell::getPheromones(), and NUMBER_PHEROMONES.

00110                                                                         {
00111   map<Pheromone *, unsigned int>* listPhero = getCell(cmd)->getPheromones();
00112   vector<unsigned int> vect = vector<unsigned int>(NUMBER_PHEROMONES);
00113 
00114   Colony * colony = _worshiper->getColony();
00115 
00116   for(int i = 0; i < NUMBER_PHEROMONES; i++){
00117 
00118     map<Pheromone *, unsigned int>::iterator res =
00119         listPhero->find(colony->getPheromone(i));
00120 
00121     vect[i] = (res != listPhero->end() ? res->second : 0);
00122 
00123   }
00124 
00125   return vect;
00126 }

Here is the call graph for this function:

vector< unsigned int > VisualContext::getPheromones ( const MoveIterator i  )  const

Get the understandable pheromones in a visible cell.

Return a vector of booleans. The case number i means that the pheromone number i is present in the case.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
vector of booleans

Definition at line 128 of file VisualContext.cpp.

References getCell(), and getPheromones().

00128                                                                              {
00129   return getPheromones(getCell(i));
00130 }

Here is the call graph for this function:

list< WorshiperInfo > VisualContext::getWorshipers ( move_command  cmd  )  const

Get the info about worshipers present in a cell.

Return a vector of WorshiperInfo (one per worshiper)

Parameters:
cmd wanted move
Returns:
list of WorshiperInfo

Definition at line 188 of file VisualContext.cpp.

References getCell(), and getWorshipers().

00188                                                                        {
00189   return getWorshipers(getCell(cmd));
00190 }

Here is the call graph for this function:

list< WorshiperInfo > VisualContext::getWorshipers ( const MoveIterator i  )  const

Get the info about worshipers present in a cell.

Return a vector of WorshiperInfo (one per worshiper)

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
list of WorshiperInfo

Definition at line 192 of file VisualContext.cpp.

References getCell(), and getWorshipers().

00192                                                                             {
00193   return getWorshipers(getCell(i));
00194 }

Here is the call graph for this function:

bool VisualContext::haveFood ( move_command  cmd  )  const

Detect food in a cell.

Parameters:
cmd wanted move
Returns:
true if the cell contains food, false else

Definition at line 136 of file VisualContext.cpp.

References getCell(), and haveFood().

00136                                                    {
00137   return haveFood(getCell(cmd));
00138 }

Here is the call graph for this function:

bool VisualContext::haveFood ( const MoveIterator i  )  const

Detect food in a cell.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
true if the cell contains food, false else

Definition at line 140 of file VisualContext.cpp.

References getCell(), and haveFood().

00140                                                         {
00141   return haveFood(getCell(i));
00142 }

Here is the call graph for this function:

bool VisualContext::reachable ( move_command  cmd  )  const

Detect if a cell is reachable.

Parameters:
cmd wanted move
Returns:
true if the cell is reachable, false else

Definition at line 144 of file VisualContext.cpp.

References _worshiper, getCell(), and Cell::reachable().

Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().

00144                                                     {
00145   bool res = false;
00146 
00147   try {
00148     res = getCell(cmd)->reachable(_worshiper);
00149   } catch(...) {
00150     res = false;
00151   }
00152 
00153   return res;
00154 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool VisualContext::reachable ( const MoveIterator i  )  const

Detect if a cell is reachable.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
true if the cell is reachable, false else

Definition at line 156 of file VisualContext.cpp.

References _worshiper, getCell(), and Cell::reachable().

00156                                                          {
00157   bool res = false;
00158 
00159   try {
00160     res = getCell(i)->reachable(_worshiper);
00161   } catch(...) {
00162     res = false;
00163   }
00164 
00165   return res;
00166 }

Here is the call graph for this function:

move_command VisualContext::opposite ( move_command  cmd  )  const

Give the opposite movement.

Parameters:
cmd the command
Returns:
the opposite movement

Definition at line 196 of file VisualContext.cpp.

References _world, World::getMove(), and Move::opposite().

00196                                                            {
00197   return _world->getMove()->opposite(cmd);
00198 }

Here is the call graph for this function:

move_command VisualContext::opposite ( const MoveIterator i  )  const

Give the opposite movement.

Parameters:
i iterator pointing to the *Move handling the wanted movement
Returns:
the opposite movement

Definition at line 200 of file VisualContext.cpp.

00200                                                                 {
00201   return i->getHandledOpposite();
00202 }

MoveIterator VisualContext::beginMove (  )  const

Give the beginning iterator to get the handled movements.

Returns:
iterator

Definition at line 204 of file VisualContext.cpp.

References _world, Move::begin(), and World::getMove().

Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().

00204                                             {
00205   return _world->getMove()->begin();
00206 }

Here is the call graph for this function:

Here is the caller graph for this function:

MoveIterator VisualContext::endMove (  )  const

Give the end iterator to get the handled movements.

Returns:
iterator

Definition at line 208 of file VisualContext.cpp.

References _world, Move::end(), and World::getMove().

Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().

00208                                           {
00209   return _world->getMove()->end();
00210 }

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int VisualContext::nbMovements (  )  const

Give the number of handled movements.

Returns:
number of handled movements

Definition at line 212 of file VisualContext.cpp.

References _world, World::getMove(), and Move::size().

Referenced by Behaviour::randomMove().

00212                                               {
00213   return _world->getMove()->size();
00214 }

Here is the call graph for this function:

Here is the caller graph for this function:


Field Documentation

World* VisualContext::_world [protected]

The world

Definition at line 49 of file VisualContext.h.

Referenced by beginMove(), endMove(), getCell(), nbMovements(), and opposite().

Worshiper* VisualContext::_worshiper [protected]

The Worshiper

Definition at line 52 of file VisualContext.h.

Referenced by getPheromones(), getWorshipers(), reachable(), and setWorshiper().

int VisualContext::_i [protected]

z position

Definition at line 55 of file VisualContext.h.

Referenced by getCell(), legal(), and World::makeCommand().

int VisualContext::_j [protected]

x position

Definition at line 58 of file VisualContext.h.

Referenced by getCell(), legal(), and World::makeCommand().


The documentation for this class was generated from the following files:
Generated on Sat Feb 2 22:23:26 2008 for Teapot Colony Wars by  doxygen 1.5.4