-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cpp
152 lines (124 loc) · 4.49 KB
/
World.cpp
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
#include <iostream>
#include <algorithm>
#include "World.h"
#include "Animals.h"
#include "Plants.h"
World::World() : allOrganisms(), eventsThatHappenedInRound(), wordlHeight(0),
worldWidth(), userLastMove(NO_PROPER_KEY) { }
World::~World() {
for(Organism* organism : allOrganisms)
delete organism;
}
void World::PrintWorld() const {
for(int i = 0; i < worldWidth + 2; i++)
std::cout << DIVIDER_BLOCK;
std::cout << "\n";
for(int i = 0; i < wordlHeight; i++) {
std::cout << DIVIDER_BLOCK;
for(int j = 0; j < worldWidth; j++) {
Organism* foundOrganism = FindWhoStandsOnThisPosition(position(j,i));
if(foundOrganism != nullptr) {
foundOrganism->Draw();
} else
std::cout << " ";
}
std::cout << DIVIDER_BLOCK << "\n";
}
for(int i = 0; i < worldWidth + 2; i++)
std::cout << DIVIDER_BLOCK;
std::cout << "\n";
}
void World::InitDimensions(const int width, const int height) {
worldWidth = width;
wordlHeight = height;
}
void World::InitOrganisms(int organismsNum) {
if(organismsNum >= (worldWidth * wordlHeight))
throw "Za duzo organizmow";
int humanNum = 1;
int animalsNum = (int)((organismsNum - humanNum) * HOW_MANY_PERCENTAGE_OF_ORGANISMS_ARE_ANIMALS/100);
int plantsNum = organismsNum - humanNum - animalsNum;
allOrganisms.reserve(worldWidth*wordlHeight);
CreateAnimals(animalsNum);
CreatePlants(plantsNum);
CreateHuman();
}
void World::CreateAnimals(const int animalsNum) {
using namespace animals;
for(int i = 0; i < animalsNum; i++) {
int animalTypeNum = rand() % ANIMAL_TYPES_NUM;
switch (animalTypeNum) {
case 0: CreateOrganism<Wolf>(); break;
case 1: CreateOrganism<Sheep>(); break;
case 2: CreateOrganism<Fox>(); break;
case 3: CreateOrganism<Turtle>(); break;
case 4: CreateOrganism<Antelope>(); break;
}
}
}
void World::CreatePlants(const int plantsNum) {
using namespace plants;
for(int i = 0; i < plantsNum; i++) {
int plantTypeNum = rand() % PLANT_TYPES_NUM;
switch (plantTypeNum) {
case 0: CreateOrganism<Grass>(); break;
case 1: CreateOrganism<Sonchus>(); break;
case 2: CreateOrganism<Guarana>(); break;
case 3: CreateOrganism<Berries>(); break;
case 4: CreateOrganism<SosnowskiBorsch>(); break;
}
}
}
void World::CreateHuman() {
CreateOrganism<animals::Human>();
}
template<typename T>
void World::CreateOrganism() {
position freePosition;
do {
freePosition.x = rand() % worldWidth;
freePosition.y = rand() % wordlHeight;
} while(FindWhoStandsOnThisPosition(freePosition) != nullptr);
allOrganisms.push_back(new T(this, freePosition));
}
Organism* World::FindWhoStandsOnThisPosition(const position& checkedPosition, Organism* organismToIgnore) const {
Organism* foundOrganism = nullptr;
for(Organism* organism : allOrganisms)
if(organism->IsStandingHere(checkedPosition) && organism != organismToIgnore) {
foundOrganism = organism;
break;
}
return foundOrganism;
}
void World::MakeMove(const int userMove) {
SortOrganisms();
userLastMove = userMove;
for(int i = 0; i < allOrganisms.size(); i++) {
allOrganisms[i]->IncrementAge();
allOrganisms[i]->Action(worldWidth, wordlHeight);
}
}
void World::SortOrganisms() {
std::sort(allOrganisms.begin(), allOrganisms.end(), [](Organism* a, Organism* b) -> bool {
return (*b) < (*a);
});
}
void World::ReportAboutOrganismNewPosition(Organism* movedOrganism) {
Organism* otherOrganism = FindWhoStandsOnThisPosition(movedOrganism->GetPosition(), movedOrganism);
if(otherOrganism != nullptr)
movedOrganism->Collision(otherOrganism);
}
void World::ReportAboutOrganismDeath(Organism* killer, Organism* deadOrganism) {
std::vector<Organism*>::iterator position = std::find(allOrganisms.begin(), allOrganisms.end(), deadOrganism);
if (position != allOrganisms.end()) {
if(deadOrganism->IsAnimal())
eventsThatHappenedInRound += (deadOrganism->GetName() + " zostal/a zabity/a przez " + killer->GetName() + "\n");
allOrganisms.erase(position);
}
delete deadOrganism;
}
std::string World::GetEventsFromLastRound() {
std::string events = eventsThatHappenedInRound;
eventsThatHappenedInRound = "";
return events;
}