online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <chrono> #include <iostream> #include <random> #include <thread> enum class Desire { None, Food }; void tone(int frequency, int duration) { // Здесь мог бы быть реальный звук } int main() { std::mt19937_64 rng(std::random_device{}()); // Возвращает true с вероятностью 1 / n auto happensRarely = [&](unsigned long long n) { return std::uniform_int_distribution<unsigned long long>(0, n - 1)(rng) == 0; }; // Константы симуляции constexpr int loopDelayMs = 10; // 1 цикл = 10 мс constexpr int hungerDelayCycles = 200; // голод через ~2 сек constexpr int hungerTimeoutCycles = 140; // смерть через ~8 сек голода constexpr unsigned long long button1Chance = 130; // кнопка 1: в среднем раз в ~2 сек constexpr unsigned long long button2Chance = 120; // кнопка 2: в среднем раз в ~1.2 сек Desire currentDesire = Desire::None; int alienSize = 1; int cyclesWithoutHunger = 0; // сколько циклов прошло без голода int hungryCycles = 0; // сколько циклов существо голодно std::cout << "Симуляция началась\n"; std::cout << "Существо спокойно. Размер: " << alienSize << "\n"; while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(loopDelayMs)); bool isButton1Pressed = happensRarely(button1Chance); bool isButton2Pressed = happensRarely(button2Chance); if (currentDesire == Desire::None) { // Ждём появления голода ++cyclesWithoutHunger; if (cyclesWithoutHunger >= hungerDelayCycles) { currentDesire = Desire::Food; cyclesWithoutHunger = 0; hungryCycles = 0; std::cout << "Появился голод\n"; } } else { // Существо голодно ++hungryCycles; // Кнопка 1 кормит существо if (isButton1Pressed) { std::cout << "Нажата кнопка 1: существо накормлено\n"; tone(2000, 300); ++alienSize; currentDesire = Desire::None; hungryCycles = 0; cyclesWithoutHunger = 0; std::cout << "Размер существа: " << alienSize << "\n"; std::cout << "Существо снова спокойно\n"; } // Кнопка 2 не кормит, только издаёт другой звук else if (isButton2Pressed) { std::cout << "Нажата кнопка 2: это не еда\n"; tone(600, 300); } // Если слишком долго голодно — завершаем программу if (hungryCycles >= hungerTimeoutCycles) { std::cout << "Существо слишком долго было голодно\n"; std::cout << "Игра окончена\n"; return 1; } } } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue