-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_game.cpp
72 lines (62 loc) · 1.6 KB
/
simple_game.cpp
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
void print_array(int array[], int count)
{
std::cout << "Your guesses were: " << std::endl;
for (int i = 0; i < count; i++)
{
std::cout << array[i] << '\t';
}
std::cout << std::endl;
std::cout << "Solved in: " << count << " attempts." << std::endl;
}
void play_game()
{
int guesses[251];
int guess_count = 0;
int random = rand() % 251;
std::cout << random << std::endl; //This prints the answer, useful for debugging.
std::cout << "Guess a number: ";
while(true)
{
int guess;
std::cin >> guess;
guesses[guess_count++] = guess; //counter can be incremented in the index!
if(guess == random)
{
std::cout << "You Win!\n";
break;
} else if (guess < random)
{
std::cout << "Too low\n";
} else
{
std::cout << "Too High\n";
}
}
print_array(guesses, guess_count);
std::cout << std::endl;
}
int main()
{
srand(time(NULL)); //Seed a random number using the time
int choice;
do
{
std::cout << "Pick an option:\n0. Quit\n1. Play Game\n";
std::cin >> choice;
switch(choice)
{
case 0:
std::cout << "Bye!\n";
return 0;
case 1:
std::cout << "Let's go!\n";
play_game();
break;
}
}
while (choice != 0);
}