Maxim Teodor

// SARMALE, de Teodor Maxim

// Pentru ca programul sa functioneze, activati-va flagul std=c++11 din setarile compilatorului!

// CodeBlocks : (bara de sus) Settings -> Compiler -> Compiler Flags, bifati "Have g++ follow the C++11 language standard"

#include <iostream>

#include <thread>

#include <string>

#include <limits>

#include <random>

 

using namespace std::chrono_literals;

using std::chrono::milliseconds;

 

unsigned long long num;

milliseconds delay2;

bool picker, picker2;

enum dots_type { backspaced, frontspaced };

std::mt19937 dev(std::chrono::high_resolution_clock::now().time_since_epoch().count());

std::uniform_int_distribution<unsigned> udist(29, 36);

std::bernoulli_distribution dist(0.09);

 

inline void Delay(milliseconds);

void PrintChar(const std::string&, milliseconds);

void PrintDots(milliseconds, dots_type);

void Sarma(int, milliseconds);

std::string IntToStr(unsigned long long);

std::string ReadIns(std::string &str);

bool CompareStrings(const std::string&, const std::string&);

void Lowercase(std::string&);

 

const std::string afisaj[] = {"Bine ai venit in bucataria sarmalelor!",

                              "Doresti sarmale cu carne sau vegane?\n",

                              "carne",

                              "Clasic! ",

                              "Doresti cu smantana? (da sau nu): ",

                              "da",

                              "Multe calorii...\n",

                              "nu",

                              "Nicio problema!\n",

                              "E greu sa alegi? ",

                              "Mai incearca o data: ",

                              "vegane",

                              "Ooh, fancy!\n",

                              "Heiii",

                              ", sunt sigur ca stii sau te-ai incurcat atunci cand ai comandat.",

                              "\nIncearca din nou",

                              ", doresti sarmale cu carne sau vegane? ",

                              "Cate sarmale doriti sa mancati?\n",

                              "Valoare invalida, incearca din nou: ",

                              "Numar de sarmale in pregatire: ",

                              "Se pregatesc sarmalele, te rog sa astepti:\n",

                              "Pofta buna",

                              " rosu!",

                              "Sarmaua ",

                              "cu carne ",

                              "si smantana ",

                              "vegana ",

                              " este gata!",

                              "Eroare! Incearca din nou: ",

                              "Nu fii asa, ",

                              "De ce esti naspa, ",

                              "Fii serios acum, ",

                              "Ori esti prost, ori te faci, ",

                              "Acum chiar esti prost, ",

                              "Vegane sau cu carne, ",

                              "Ma faci sa cred in Dumnezeu, ",

                              "Laket, ",

                              "incearca din nou: "};

 

int main()

