forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwild-west.cpp
87 lines (75 loc) · 2.19 KB
/
wild-west.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
using namespace std;
// Function to print the game instructions
void printInstructions()
{
cout << "Welcome to the Wild West! Your mission is to catch the notorious outlaw, Jesse James." << endl;
cout << "You have a limited number of bullets, so use them wisely. Good luck!" << endl;
cout << "Commands: 'shoot', 'reload', 'quit'" << endl;
}
// Function to play the game
void playGame()
{
// Initialize game variables
int bullets = 6;
bool jesseJamesCaught = false;
bool gameWon = false;
string input;
// Print instructions
printInstructions();
// Game loop
while (bullets > 0 && !gameWon)
{
// Get player input
cout << "You have " << bullets << " bullets. Enter command: ";
getline(cin, input);
// Process player input
if (input == "shoot")
{
bullets--;
cout << "You shoot your gun..." << endl;
// Generate a random number between 1 and 10
int randomNum = rand() % 10 + 1;
// If the number is 1-4, Jesse James is caught
if (randomNum >= 1 && randomNum <= 4)
{
jesseJamesCaught = true;
gameWon = true;
cout << "You caught Jesse James! You win!" << endl;
}
else
{
cout << "You missed Jesse James. He's still on the loose." << endl;
}
}
else if (input == "reload")
{
bullets = 6;
cout << "You reload your gun." << endl;
}
else if (input == "quit")
{
cout << "Quitting game." << endl;
return;
}
else
{
cout << "Invalid command. Please enter 'shoot', 'reload', or 'quit'." << endl;
}
}
// If the player runs out of bullets without catching Jesse James, they lose
if (bullets == 0 && !jesseJamesCaught)
{
cout << "You ran out of bullets and Jesse James got away. Game over." << endl;
}
}
// Main function
int main()
{
// Seed the random number generator
srand(time(NULL));
// Play the game
playGame();
return 0;
}