This is one of the games I created in C++:
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;
const int SIZE = 100;
void initGame(int[], const int);
void printGame(int[], const int);
int findMin(int[], const int);
int findMax(int[], const int);
int guess();
bool found(int[], const int, int);
int main()
{
srand(time(0));
int game[SIZE];
initGame(game, SIZE);
// printGame(game, SIZE);
cout << "The minimum value in the array is " << findMin(game, SIZE) << endl;
cout << "The maximum value is " << findMax(game, SIZE) << endl;
bool quit = false;
while (quit == false)
{
int value = guess();
bool value_found = found(game, SIZE, value);
if (value_found == true)
cout << "Your value was found in the game\n";
if (value_found == false)
cout << "Your value wasn't found in the game\n";
cout << "Do you want to guess again or quit?"
" If you want to quit, type q\n"
"If you want to guess again, type any other key: ";
char answer;
cin >> answer;
if (answer == 'q')
quit = true;
}
}
void initGame(int int_array[], const int length)
{
for (int i = 0; i < length; i++)
int_array[i] = rand() % 1000 + 1;
}
void printGame(int int_array[], const int length)
{
for (int i = 0; i < length; i++)
cout << int_array[i] << endl;
}
int findMin(int int_array[], const int length)
{
int smallest = int_array[0];
for (int i = 1; i < length; i++)
{
if (int_array[i] < smallest)
smallest = int_array[i];
}
return smallest;
}
int findMax(int int_array[], const int length)
{
int largest = int_array[0];
for (int i = 1; i < length; i++)
{
if (int_array[i] > largest)
largest = int_array[i];
}
return largest;
}
int guess()
{
int value = 1;
while (value != 0)
{
cout << "Please enter a value between 1 and 1000: ";
cin >> value;
if (value >= 1 && value <= 1000)
{
break;
}
if (value == 0)
{
cout << "You decided to exit the program. Bye.\n";
exit(0);
}
}
return value;
}
bool found(int int_array[], const int length, int value)
{
bool value_found = false;
for (int i = 0; i < length; i++)
{
if (value == int_array[i])
value_found = true;
}
return value_found;
}
Later, I will post some other games I created in C++.
Bookmarks