Skip to content

Commit

Permalink
Quit, improved children placing
Browse files Browse the repository at this point in the history
  • Loading branch information
FLAK-ZOSO committed Nov 1, 2023
1 parent 71c2c44 commit 88bad22
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
46 changes: 26 additions & 20 deletions organism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,48 @@ void Organism::breed(Organism* other) {
for (Organism* child : children) {
sista::Coordinates new_coordinates = coordinates;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
Range r_i, r_j;
int random = random_engine() % 4;
if (random % 4 == 0) {
r_i = {-10, 10, 1};
r_j = {-10, 10, 1};
} else if (random % 4 == 1) {
r_i = {-10, 10, 1};
r_j = {10, -10, -1};
} else if (random % 4 == 2) {
r_i = {10, -10, -1};
r_j = {-10, 10, 1};
} else if (random % 4 == 3) {
r_i = {10, -10, -1};
r_j = {10, -10, -1};
}
for (int i = r_i.start; i < r_i.stop; i+=r_i.step) {
for (int j = r_j.start; j < r_j.stop; j+=r_j.step) {

int random = rand() % 4;
for (int i = -10; i < 10; i++) {
for (int j = -10; j < 10; j++) {
if (i == 0 && j == 0) {
continue;
}
if (random == 0) {
i = -i;
} else if (random == 1) {
j = -j;
} else if (random == 2) {
i = -i;
j = -j;
}
new_coordinates.y = coordinates.y + i;
new_coordinates.x = coordinates.x + j;
if (field->isOutOfBounds(new_coordinates)) {
continue;
}
if (field->isFree(new_coordinates)) {
#if DEBUG
debug << "\t" << child << " is placed at delta {" << i << ", " << j << "}" << std::endl;
#endif
goto found;
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() > 0.01) {
goto label;
if (std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() > 0.1) {
goto not_found;
}
}
}
goto not_found;
found:
child->coordinates = new_coordinates;
field->addPawn((sista::Pawn*)child);
continue;
label:
not_found:
#if DEBUG
debug << "\t" << child << " couldn't find a place to be born" << std::endl;
#endif
organisms.erase(std::find(organisms.begin(), organisms.end(), child));
children.erase(std::find(children.begin(), children.end(), child));
delete child;
return; // There's no space for other children
Expand Down
Binary file modified starklag
Binary file not shown.
39 changes: 31 additions & 8 deletions starklag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,22 @@ int main() {
Organism::dead_organisms.clear();

bool paused = false;
std::thread input_thread([&paused]() {
bool quit = false;
std::thread input_thread([&paused, &quit]() {
while (true) {
char c;
#if defined(_WIN32) or defined(__linux__)
c = getch();
#elif __APPLE__
c = getchar();
#endif
if (paused) {
if (c == 'r') {
std::string resume = "resume";
bool resume_ = true;
for (int i = 0; i < (int)(resume.size()); i++) {
if (c != resume[i]) {
resume_ = false;
return;
break;
}
#if defined(_WIN32) or defined(__linux__)
c = getch();
Expand All @@ -170,13 +171,13 @@ int main() {
if (resume_) {
paused = false;
}
} else {
} else if (c == 'p') {
std::string pause = "pause";
bool pause_ = true;
for (int i = 0; i < (int)(pause.size()); i++) {
if (c != pause[i]) {
pause_ = false;
return;
break;
}
#if defined(_WIN32) or defined(__linux__)
c = getch();
Expand All @@ -187,10 +188,28 @@ int main() {
if (pause_) {
paused = true;
}
} else if (c == 'q') {
std::string quit_string = "quit";
bool quit_ = true;
for (int i = 0; i < (int)(quit_string.size()); i++) {
if (c != quit_string[i]) {
quit_ = false;
break;
}
#if defined(_WIN32) or defined(__linux__)
c = getch();
#elif __APPLE__
c = getchar();
#endif
}
if (quit_) {
quit = true;
return;
}
}
}
});
for (int _ = 0; _ < 100; _++) {
while (!quit) {
for (int i = 0; i < 10; i++) {
while (paused) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Expand All @@ -214,7 +233,7 @@ int main() {
int free_spaces = freeSpacesAround(organism);
if (!free_spaces) {
#if DEBUG
debug << "Organism" << organism << " (" << organism->id << ") is asphyxiated with " << organism->health << " health and " << organism->left << " left at {" << organism->getCoordinates().y << ", " << organism->getCoordinates().x << "}" << std::endl;
debug << "Organism " << organism << " (" << organism->id << ") is asphyxiated with " << organism->health << " health and " << organism->left << " left at {" << organism->getCoordinates().y << ", " << organism->getCoordinates().x << "}" << std::endl;
#endif
Organism::dead_organisms.push_back(organism);
continue;
Expand All @@ -224,7 +243,7 @@ int main() {
// Check of death and aging
if (organism->left <= 0 || organism->health <= 0) {
#if DEBUG
debug << "Organism" << organism << " (" << organism->id << ") is dead with " << organism->health << " health and " << organism->left << " left at {" << organism->getCoordinates().y << ", " << organism->getCoordinates().x << "}" << std::endl;
debug << "Organism " << organism << " (" << organism->id << ") is dead with " << organism->health << " health and " << organism->left << " left at {" << organism->getCoordinates().y << ", " << organism->getCoordinates().x << "}" << std::endl;
#endif
Organism::dead_organisms.push_back(organism);
continue;
Expand Down Expand Up @@ -323,6 +342,9 @@ int main() {
break; // Don't print all the organisms over each other
}
std::cout << std::flush;
if (quit) {
break;
}
}
#if WIN32
ANSI::reset();
Expand Down Expand Up @@ -350,4 +372,5 @@ int main() {
// noecho.c_lflag &= ~ECHO;, noecho.c_lflag |= ECHO;
tcsetattr(0, TCSAFLUSH, &orig_termios);
#endif
return 0;
}

0 comments on commit 88bad22

Please sign in to comment.