{

    PrintChar(afisaj[0], 20ms);

    Delay(1000ms);

    std::cout << '\n';

    PrintChar(afisaj[1], 20ms);

    std::string pick;

    bool readpick = true, firstwrong = true, smantanawrong = true;

    while (readpick)

    {

        ReadIns(pick);

        if (CompareStrings(afisaj[2], pick))

        {

            picker = false;

            PrintChar(afisaj[3], 20ms);

            Delay(1000ms);

            PrintChar(afisaj[4], 20ms);

            std::string pick2;

            while (readpick)

            {

                ReadIns(pick2);

                if (CompareStrings(afisaj[5], pick2))

                {

                    picker2 = 1;

                    PrintChar(afisaj[6], 20ms);

                    readpick = false;

                    Delay(1000ms);

                }

                else if (CompareStrings(afisaj[7], pick2))

                {

                    PrintChar(afisaj[8], 20ms);

                    readpick = false;

                    Delay(1000ms);

                }

                else

                {

                    if (smantanawrong)

                    {

                        PrintChar(afisaj[9], 20ms);

                        Delay(1000ms);

                        PrintChar(afisaj[10], 20ms);

                        smantanawrong = false;

                    }

                    else

                    {

                        PrintChar(afisaj[udist(dev)], 20ms);

                        Delay(250ms);

                        PrintChar(afisaj[37], 20ms);

                    }

                }

            }

        }

        else if (CompareStrings(afisaj[11], pick))

        {

            picker = 1;

            PrintChar(afisaj[12], 20ms);

            readpick = false;

            Delay(1000ms);

        }

        else

        {

            if (firstwrong)

            {

                PrintChar(afisaj[13], 20ms);

                Delay(250ms);

                PrintChar(afisaj[14], 20ms);

                Delay(500ms);

                PrintChar(afisaj[15], 20ms);

                Delay(250ms);

                PrintChar(afisaj[16], 20ms);

                firstwrong = false;

            }

            else

            {

                PrintChar(afisaj[udist(dev)], 20ms);

                Delay(250ms);

                PrintChar(afisaj[37], 20ms);

            }

        }

    }

    PrintChar(afisaj[17], 20ms);

    while (num <= 0)

    {

        while (!(std::cin >> num))

        {

            std::cin.clear();

            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

            PrintChar(afisaj[18], 3ms);

        }

        if (num <= 0)

            PrintChar(afisaj[18], 3ms);

    }

    milliseconds delay;

    if (num < 50 && num > 0)

    {

        if (picker == 0)

            delay = 80ms;

        if (picker == 1)

            delay = 40ms;

        delay2 = 5ms;

    }

    else if (num >= 50 && num < 100)

    {

        if (picker == 0)

            delay = 20ms;

        if (picker == 1)

            delay = 10ms;

        delay2 = 2ms;

    }

    else

    {

        delay = 5ms;

        delay2 = 1ms;

    }

    PrintChar(afisaj[19], 20ms);

    PrintDots(250ms, backspaced);

    Delay(200ms);

    PrintChar(IntToStr(num), 20ms);

    std::string finalprompt;

    if (!picker)

    {

        if (picker2)

            finalprompt = " sarmale cu carne si smantana.\n";

        else

            finalprompt = " sarmale cu carne.\n";

    }

    else

        finalprompt = " sarmale vegane.\n";

    PrintChar(finalprompt, 20ms);

    std::cout << '\n';

    PrintChar(afisaj[20], 20ms);

    Delay(1000ms);

    for (unsigned long long cnt = 1; cnt <= num; cnt++)

    {

        Sarma(cnt, delay2);

        std::cout << '\n';

        Delay(delay);

    }

    PrintDots(250ms, backspaced);

    if (dist(dev))

    {

        PrintChar(afisaj[21], 20ms);

        PrintChar(afisaj[22], 20ms);

    }

    else

        PrintChar(afisaj[21], 20ms);

    std::cout << '\n';

    std::cin.ignore();

    std::cin.get();

}

 

inline void Delay(std::chrono::milliseconds del)

{

    std::this_thread::sleep_for(del);

}

 

void PrintChar(const std::string& arr, std::chrono::milliseconds delaytime)

{

    for (auto& c : arr)

    {

        std::cout << c;

        Delay(delaytime);

    }

}

 

void PrintDots(milliseconds delaytime, dots_type type)

{

    for (int dot = 0; dot < 3; dot++)

    {

        std::cout << (type == backspaced ? ". " : " .");

        Delay(delaytime * 2);

    }

}

 

void Lowercase(std::string &s)

{

    for (char& c : s)

        c = tolower(c);

}

 

std::string IntToStr(unsigned long long a)

{

    std::string converted;

    for (; a; a /= 10)

        converted += static_cast<char>('0' + a % 10);

    for (std::size_t i = 0; i <= (converted.size() - 1) / 2; i++)

        std::swap(converted[i], converted[converted.size() - i - 1]);

    return converted;

}

 

void Sarma(int num, milliseconds delay2)

{

    std::cout << '[';

    Delay(delay2);

    PrintChar(afisaj[23], delay2);

    if (!picker && !picker2)

        PrintChar(afisaj[24], delay2);

    if (!picker && picker2)

    {

        PrintChar(afisaj[24], delay2);

        PrintChar(afisaj[25], delay2);

    }

    if (picker)

        PrintChar(afisaj[26], delay2);

    PrintChar(IntToStr(num), delay2);

    PrintChar(afisaj[27], delay2);

    std::cout << ']';

}

 

std::string ReadIns(std::string& str)

{

    str.clear();

    while (str.empty())

    {

        getline(std::cin, str);

        if (str.empty())

            PrintChar(afisaj[28], 3ms);

    }

    Lowercase(str);

    return str;

}

 

bool CompareStrings(const std::string& str1, const std::string& str2)

{

    std::string word = "";

    for (auto& x : str2)

    {

        if (!isalpha(x))

        {

            if (word == str1)

                return true;

            word.clear();

        }

        else

            word += x;

    }

    if (word == str1)

        return true;

    return false;

}