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
|
#include<iostream>
#include<array>
#include<cstdlib>
#include<iomanip>
#include<ctime>
using namespace std;
bool UniqueNums(array<array<int, 8>, 8>&a, const int col, const int row, int check)
{
bool result = false;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (check == a[i][j])
result = true;
}
}
return result;
}
void FillFunction(array<array<int, 8>, 8>&user, int c, int r)
{
for (int i = 0; i < r; i++)
{
int dup;
for (int j = 0; j < c; j++)
{
do{
user[i][j] = 50 - rand() % 101;
dup = user[i][j];
} while (UniqueNums(user, 8, 8, dup)!=false);
}
}
}
void PrintArray(array<array<int, 8>, 8>&user, int c, int r)
{
int counter = 0;
for (int k = 0; k < r; k++)
for (int h = 0; h < c; h++)
{
cout << setw(4.5) << user[k][h] << setw(5);
counter++;
if (counter % 8 == 0)
cout << endl;
}
cout << endl;
}
void main()
{
unsigned int srand(time(0));
int menu_input;
int magic_input;
int magic_number;
cout << "Game Menu : " << endl;
cout << "-----------" << endl;
cout << "1- Play" << endl;
cout << "2- Quit" << endl;
cout << endl;
cout << "Enter your choice : ";
cin >> menu_input;
cout << endl;
if (menu_input == 2)
cout << "END" << endl;
else {
cout << "Magical Number(between -50 & 50):" << endl;
cout << "*********************************" << endl;
cout << "1- Pick a number" << endl;
cout << "2- Let the computer pick a number" << endl;
cout << endl;
cout << "Enter your choice : ";
cin >> magic_input;
if (magic_input == 2)
{
magic_number = 50 - rand() % 101;
cout << endl;
cout << "Magical Number is : " << magic_number << endl;
cout << endl;
}
if (magic_input == 1)
{
cout << "Enter Magical Number: ";
cin >> magic_number;
cout << endl;
}
int computer_row_index1;
int computer_column_index1;
int user_row_index1;
int user_column_index1;
//*Initializing the 2 arrays
array<array<int, 8>, 8>user_array{};
array<array<int, 8>, 8>computer_array{};
if (magic_input == 2 || magic_input == 1) {
cout << " " << "User's Grid : " << endl;
cout << endl;
//*Filling and Printing user array
FillFunction(user_array, 8, 8);
PrintArray(user_array, 8, 8);
cout << endl;
cout << endl;
cout << " " << "Computer's Grid : " << endl;
cout << endl;
//*FIlling and Printing pc array
FillFunction(computer_array, 8, 8);
PrintArray(computer_array, 8, 8);
cout << endl;
}
}
system("pause");
}
|