I have a game that generates three random numbers and you have to guess them. I don't know how to check whether these 3 numbers are the same as these generated. I have written this but I know it's really bad and not readable. I looked for something better but didn't find anything. Is there any way to make it better and more readable?
bool Guess(int number1, int number2, int number3)
{
int guess1, guess2, guess3;
std::cout << "Enter three numbers separated by spaces: ";
std::cin >> guess1 >> guess2 >> guess3;
if (guess1 == number1)
{
if (guess2 == number2)
{
if (guess3 == number3)
{
return true;
}
else
{
return false;
}
}
else if (guess2 == number3)
{
if (guess3 == number2)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (guess2 == number1)
{
if (guess1 == number2)
{
if (guess3 == number3)
{
return true;
}
else
{
return false;
}
}
else if (guess1 == number3)
{
if (guess3 == number2)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (guess3 == number1)
{
if (guess2 == number2)
{
if (guess1 == number3)
{
return true;
}
else
{
return false;
}
}
else if(guess1 == number2)
{
if (guess2 == number3)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
Here is the fragment how I generate these numbers
int GenerateNumber(int level)
{
if (level == 1)
{
return (rand() % 2) + level; // if level is one than number range is from 1 to 2
}
else
{
return (rand() % level) + level; // else number range is from level to (level * 2) - 1
}
}
int a = GenerateNumber(level);
int b = GenerateNumber(level);
int c = GenerateNumber(level);
If the order doesn't matter, then change the order to something that makes it easy to do. To do that, you can put the guesses and numbers in separate arrays, sort them and then compare. You can make this whole process even easier by using a std::multiset
/std::unordered_multiset
to do all of this work for you. That would look like
bool Guess(int number1, int number2, int number3)
{
int guess1, guess2, guess3;
std::cout << "Enter three numbers separated by spaces: ";
std::cin >> guess1 >> guess2 >> guess3;
return std::multiset<int>{guess1, guess2, guess3} == std::multiset<int>{number1, number2, number3};
}
I can suggest the following solution as it is shown in the demonstrative program.
#include <iostream>
#include <iomanip>
#include <array>
#include <iterator>
#include <algorithm>
bool Guess( int number1, int number2, int number3 )
{
const size_t N = 3;
std::array<int, N> target = { number1, number2, number3 };
std::sort( std::begin( target ), std::end( target ) );
std::array<int, N> guess;
std::cout << "Enter three numbers separated by spaces: ";
std::copy_n( std::istream_iterator<int>( std::cin ), N, std::begin( guess ) );
std::sort( std::begin( guess ), std::end( guess ) );
return target == guess;
}
int main()
{
int n1 = 3, n2 = 1, n3 = 2;
std::cout << std::boolalpha << Guess( n1, n2, n3 ) << '\n';
return 0;
}
Its output might look like
Enter three numbers separated by spaces: 1 2 3
true
Instead of the call pf the algorithm std::copy_n
you can just use the following statement
std::cin >> guess[0] >> guess[1] >> guess[2];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With