diff --git a/organism.cpp b/organism.cpp index 6d77b95..d6672a9 100644 --- a/organism.cpp +++ b/organism.cpp @@ -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>(end - begin).count() > 0.01) { - goto label; + if (std::chrono::duration_cast>(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 diff --git a/starklag b/starklag index 857cfa5..e17ed1f 100755 Binary files a/starklag and b/starklag differ diff --git a/starklag.cpp b/starklag.cpp index 1be789d..fc8da5e 100644 --- a/starklag.cpp +++ b/starklag.cpp @@ -145,7 +145,8 @@ 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__) @@ -153,13 +154,13 @@ int main() { #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(); @@ -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(); @@ -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)); @@ -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; @@ -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; @@ -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(); @@ -350,4 +372,5 @@ int main() { // noecho.c_lflag &= ~ECHO;, noecho.c_lflag |= ECHO; tcsetattr(0, TCSAFLUSH, &orig_termios); #endif + return 0; } \ No newline at end of file