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
|
#include <iostream>
#include <string>
#include <vector>
struct Rect
{
int x, y, w, h;
};
class House
{
std::string name;
Rect r;
public:
House(std::string const &name_arg,
Rect const &r_arg)
:
name(name_arg),
r(r_arg)
{}
void get_house_info(std::string &name_arg,
Rect &r_arg)
{
name_arg = name;
r_arg = r;
}
};
class Island
{
std::vector<House> houses_on_island;
public:
void add_house(std::string const &house_name, Rect const &r)
{
houses_on_island.push_back(House(house_name, r));
int house_count = houses_on_island.size();
if (house_count == 1)
std::cout << "There is now " << house_count << " house ";
else
std::cout << "There are now " << house_count << " houses ";
}
void get_house_info(int const house_number, std::string &house_name, Rect &r)
{
houses_on_island[house_number].get_house_info(house_name, r);
}
int get_house_count()
{
return houses_on_island.size();
}
};
// map is a keyword, so Map is now Region.
class Region
{
std::vector<Island> islands_in_region;
public:
void add_island()
{
islands_in_region.push_back(Island());
int island_count = islands_in_region.size();
if (island_count == 1)
std::cout << "There is now " << island_count << " island.\n";
else
std::cout << "There are now " << island_count << " islands.\n";
}
void add_house(int island_number, std::string const &house_name, Rect const &r)
{
int island_count = islands_in_region.size();
if (island_number > (island_count - 1))
std::cout << "That island doesn't exist, try again.\n";
else
{
islands_in_region[island_number].add_house(house_name, r);
std::cout << "located on island " << island_number << ".\n";
}
}
void get_house_info(int const island_number, int const house_number, std::string &house_name, Rect &r)
{
islands_in_region[island_number].get_house_info(house_number, house_name, r);
}
int get_island_count()
{
return islands_in_region.size();
}
int get_house_count(int island_number)
{
return islands_in_region[island_number].get_house_count();
}
};
class Draw_Something
{
public:
void draw_house(std::string const &house_name, Rect const &r)
{
std::cout << "House name: " << house_name << "\n";
std::cout << "Drawing house with properties: " << r.x << " " << r.y
<< " " << r.w << " " << r.h << "\n\n";
}
};
int main()
{
Region region;
region.add_island();
region.add_house(0, "House 1", {0, 0, 100, 100});
region.add_house(0, "House 2", {100, 0, 25, 25});
region.add_island();
region.add_house(1, "House 3", {200, 0, 50, 50});
region.add_house(1, "House 4", {0, 300, 75, 75});
Draw_Something screen;
std::cout << "---------------------------------------------\n";
int total_islands = 0;
total_islands = region.get_island_count();
if (total_islands == 0)
{
std::cout << "There are no islands!\n";
return 0;
}
for (int i = 0; i < total_islands; i++)
{
int total_houses = 0;
total_houses = region.get_house_count(i);
if (total_houses == 0)
std::cout << "There are no houses on island #" << i << "\n";
else
{
for (int j = 0; j < total_houses; j++)
{
std::string house_name;
Rect house_stats;
region.get_house_info(i, j, house_name, house_stats);
screen.draw_house(house_name, house_stats);
}
}
}
return 0;
}
|