-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumHunter.CPP
More file actions
169 lines (151 loc) · 5.66 KB
/
Copy pathNumHunter.CPP
File metadata and controls
169 lines (151 loc) · 5.66 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* NumHunter - A Number Guessing Game
*
* Description: An interactive console-based number guessing game with multiple difficulty levels.
* Players choose from 5 difficulty levels, each with different ranges and attempt limits.
*
* Author: Amr (Sa3dwy)
* Date: July 17, 2025
* Language: C++
*
* Features:
* - 5 difficulty levels (Very Easy to Very Hard)
* - Different number ranges and attempt limits for each level
* - Replay functionality
* - Clear console interface
*/
#include <iostream> // For input/output operations
#include <vector> // For storing difficulty level information
#include <cstdlib> // For random number generation and system commands
#include <ctime> // For seeding random number generator
using namespace std;
/**
* Game Function - Core game logic for number guessing
*
* @param RandomNum: The randomly generated number to guess
* @param att: Number of attempts allowed for the current difficulty level
*
* This function handles the main game loop where the player makes guesses
* and receives feedback until they either win or run out of attempts.
*/
void Game(int RandomNum, int att)
{
int EnterNum; // Variable to store player's guess
// Game loop: Allow player to make guesses up to the attempt limit
for (int i = 1; i <= att; i++)
{
cout << "#" << i; // Display attempt number
cout << ": ";
cin >> EnterNum; // Get player's guess
// Check if the guess is correct
if (EnterNum == RandomNum)
{
cout << "Congrats!" << EnterNum << " = " << RandomNum << endl;
return; // Exit function if player wins
}
}
// Player has run out of attempts - display game over message
cout << "==================================\n";
cout << "Unfortunately, your attempts have ended.\n";
cout << "The number was: " << RandomNum << " Try again\n";
cout << "==================================\n";
}
/**
* Main Function - Entry point of the program
*
* Handles the main game flow including:
* - Displaying difficulty levels and options
* - Processing user's difficulty selection
* - Generating random numbers based on difficulty
* - Managing game replay functionality
*/
int main()
{
// Display game title and welcome message
cout << "===============================\n";
cout << "= **** Guess the number **** =\n";
cout << "===============================\n";
cout << "\n";
char again; // Variable to store user's choice to continue or exit
// Main game loop - continues until user chooses to exit
do {
// Initialize difficulty level information arrays
vector<string> LV = { "LV(1)","LV(2)","LV(3)","LV(4)","LV(5)" };
vector <string> GDL = { "(Very Easy)","(Easy)","(Middle)","(Hard)","(Very Hard)" };
vector <string> Attempts = { "(4) Attempts Guess from (1) to (5)", "(4) Attempts Guess from (1) to (10)","(3) Attempts Guess from (1) to (15)","(3) Attempts Guess from (1) to (30)","(2) Attempts Guess from (1) to (50)" };
// Display all available difficulty levels with their details
for (int i = 0; i < GDL.size(); i++)
{
cout << "#" << LV.at(i) << " " << GDL.at(i) << " Guidance \"" << Attempts.at(i) << "\"" << endl;
}
cout << endl;
// Display difficulty selection menu
cout << "Please select the level" << endl;
int pick; // Variable to store user's difficulty choice
int counter = 1;
// Show numbered list of difficulty levels
for (int i = 0; i < GDL.size(); i++)
{
cout << "#" << counter << " " << GDL.at(i) << endl;
counter++;
}
cout << ": ";
cin >> pick; // Get user's difficulty selection
// Seed random number generator with current time
srand(time(0));
// Process difficulty selection and start appropriate game mode
if (pick == 1) // Very Easy: 1-5 range, 4 attempts
{
int RandomNumVeryyEasy = rand() % 5 + 1;
int attVeryyEasy = 4;
cout << "\nPlease guess the number from (1) to (5)." << endl;
Game(RandomNumVeryyEasy, attVeryyEasy);
}
else if (pick == 2) // Easy: 1-10 range, 4 attempts
{
int RandomNumEasy = rand() % 10 + 1;
int attEasy = 4;
cout << "\nPlease guess the number from (1) to (10)." << endl;
Game(RandomNumEasy, attEasy);
}
else if (pick == 3) // Middle: 1-15 range, 3 attempts
{
int RandomNumMiddle = rand() % 15 + 1;
int attMiddle = 3;
cout << "\nPlease guess the number from (1) to (15)." << endl;
Game(RandomNumMiddle, attMiddle);
}
else if (pick == 4) // Hard: 1-30 range, 3 attempts
{
int RandomNumHard = rand() % 30 + 1;
int attHard = 3;
cout << "\nPlease guess the number from (1) to (30)." << endl;
Game(RandomNumHard, attHard);
}
else if (pick == 5) // Very Hard: 1-50 range, 2 attempts
{
int RandomNumVeryyHard = rand() % 50 + 1;
int attVeryyHard = 2;
cout << "\nPlease guess the number from (1) to (50)." << endl;
Game(RandomNumVeryyHard, attVeryyHard);
}
else // Invalid selection
{
cout << "Note!:-" << endl;
cout << "__________***************************************__________" << endl;
cout << "_________ Please select a difficulty level to start _______\n";
cout << "__________***************************************__________" << endl;
}
// Ask user if they want to continue playing
cout << "Do you want to continue or try another difficulty?(Y|N)\n";
cout <<": ";
cin >> again;
// Clear console screen for next game
system("cls");
} while (again == 'y'||again == 'Y'); // Continue if user enters 'y' or 'Y'
// Display farewell message when user exits
cout << "__________*****************************************__________" << endl;
cout << "_________ Thank you for using the program. Goodbye! _______\n";
cout << "__________*****************************************__________" << endl;
return 0; // Successful program termination
}