1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include "stdafx.h"
#include "GameBoard.h"
GameBoard::GameBoard( ) // populates gameboard with empty organisms
{
for(int i =0; i < 20; i++)
{
for(int j =0; j < 20; j++)
m_gameBoard[i][j] = Organism(Location(i, j), '*');
}
populate_gameBoard( ); // places ants and doodlebugs in vectors and on gameboard
}
void GameBoard::remove_organism_from_gameBoard(Location& cell)
{
m_gameBoard[cell.get_xpoint()][cell.get_ypoint()] = Organism(cell, '*');
}
void GameBoard::add_ant_to_gameBoard(Ant& ant)
{
m_gameBoard[ant.get_location().get_xpoint()][ant.get_location().get_ypoint()] = ant;
}
void GameBoard::add_doodleBug_to_gameBoard(DoodleBug& doodleBug)
{
m_gameBoard[doodleBug.get_location().get_xpoint()][doodleBug.get_location().get_ypoint()] = doodleBug;
}
Location GameBoard::pick_random_surrounding_cell(Location& current_cell)
{
switch(rand( ) % 4)
{
case 0: //<--- UP
if(current_cell.get_xpoint( ) > 0)
return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());
case 1: //<--- DOWN
if (current_cell.get_xpoint( ) < 19)
return Location((current_cell.get_xpoint()+1), current_cell.get_ypoint());
case 2: //<--- LEFT
if (current_cell.get_ypoint( ) > 0)
return Location((current_cell.get_xpoint()), current_cell.get_ypoint()-1);
case 3: //<--- RIGHT
if(current_cell.get_xpoint( ) < 19)
return Location((current_cell.get_xpoint()), current_cell.get_ypoint()+1);
}
return current_cell;
}
char GameBoard::get_contents_of_chosen_cell(Location chosen_cell) //removed reference
{
return m_gameBoard[chosen_cell.get_xpoint( )][chosen_cell.get_ypoint( )].get_species( );
}
Location GameBoard::check_for_empty_cell(Location& current_cell)
{
if(current_cell.get_xpoint() > 0) //
if(m_gameBoard[current_cell.get_xpoint()-1][current_cell.get_ypoint()].get_species() == '*')
return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());
if(current_cell.get_xpoint() < 19) //<--- Down
if(m_gameBoard[current_cell.get_xpoint()+1][current_cell.get_ypoint()].get_species() == '*')
return Location((current_cell.get_xpoint()+1), current_cell.get_ypoint());
if(current_cell.get_ypoint() > 0) //<--- Left
if(m_gameBoard[current_cell.get_xpoint()][current_cell.get_ypoint()-1].get_species() == '*')
return Location((current_cell.get_xpoint()), current_cell.get_ypoint()-1);
if(current_cell.get_ypoint() < 19) //<--- Right
if(m_gameBoard[current_cell.get_xpoint()][current_cell.get_ypoint()+1].get_species() == '*')
return Location((current_cell.get_xpoint()), current_cell.get_ypoint()+1);
return current_cell; // All are Occupied
}
void GameBoard::populate_gameBoard( )
{
while(m_ants.size() < 100)
{
int x = rand( ) % 20;
int y = rand( ) % 20;
if (get_contents_of_chosen_cell(Location(x, y)) == '*')
{
m_ants.push_back(Ant(Location(x, y), 'A', 0));
m_gameBoard[x][y] = (Ant(Location(x, y), 'A', 0));
}
}
while (m_doodleBugs.size() < 5)
{
int x = rand( ) % 20;
int y = rand( ) % 20;
if (get_contents_of_chosen_cell(Location(x, y)) == '*')
{
m_doodleBugs.push_back(DoodleBug(Location(x, y), 'D', 0, 0));
m_gameBoard[x][y] = (DoodleBug(Location(x, y), 'D', 0, 0));
}
}
}
void GameBoard::move_ant(Ant& ant_2_move)
{
Location desired_cell = pick_random_surrounding_cell(ant_2_move.get_location());
Location current_cell = ant_2_move.get_location();
if (get_contents_of_chosen_cell(desired_cell) == '*')
{
remove_organism_from_gameBoard(ant_2_move.get_location());
ant_2_move.set_location(desired_cell);
add_ant_to_gameBoard(ant_2_move);
(ant_2_move.get_steps_till_breed() == 3) ? breed_ant(ant_2_move) :
ant_2_move.set_steps_till_breed(ant_2_move.get_steps_till_breed()+1);
}
}
void GameBoard::breed_ant(Ant& ant_2_breed)
{
Location spawns_cell = check_for_empty_cell(ant_2_breed.get_location());
if (spawns_cell != ant_2_breed.get_location())
{
m_ants.push_back(Ant(spawns_cell, 'A', 0));
m_gameBoard[spawns_cell.get_xpoint()][spawns_cell.get_ypoint()]
= Ant(spawns_cell, 'A', 0);
ant_2_breed.set_steps_till_breed(0);
}
}
void GameBoard::draw_gameBoard_2_console( )
{
for(int i =0; i < 20; i++)
{
for(int j =0; j < 20; j++)
{
std::cout << get_contents_of_chosen_cell(Location(i, j)) << " ";
}
std::cout << std::endl;
}
}
int GameBoard::get_size_of_ants_vector( )
{
return m_ants.size();
}
int GameBoard::get_size_of_doodleBugs_vector( )
{
return m_doodleBugs.size();
}
Ant& GameBoard::get_ant_from_ants_vector(int index)
{
return m_ants[index];
}
DoodleBug& GameBoard::get_doodleBug_from_doodlebug_vector(int index)
{
return m_doodleBugs[index];
}
|