Advertisement
otisphat80

Disappearing data

Jul 15th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.84 KB | None | 0 0
  1. #ifndef ORGANSIM_H
  2. #define ORGANSIM_H
  3.  
  4. #pragma once
  5.  
  6. #include "location.h"
  7. #include <iostream>
  8.  
  9. namespace DoodleBugGameOrtell
  10. {
  11.     class Organism 
  12.     {
  13.     public:
  14.         Organism( );
  15.         Organism(Location postion, char species);
  16.         Organism(const Organism& other);
  17.         Organism& operator=(const Organism& other);
  18.         void set_location(Location new_location);
  19.         virtual Location& get_location( );
  20.         virtual char get_species( );
  21.  
  22.     private:   
  23.         Location m_points;
  24.         char m_species;
  25.     };
  26. }//DoodleBugGameOrtell
  27. #endif // ORGANSIM_H
  28.  
  29.  
  30. #include "stdafx.h"
  31. #include "Organism.h"
  32.  
  33. namespace DoodleBugGameOrtell
  34. {
  35.     Organism::Organism( )
  36.     {  }
  37.  
  38.     Organism::Organism(     Location points,
  39.                     char species):
  40.                     m_points(points),
  41.                     m_species(species)
  42.     {  }
  43.  
  44.     Organism::Organism(const Organism& other)
  45.     {
  46.         this->m_points = other.m_points;
  47.         this->m_species = other.m_species;
  48.     }
  49.  
  50.     Organism& Organism::operator=(const Organism& other)
  51.     {
  52.         this->m_points = other.m_points;
  53.         this->m_species = other.m_species;
  54.         return *this;
  55.     }
  56.  
  57.     char Organism::get_species( )
  58.     {
  59.         return m_species;
  60.     }
  61.    
  62.     Location& Organism::get_location( )
  63.     {
  64.         return m_points;
  65.     }
  66.  
  67.     void Organism::set_location(Location new_location) //validate x and y are >= 0 && <= 20
  68.     {
  69.         ((new_location.get_xpoint( ) - 1) >= 0 && (new_location.get_xpoint( ) + 1) <= 19) ?
  70.             m_points.set_xpoint(new_location.get_xpoint( )) : m_points.set_xpoint(m_points.get_xpoint( ));
  71.         ((new_location.get_ypoint( ) - 1) >= 0 && (new_location.get_ypoint( ) + 1) <= 19) ?
  72.             m_points.set_ypoint(new_location.get_ypoint( )) : m_points.set_ypoint(m_points.get_ypoint( ));
  73.     }
  74. }//DoodleBugsGameOrtell
  75.  
  76.  
  77. #ifndef ANT_H
  78. #define ANT_H
  79.  
  80. #pragma once
  81.  
  82. #include "Organism.h"
  83.  
  84. namespace DoodleBugGameOrtell
  85. {
  86.     class Ant : public Organism
  87.     {
  88.     public:
  89.         Ant( );
  90.         Ant(Location postion, char species, int steps_till_breed);
  91.         char get_species( );
  92.         Location& get_location( );
  93.         void set_steps_till_breed(const int count);
  94.         int get_steps_till_breed( ) const;
  95.        
  96.     private:
  97.         int m_steps_till_breed;
  98.     };
  99. } // DoodleBugGameOrtell
  100. #endif //ANT_H
  101.  
  102.  
  103. #include "stdafx.h"
  104. #include "Ant.h"
  105. #include <cstdlib>
  106.  
  107. namespace DoodleBugGameOrtell
  108. {
  109.     Ant::Ant( )
  110.     {  }
  111.  
  112.     Ant::Ant(   Location points,
  113.                 char species,
  114.                 int steps_till_breed):
  115.                 Organism(points, species),
  116.                 m_steps_till_breed(steps_till_breed)
  117.     {  }
  118.  
  119.     char Ant::get_species( )
  120.     {
  121.         return 'A';
  122.     }
  123.  
  124.     Location& Ant::get_location( )
  125.     {
  126.         return Organism::get_location( );
  127.     }
  128.  
  129.     void Ant::set_steps_till_breed(const int count)
  130.     {
  131.         m_steps_till_breed = count;  
  132.     }
  133.  
  134.     int Ant::get_steps_till_breed( ) const
  135.     {
  136.         return m_steps_till_breed;
  137.     }
  138. }//DoodleBugGameOrtell
  139.  
  140.  
  141. #ifndef DOODLEBUG_H
  142. #define DOODLEBUG_H
  143.  
  144. #pragma once
  145.  
  146. #include "Organism.h"
  147.  
  148. namespace DoodleBugGameOrtell
  149. {
  150.     class DoodleBug : public Organism
  151.     {
  152.     public:
  153.         DoodleBug( );
  154.         DoodleBug(Location postion, char species, int steps_till_breed, int steps_till_death);
  155.         char get_species( );
  156.         Location& get_location( );
  157.         void set_steps_till_breed(const int count);
  158.         void set_steps_till_death(const int count);
  159.         int get_steps_till_breed( ) const;
  160.         int get_steps_till_death( ) const;
  161.  
  162.     private:
  163.         int m_steps_till_breed;
  164.         int m_steps_till_death;
  165.     };
  166. } // DoodleBugGameOrtell
  167. #endif //DOODLEBUG_H
  168.  
  169.  
  170. #include "stdafx.h"
  171. #include "Doodlebug.h"
  172.  
  173. namespace DoodleBugGameOrtell
  174. {
  175.     DoodleBug::DoodleBug( )
  176.     {  }
  177.  
  178.     DoodleBug::DoodleBug(   Location postion,
  179.                 char species,
  180.                 int till_breed,    
  181.                 int till_death):
  182.                 Organism(postion, species),
  183.                 m_steps_till_breed(till_breed),
  184.                 m_steps_till_death(till_death)
  185.     {  }
  186.  
  187.     char DoodleBug::get_species( )
  188.     {
  189.         return 'D';
  190.     }
  191.  
  192.     Location& DoodleBug::get_location( )
  193.     {
  194.         return Organism::get_location( );
  195.     }
  196.  
  197.     void DoodleBug::set_steps_till_breed(const int count)
  198.     {
  199.         m_steps_till_breed = count;
  200.     }
  201.  
  202.     void DoodleBug::set_steps_till_death(const int count)
  203.     {
  204.         m_steps_till_death = count;
  205.     }
  206.  
  207.     int DoodleBug::get_steps_till_breed( ) const
  208.     {
  209.         return m_steps_till_breed;
  210.     }
  211.  
  212.     int DoodleBug::get_steps_till_death( ) const
  213.     {
  214.         return m_steps_till_death;
  215.     }
  216. }//DoodlebugGameOrtell
  217.  
  218.  
  219. #ifndef GAMEBOARD_H
  220. #define GAMEBOARD_H
  221.  
  222. #pragma once
  223.  
  224. #include <iostream>
  225. #include "Location.h"
  226. #include "Constants.h"
  227. #include "Ant.h"
  228. #include "DoodleBug.h"
  229.  
  230. namespace DoodleBugGameOrtell
  231. {
  232.     class GameBoard
  233.     {
  234.     public:
  235.         GameBoard( );
  236.         Location check_for_empty_cell(Location& current_cell);
  237.         Location check_for_ant_cell(Location& current_cell);
  238.         Location pick_random_surrounding_cell(Location& current_cell);
  239.         char get_contents_of_chosen_cell(Location& chosen_cell);
  240.         void remove_organism_from_gameBoard(Location& cell);
  241.         void add_ant_to_gameBoard(Ant& ant);
  242.         void add_doodleBug_to_gameBoard(DoodleBug& player);
  243.  
  244.     private:
  245.         Organism m_gameBoard[ROWS][COLUMNS];
  246.     };
  247. }//DoodleBugGameOrtell
  248. #endif //GAMEBOARD_H
  249.  
  250.  
  251. #include "stdafx.h"
  252. #include "GameBoard.h"
  253.  
  254. namespace DoodleBugGameOrtell
  255. {
  256.     GameBoard::GameBoard( )   // populates gameBoard with empty organisms
  257.     {
  258.         for(int i =0; i < ROWS; i++)
  259.             for(int j =0; j < COLUMNS; j++)
  260.                 m_gameBoard[i][j] = Organism(Location(i, j),'*');  
  261.     }
  262.        
  263.     void GameBoard::remove_organism_from_gameBoard(Location& cell)
  264.     {
  265.         m_gameBoard[cell.get_xpoint( )][cell.get_ypoint( )] = Organism(cell, '*');
  266.     }
  267.  
  268.     void GameBoard::add_ant_to_gameBoard(Ant& ant)
  269.     {
  270.         m_gameBoard[ant.get_location( ).get_xpoint( )][ant.get_location( ).get_ypoint( )] = ant;
  271.     }
  272.  
  273.     void GameBoard::add_doodleBug_to_gameBoard(DoodleBug& doodleBug)
  274.     {
  275.         m_gameBoard[doodleBug.get_location( ).get_xpoint( )][doodleBug.get_location( ).get_ypoint( )] = doodleBug;
  276.     }
  277.    
  278.     Location GameBoard::pick_random_surrounding_cell(Location& current_cell)
  279.     {
  280.         switch(rand( ) % 4)
  281.         {
  282.         case 0: //<--- UP
  283.             if(current_cell.get_xpoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE )
  284.                 return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());
  285.  
  286.         case 1: //<--- DOWN
  287.             if (current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE)
  288.                 return Location((current_cell.get_xpoint( )+1), current_cell.get_ypoint( ));
  289.  
  290.         case 2: //<--- LEFT
  291.             if (current_cell.get_ypoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE)
  292.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )-1);
  293.  
  294.         case 3: //<--- RIGHT
  295.             if(current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE)
  296.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )+1);
  297.         }
  298.         return current_cell;
  299.     }
  300.  
  301.     char GameBoard::get_contents_of_chosen_cell(Location& chosen_cell)
  302.     {
  303.         return m_gameBoard[chosen_cell.get_xpoint( )][chosen_cell.get_ypoint( )].get_species( );
  304.     }
  305.  
  306.     Location GameBoard::check_for_empty_cell(Location& current_cell)
  307.     {
  308.         if(current_cell.get_xpoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE) //<--- Up
  309.             if(m_gameBoard[current_cell.get_xpoint( )-1][current_cell.get_ypoint( )].get_species( ) == '*')
  310.                 return Location((current_cell.get_xpoint( )-1), current_cell.get_ypoint( ));
  311.  
  312.         if(current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE) //<--- Down
  313.             if(m_gameBoard[current_cell.get_xpoint( )+1][current_cell.get_ypoint( )].get_species( ) == '*')
  314.                 return Location((current_cell.get_xpoint( )+1), current_cell.get_ypoint( ));
  315.  
  316.         if(current_cell.get_ypoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE) //<--- Left
  317.             if(m_gameBoard[current_cell.get_xpoint( )][current_cell.get_ypoint( )-1].get_species( ) == '*')
  318.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )-1);
  319.  
  320.         if(current_cell.get_ypoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE) //<--- Right
  321.             if(m_gameBoard[current_cell.get_xpoint( )][current_cell.get_ypoint( )+1].get_species( ) == '*')
  322.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )+1);
  323.  
  324.         return current_cell; // All are Occupied
  325.     }
  326.  
  327.     Location GameBoard::check_for_ant_cell(Location& current_cell)
  328.     {
  329.         if(current_cell.get_xpoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE) //<--- Up
  330.             if(m_gameBoard[current_cell.get_xpoint( )-1][current_cell.get_ypoint( )].get_species( ) == 'A')
  331.                 return Location((current_cell.get_xpoint( )-1), current_cell.get_ypoint( ));
  332.  
  333.         if(current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE) //<--- Down
  334.             if(m_gameBoard[current_cell.get_xpoint( )+1][current_cell.get_ypoint( )].get_species( ) == 'A')
  335.                 return Location((current_cell.get_xpoint( )+1), current_cell.get_ypoint( ));
  336.  
  337.         if(current_cell.get_ypoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE) //<--- Left
  338.             if(m_gameBoard[current_cell.get_xpoint( )][current_cell.get_ypoint( )-1].get_species( ) == 'A')
  339.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )-1);
  340.  
  341.         if(current_cell.get_ypoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE) //<--- Right
  342.             if(m_gameBoard[current_cell.get_xpoint( )][current_cell.get_ypoint( )+1].get_species( ) == 'A')
  343.                 return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )+1);
  344.  
  345.         return current_cell; // All are Occupied
  346.     }
  347.    
  348. }//DoodleBugGameOrtell
  349.  
  350.  
  351. #ifndef GAMEENGINE_H
  352. #define GAMEENGINE_H
  353.  
  354. #pragma once
  355.  
  356. #include <vector>
  357. #include "Ant.h"
  358. #include "DoodleBug.h"
  359. #include "GameBoard.h"
  360. #include "Constants.h"
  361.  
  362. namespace DoodleBugGameOrtell
  363. {
  364.     class GameEngine
  365.     {
  366.     public:
  367.         GameEngine( );
  368.         Ant& get_ant_from_ants(int index);
  369.         DoodleBug& get_doodleBug_from_doodlebugs(int index);
  370.         void move_ant(Ant& ant_2_move);
  371.         void breed_ant(Ant& ant_2_move);
  372.         void move_doodleBug(DoodleBug& doodleBug_2_move);
  373.         void eat_ant(Location& location_of_ant_2_eat);
  374.         void breed_doodleBug(DoodleBug& doodleBug_2_breed);
  375.         void kill_doodlebug(DoodleBug& doodleBug_2_kill);
  376.         void populate_gameBoard( );
  377.         void draw_gameBoard_2_console( );
  378.         int get_number_of_ants( ) const;
  379.         int get_number_of_doodleBugs( ) const;
  380.         void set_number_of_ants(const int number_of_ants);
  381.         void set_number_of_doodleBugs(const int number_of_doodleBugs);
  382.  
  383.  
  384.     private:
  385.         int m_number_of_ants;
  386.         int m_number_of_doodleBugs;
  387.         GameBoard m_gameBoard;
  388.         Ant m_ants[SIZE_OF_ANTS];
  389.         DoodleBug m_doodleBugs[SIZE_OF_DOODLEBUGS];
  390.     };
  391. }//DoodlebugGameOrtell
  392. #endif //GAMEENGINE_H
  393.  
  394.  
  395. #include "stdafx.h"
  396. #include "GameEngine.h"
  397.  
  398. namespace DoodleBugGameOrtell
  399. {
  400.     GameEngine::GameEngine( )
  401.     {
  402.         m_number_of_ants =0;
  403.         m_number_of_doodleBugs =0;
  404.  
  405.         populate_gameBoard( );
  406.     }
  407.  
  408.     int GameEngine::get_number_of_ants( ) const
  409.     {
  410.         return m_number_of_ants;
  411.     }
  412.  
  413.     int GameEngine::get_number_of_doodleBugs( ) const
  414.     {
  415.         return m_number_of_doodleBugs;
  416.     }
  417.  
  418.     void GameEngine::set_number_of_ants(const int number_of_ants)
  419.     {
  420.         m_number_of_ants = number_of_ants;
  421.     }
  422.  
  423.     void GameEngine::set_number_of_doodleBugs(const int number_of_doodleBugs)
  424.     {
  425.         m_number_of_doodleBugs = number_of_doodleBugs;
  426.     }
  427.  
  428.     Ant& GameEngine::get_ant_from_ants(int index)
  429.     {
  430.         return m_ants[index];
  431.     }
  432.    
  433.     DoodleBug& GameEngine::get_doodleBug_from_doodlebugs(int index)
  434.     {
  435.         return m_doodleBugs[index];
  436.     }
  437.  
  438.     void GameEngine::populate_gameBoard( )
  439.     {
  440.         int count =0;
  441.         while(m_number_of_ants < 100)
  442.         {
  443.             int x = rand( ) % 20;
  444.             int y = rand( ) % 20;
  445.  
  446.             if (m_gameBoard.get_contents_of_chosen_cell(Location(x, y)) == '*')
  447.             {
  448.                 m_ants[count] = (Ant(Location(x, y), 'A', 0));
  449.                 m_gameBoard.add_ant_to_gameBoard(Ant(Location(x, y), 'A', 0));
  450.                 m_number_of_ants++;
  451.                 count++;
  452.             }
  453.         }
  454.         count =0;
  455.         while (m_number_of_doodleBugs < 5)
  456.         {
  457.             int x = rand( ) % 20;
  458.             int y = rand( ) % 20;
  459.  
  460.             if (m_gameBoard.get_contents_of_chosen_cell(Location(x, y)) == '*')
  461.             {
  462.                 m_doodleBugs[count] = (DoodleBug(Location(x, y), 'D', 0, 0));
  463.                 m_gameBoard.add_doodleBug_to_gameBoard(DoodleBug(Location(x, y), 'D', 0, 0));
  464.                 m_number_of_doodleBugs++;
  465.                 count++;
  466.             }
  467.         }
  468.     }
  469.  
  470.     void GameEngine::move_ant(Ant& ant_2_move)
  471.     {
  472.         Location desired_cell = m_gameBoard.pick_random_surrounding_cell(ant_2_move.get_location( ));
  473.  
  474.         if (m_gameBoard.get_contents_of_chosen_cell(desired_cell) == '*')
  475.         {
  476.             m_gameBoard.remove_organism_from_gameBoard(ant_2_move.get_location( ));  
  477.             ant_2_move.set_location(desired_cell);
  478.             m_gameBoard.add_ant_to_gameBoard(ant_2_move);
  479.  
  480.             (ant_2_move.get_steps_till_breed( ) == 3) ? breed_ant(ant_2_move) :
  481.                 ant_2_move.set_steps_till_breed(ant_2_move.get_steps_till_breed( )+1);
  482.         }
  483.     }    
  484.  
  485.     void GameEngine::breed_ant(Ant& ant_2_move)
  486.     {
  487.         Location spawns_cell = m_gameBoard.check_for_empty_cell(ant_2_move.get_location( ));
  488.  
  489.         if (spawns_cell != ant_2_move.get_location( ))
  490.         {
  491.             m_ants[m_number_of_ants] = (Ant(spawns_cell, 'A', 0));
  492.             m_gameBoard.add_ant_to_gameBoard(Ant(spawns_cell, 'A', 0));
  493.             ant_2_move.set_steps_till_breed(0);
  494.             m_number_of_ants++;
  495.         }
  496.     }
  497.  
  498.     void GameEngine::move_doodleBug(DoodleBug& doodleBug_2_move)
  499.     {
  500.         Location desired_cell = m_gameBoard.check_for_ant_cell(doodleBug_2_move.get_location( ));
  501.  
  502.         if (m_gameBoard.get_contents_of_chosen_cell(desired_cell) == 'A')
  503.         {
  504.             m_gameBoard.remove_organism_from_gameBoard(desired_cell);
  505.             m_gameBoard.remove_organism_from_gameBoard(doodleBug_2_move.get_location());
  506.  
  507.             doodleBug_2_move.set_location(desired_cell);
  508.             m_gameBoard.add_doodleBug_to_gameBoard(doodleBug_2_move);
  509.             m_number_of_ants--;
  510.  
  511.             doodleBug_2_move.set_steps_till_death(0);
  512.  
  513.             (doodleBug_2_move.get_steps_till_breed( ) == 8) ? breed_doodleBug(doodleBug_2_move) :
  514.                 doodleBug_2_move.set_steps_till_breed(doodleBug_2_move.get_steps_till_breed( )+1);
  515.            
  516.         }
  517.         else
  518.         {
  519.             desired_cell = m_gameBoard.check_for_empty_cell(doodleBug_2_move.get_location( ));
  520.  
  521.             if (desired_cell != doodleBug_2_move.get_location( ))
  522.             {
  523.                 m_gameBoard.remove_organism_from_gameBoard(doodleBug_2_move.get_location( ));
  524.                 doodleBug_2_move.set_location(desired_cell);
  525.                 m_gameBoard.add_doodleBug_to_gameBoard(doodleBug_2_move);
  526.                
  527.                 (doodleBug_2_move.get_steps_till_death( ) == 3) ? kill_doodlebug(doodleBug_2_move) :
  528.                     doodleBug_2_move.set_steps_till_death(doodleBug_2_move.get_steps_till_death( )+1);
  529.  
  530.                 (doodleBug_2_move.get_steps_till_breed( ) == 8) ? breed_doodleBug(doodleBug_2_move) :
  531.                     doodleBug_2_move.set_steps_till_breed(doodleBug_2_move.get_steps_till_breed( )+1);
  532.                
  533.             }
  534.         }
  535.        
  536.     }
  537.  
  538.     void GameEngine::breed_doodleBug(DoodleBug& doodleBug_2_breed)
  539.     {
  540.         Location spawns_cell = m_gameBoard.check_for_empty_cell(doodleBug_2_breed.get_location( ));
  541.  
  542.         if (spawns_cell != doodleBug_2_breed.get_location( ))
  543.         {
  544.             m_doodleBugs[m_number_of_doodleBugs] = (DoodleBug(spawns_cell, 'D', 0, 0));
  545.             m_gameBoard.add_doodleBug_to_gameBoard(DoodleBug(spawns_cell, 'D', 0, 0));
  546.             doodleBug_2_breed.set_steps_till_breed(0);
  547.             doodleBug_2_breed.set_steps_till_death(0);
  548.             m_number_of_doodleBugs++;
  549.         }
  550.     }
  551.    
  552.     void GameEngine::kill_doodlebug(DoodleBug& doodleBug_2_kill)
  553.     {
  554.         m_gameBoard.remove_organism_from_gameBoard(doodleBug_2_kill.get_location());
  555.     }
  556.  
  557.     void GameEngine::draw_gameBoard_2_console( )
  558.     {
  559.         for(int i =0; i < 20; i++)
  560.         {
  561.             for(int j =0; j < 20; j++)
  562.             {
  563.                 std::cout << m_gameBoard.get_contents_of_chosen_cell(Location(i, j)) << " ";   
  564.             }
  565.             std::cout << std::endl;
  566.         }
  567.     }
  568. }//DoodlebugGameOrtell
  569.  
  570.  
  571. #ifndef CONSTANTS_H
  572. #define CONSTANTS_H
  573.  
  574. namespace DoodleBugGameOrtell
  575. {
  576.     const int ROWS = 20;
  577.     const int COLUMNS = 20;
  578.     const int SIZE_OF_ANTS =400;
  579.     const int SIZE_OF_DOODLEBUGS =200;
  580.     const int GAMEBOARD_EDGE_HIGH_SIDE = 19;
  581.     const int GAMEBOARD_EDGE_BOTTOM_SIDE =0;
  582.  
  583. }//DoodleBugGameOrtell
  584. #endif //CONSTANTS_H
  585.  
  586.  
  587. #include "stdafx.h"
  588. #include <Windows.h>
  589. #include "GameEngine.h"
  590.  
  591. using namespace DoodleBugGameOrtell;
  592.  
  593. int _tmain(int argc, _TCHAR* argv[])
  594. {
  595.     GameEngine test_engine;
  596.    
  597.     test_engine.draw_gameBoard_2_console( );
  598.    
  599.     int count =0;
  600.     while (true)
  601.     {
  602.         for (int i =0; i < test_engine.get_number_of_ants( ); i++)
  603.         {
  604.             test_engine.move_ant(test_engine.get_ant_from_ants(i));
  605.         }
  606.         for (int i =0; i < test_engine.get_number_of_doodleBugs( ); i++)
  607.         {
  608.             test_engine.move_doodleBug(test_engine.get_doodleBug_from_doodlebugs(i));
  609.             std::cout << "DoodleBugs Loop  ran " <<  i << std::endl;
  610.         }
  611.  
  612.        
  613.  
  614.         std::cout << std::endl;
  615.         std::cout  << "Outer Loop " <<  count   << std::endl;
  616.         std::cout << "Ants: "<< test_engine.get_number_of_ants( )  << " DoodleBugs: "
  617.             << test_engine.get_number_of_doodleBugs( ) << std::endl;
  618.         std::cout << std::endl;
  619.         test_engine.draw_gameBoard_2_console( );
  620.         Sleep(1000);
  621.         count++;
  622.     }
  623.     system("PAUSE");
  624.     return 0;
  625. }
  626.  
  627. /*  The goal for this programming project is to create a simple 2D predator–prey
  628.     simulation. In this simulation, the prey are ants and the predators are doodlebugs.
  629.     These critters live in a 20 * 20 grid of cells. Only one critter may occupy a cell
  630.     at a time. The grid is enclosed, so a critter is not allowed to move off the edges
  631.     of the world. Time is simulated in steps. Each critter performs some action every
  632.     time step.
  633.    
  634.     The ants behave according to the following model:
  635.     ? Move . For every time step, the ants randomly try to move up, down, left, or
  636.     right. If the neighboring cell in the selected direction is occupied or would move
  637.     the ant off the grid, then the ant stays in the current cell.
  638.     ? Breed . If an ant survives for three time steps, at the end of the time step (i.e., after
  639.     moving) the ant will breed. This is simulated by creating a new ant in an adjacent
  640.     (up, down, left, or right) cell that is empty. If there is no empty cell available,
  641.     no breeding occurs. Once an offspring is produced, an ant cannot produce an
  642.     offspring again until it has survived three more time steps.
  643.    
  644.     The doodlebugs behave according to the following model:
  645.     ? Move . For every time step, the doodlebug will move to an adjacent cell containing
  646.     an ant and eat the ant. If there are no ants in adjoining cells, the doodlebug
  647.     moves according to the same rules as the ant. Note that a doodlebug cannot eat
  648.     other doodlebugs.
  649.     ? Breed . If a doodlebug survives for eight time steps, at the end of the time step it
  650.     will spawn off a new doodlebug in the same manner as the ant.
  651.     ? Starve . If a doodlebug has not eaten an ant within three time steps, at the end of
  652.     the third time step it will starve and die. The doodlebug should then be removed
  653.     from the grid of cells.
  654.     During one turn, all the doodlebugs should move before the ants.
  655.     Write a program to implement this simulation and draw the world using ASCII
  656.     characters of “O” for an ant and “X” for a doodlebug. Create a class named
  657.     Organism that encapsulates basic data common to ants and doodlebugs. This
  658.     class should have a virtual function named move that is defined in the derived
  659.     classes of Ant and  Doodlebug . You may need additional data structures to keep
  660.     track of which critters have moved.
  661.  
  662.     Initialize the world with 5 doodlebugs and 100 ants. After each time step, prompt
  663.     the user to press Enter to move to the next time step. You should see a cyclical
  664.     pattern between the population of predators and prey, although random perturba-
  665.     tions may lead to the elimination of one or both species.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement