-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz3.cpp
More file actions
77 lines (73 loc) · 2.04 KB
/
Copy pathquiz3.cpp
File metadata and controls
77 lines (73 loc) · 2.04 KB
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
#include <cstdint>
#include <format>
#include <iostream>
#include "Random.h"
int computer_guess(auto min, auto max)
{
return Random::get(min, max);
}
bool check_guess(std::int32_t guess, std::int32_t computer_guess, int min, int max)
{
if (guess < min || guess > max)
{
std::cout << std::format(
"Please enter a valid guess that is in the range ({}, {}).\n",
min, max
);
return false;
}
if (guess == computer_guess)
{
std::cout << "Correct! You win!\n";
return true;
}
if (guess > computer_guess)
std::cout << "Your guess is too high.\n";
else
std::cout << "Your guess is too low.\n";
return false;
}
int main()
{
constexpr std::int16_t min{1};
constexpr std::int16_t max{100};
constexpr std::int16_t n_tries{7};
char play_again{'y'};
while (play_again == 'y')
{
const int computer_guessed_number{computer_guess(min, max)};
std::cout << std::format(
"Let's play a game. I'm thinking of a number between {} and {}. "
"You have {} tries to guess what it is.\n",
min, max, n_tries
);
bool won{false};
std::int32_t guess{};
for (int tries{1}; tries <= n_tries; ++tries)
{
std::cout << std::format("Guess #{}: ", tries);
std::cin >> guess;
if (check_guess(guess, computer_guessed_number, min, max))
{
won = true;
break;
}
}
if (!won)
{
std::cout << std::format(
"Sorry, you lose. The correct number was {}.\n",
computer_guessed_number
);
}
std::cout << "Would you like to play again (y/n)? ";
std::cin >> play_again;
while (play_again != 'y' && play_again != 'n')
{
std::cout << "Would you like to play again (y/n)? ";
std::cin >> play_again;
}
}
std::cout << "Thank you for playing.\n";
return 0;
}