Created
September 13, 2017 07:40
-
-
Save LukeTully/a6fb8c447cfa83b71b1c74eee027967a to your computer and use it in GitHub Desktop.
C++ basic guessing game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int divideAndConquer(int min, int max); | |
int main() | |
{ | |
int current = 50, max = 100, prev = 0; | |
std::string next; | |
std::string posAns = "y"; | |
std::string negAns = "n"; | |
std::string confirmation = "c"; | |
std::cout << "Is your number greater than " << current << "? (" << posAns << "/" << negAns << " or " << confirmation << " to confirm):" << std::endl; | |
while (std::cin >> next) | |
{ | |
if (next == posAns) | |
{ | |
if ((max - current) == 2) | |
{ | |
std::cout << "Your number was " << current + 1 << std::endl; | |
break; | |
} | |
prev = current; | |
} | |
else if (next == negAns) | |
{ | |
if ((current - prev) == 2) | |
{ | |
std::cout << "Your number was " << current + 1 << std::endl; | |
break; | |
} | |
max = current; | |
} | |
else if (next == confirmation) | |
{ | |
std::cout << "Your number was " << current << std::endl; | |
break; | |
} | |
else | |
{ | |
break; | |
} | |
current = divideAndConquer(prev, max); | |
std::cout << "Is your number greater than " << current << "? (" << posAns << "/" << negAns << " or " << confirmation << " to confirm):" << std::endl; | |
} | |
return 0; | |
} | |
int divideAndConquer(int min, int max) | |
{ | |
return min + ((max - min) / 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment