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
|
#include "clearscreen.hpp"
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
void computerguesses()
{
std::string input;
unsigned short cpuguess, guesses = 0, gumballs = 0;
unsigned short mingumballs = 1, maxgumballs = 1000;
ClearScreen();
gumballs = rand() % 1000 + 1;
while(true)
{
std::cout << "There is a gumball jar, which can hold up to 1000 gumballs." << "\n";
std::cout << "You decide how many gumballs to put in the jar!" << "\n";
std::cout << ": ";
getline(std::cin, input);
std::stringstream myStream(input);
if (myStream >> gumballs)
{
if (gumballs >= 1 and gumballs <= 1000)
break;
else
{
std::cout << "The jar can only fit 1000 gumballs and you must add at least one!";
std::cin.clear();
std::cin.ignore(10000, '\n');
ClearScreen();
}
}
ClearScreen();
}
do
{
cpuguess = rand() % (maxgumballs - mingumballs + 1) + mingumballs;
std::cout << "\n" << "Here's my guess: " << cpuguess << "\n";
if (cpuguess > gumballs)
{
while (true)
{
std::cout << "Is this right? (yes or no) : ";
getline(std::cin, input);
std::cout << "\n";
if (input == "yes")
{
std::cout << "Let's see... No! You can't fool me!" << "\n";
std::cout << "I don't know how many gumballs are there, but it's obvious that there are less!" << "\n";
maxgumballs = cpuguess;
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
else if (input == "no")
{
std::cout << "Hmmm, I see there are less..." << "\n";
maxgumballs = cpuguess;
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
}
}
else if (cpuguess < gumballs)
{
while (true)
{
std::cout << "Is this right? (yes or no) : ";
getline(std::cin, input);
std::cout << "\n";
if (input == "yes")
{
std::cout << "Let's see... No! You can't fool me!" << "\n";
std::cout << "I don't know how many gumballs are there, but it's obvious that there are more!" << "\n";
mingumballs = cpuguess;
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
else if (input == "no")
{
std::cout << "Hmmm, I see there are more..." << "\n";
mingumballs = cpuguess;
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
}
}
guesses ++;
} while (cpuguess != gumballs);
if (cpuguess == gumballs)
{
while (true)
{
std::cout << "Is this right? (yes or no) : ";
getline(std::cin, input);
std::cout << "\n";
if (input == "yes")
{
std::cout << "\n" << "Haha, I got it!" << "\n";
std::cout << "Well, well, well... Let's see..." << "\n\n";
std::cout << "This took me " << guesses << " guesses, not bad, huh?" << "\n\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
else if (input == "no")
{
std::cout << "Hmmm, do you think I'm a fool?! Don't lie! I was right!" << "\n";
std::cout << "Well, well, well... Let's see..." << "\n\n";
std::cout << "This took me " << guesses << " guesses, not bad, huh?" << "\n\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
}
}
}
|