We were given a homework assignment to create a C++ program that generates 3 different random numbers each time and the user is supposed to guess the three of them AND in order. I already achieved that, however I'm not very satisfied with how long my code is, especially with the fact that I had so many if statements. We were told at the beginning of the year that good programming is the ability to write less but more efficiently. (We haven't learned anything about arrays or vectors yet) but I'm still curious to know what are other ways to compare the 3 generated numbers and the 3 user input numbers, all while keeping the order criterion in mind)
PS. 3 guesses of 3 numbers in order -> User wins. If the users fails to guess the 3 numbers in their order within 10 attempts, they lose + I also kept the generated numbers to print out just for testing purposes. ALSO, we were required to have 3 functions so that's why I didn't write everything in just one.
Long story short, is there any way to write the conditions more concisely and efficiently instead of having to do a for statement for each and every possibility? I still haven't learned arrays since I'm just beginning but all suggestions and methods applicable in C++ are fascinatingly admired and highly appreciated, thank you very much!
Here's my code:
#include <iostream>
using namespace std;
#include <cstdlib>
#include <iomanip>
#include <ctime>
void randnumbers(int&, int&, int&); // Random Numbers Prototype
void guessnumbers(int&, int&, int&); // Guess Numbers Prototype
int main()
{
// --> Guesses of Numbers and their Orders
int guesses = 0;
for (int counter = 0; counter <= 10; counter++)
{
while (guesses <= 2)
{
//Random Numbers
int a, b, c;
randnumbers(a, b, c);
//Guessed Numbers
int d, e, f;
guessnumbers(d, e, f);
// IF 3 GUESSES
//IF A=D, A=E, A=F
if (a == d && b == f && c == e)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (a == d && b == e && c == f)
{
cout << "3 numbers guessed!.. 3 numbers in order!..\n";
guesses++;
break;
}
else if (a == e && b == d && c == f)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (a == e && b == f && c == d)
{
cout << "3 numbers guessed!.. 0 numbers in order..\n";
break;
}
else if (a == f && b == e && c == d)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (a == f && b == d && c == e)
{
cout << "3 numbers guessed!.. 0 numbers in order..\n";
guesses++;
break;
}
//IF B=D, B=E, B=F
else if (b == d && a == e && c == f)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (b == d && a == e && c == e)
{
cout << "3 numbers guessed!.. 0 numbers in order..\n";
break;
}
else if (b == e && a == d && c == f)
{
cout << "3 numbers guessed!.. 3 numbers in order..\n";
guesses++;
break;
}
else if (b == e && a == f && c == d)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (b == f && a == e && c == d)
{
cout << "3 numbers guessed!.. 0 numers in order..\n";
break;
}
else if (b == f && a == d && c == e)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
// IF C=D, C=E, C=F
else if (c == d && a == e && d == f)
{
cout << "3 numbers guessed!.. 0 number in order..\n";
break;
}
else if (c == d && a == f && b == e)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (c == e && a == d && b == f)
{
cout << "3 numbers guessed!.. 1 number in order..\n";
break;
}
else if (c == e && a == f && b == d)
{
cout << "3 numbers guessed!.. 0 numbers in order..\n";
break;
}
else if (c == f && a == e && b == d)
{
cout << "3 numbers guessed!.. 0 numbers in order..\n";
break;
}
else if (c == f && a == d && b == e)
{
cout << "3 numbers guessed! 3 numbers in order!..\n";
guesses++;
break;
}
//IF 2 GUESSES
else if (a == d && c == f)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
else if (a == d && c == e)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (a == d && b == f)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (a == d && b == e)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
else if (a == e && c == f)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (a == e && c == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == e && b == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == e && b == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == f && c == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == f && c == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == f && b == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (a == f && b == e)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (b == d && c == f)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (b == d && c == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (b == d && a == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (b == d && a == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (b == e && c == f)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
else if (b == e && c == d)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (b == e && a == f)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (b == e && a == d)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
else if (b == f && c == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (b == f && c == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (b == f && a == d)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (b == f && a == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == d && a == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == d && a == e)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == d && b == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == d && b == e)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (c == e && a == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == e && a == d)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (c == e && b == f)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == e && b == d)
{
cout << "2 numbers guessed.. 0 numbers in order..\n";
break;
}
else if (c == f && a == d)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
else if (c == f && a == e)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (c == f && b == d)
{
cout << "2 numbers guessed.. 1 number in order..\n";
break;
}
else if (c == f && b == e)
{
cout << "2 numbers guessed.. 2 numbers in order..\n";
break;
}
//IF ONE GUESS
else if (a == d || b == e || c == f)
{
cout << "1 number guessed.. 1 number in order..\n";
break;
}
else if (a == e || a == f || b == d || b == f || c == d || c == e)
{
cout << "1 number guessed.. 0 numbers in order..\n";
break;
}
else
{
cout << "0 numbers guessed.. 0 numbers in order..\n";
break;
}
}
}
if (guesses >= 3)
cout << "Congratulations, you guessed the numbers in order 3 times! You win..\n";
else if (guesses<3)
cout << "You failed to guess the numbers in order over an attempt of 10 times! You lose..\n";
system("pause");
return 0;
}
void randnumbers(int& x, int& y, int &z)
{
srand(time(0));
x = 1 + rand() % 6;
cout << x << setw(2);
do {
y = 1 + rand() % 6;
} while (y == x);
cout << y << setw(2);
do {
z = 1 + rand() % 6;
} while (z == x || z == y);
cout << z << setw(2);
}
void guessnumbers(int& nb1, int& nb2, int& nb3)
{
cout << "Guess the 3 numbers from 1-->6: ";
cin >> nb1 >> nb2 >> nb3;
}
a,b,c
andd,e,f
in two 3-item arraysA[3]
andB[3]
, then quickly test how many numbers were guessed by scanning the arrays with two wandering pointers: you start with 3 vs. 3 numbers; If the least item ofA
is smaller than the least one inB
, drop it and continue with 2 vs. 3, similary in opposite case; and if they are equal, count the hit and drop both items, proceeding with remaining 2 vs. 2. Finally you know how many numbers were guessed. And the direct comparision ofa
tod
,b
toe
andc
tof
will tell how many of them are in right positions. \$\endgroup\$