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
|
#include <iostream>
#include <ctime>
using namespace std;
void prompt (int);
void shuffle (int* const, int);
void play (int* const, int);
bool check (int* const, int*, int, int);
int main()
{
int n;
cout << "Enter total number: ";
cin >> n;
int* arr = new int[n];
prompt(n);
shuffle (arr, n);
play (arr, n);
system ("pause");
return 0;
}
void prompt (int n)
{
cout << "Number Guessing " << endl;
cout <<"Enter " << n << " digits (1- " << n << ") separated by a space " << endl;
cout << "---------------------------------------------------------------";
}
void shuffle (int* const randNoArr, int size )
{
srand ((unsigned int)time(NULL));
for( int i=0; i<(size-1); i++ )
{
int r = i + (rand() % (size-i));
int temp = randNoArr[i];
randNoArr[i] = randNoArr[r];
randNoArr[r] = temp;
}
}
void play (int* const randNoArr, int n)
{
int* input = new int[n];
int round=1;
do
{
cout << "\nRound " << round
<< "\nEnter Guess:";
for (int i=0; i<n; i++)
{
cin >> input[i];
}
round++;
}
while (check(randNoArr, input, n,round) == false);
}
bool check (int* const randNoArr, int* input, int n,int round)
{
int checking = 0;
for (int i=0; i<n; i++)
{
if (randNoArr[i] == input[i])
{
cout << "O ";
checking += 1;
}
else
cout << "X ";
}
cout << "\n----------------------------------------------------------";
if (n != checking)
return false;
else
{
cout << "\nCongratulations! You win the game in "
<< round-1 << " steps\n";
return true;
}
}
|