-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonster-battle.cpp
96 lines (85 loc) · 2.98 KB
/
monster-battle.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
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL)); // Seed the random number generator with the current time
int playerHealth = 100;
int monsterHealth = 100;
string playerName;
string monsterName = "Goblin";
int playerAttackPower = 10;
int monsterAttackPower = 5;
bool isPlayerTurn = true;
bool isGameOver = false;
cout << "Welcome to Monster Battle!" << endl;
cout << "What is your name, adventurer? ";
cin >> playerName;
cout << "Welcome, " << playerName << "! Your adventure begins now!" << endl;
while (!isGameOver)
{
// Display current health of both player and monster
cout << "==============================" << endl;
cout << "Player: " << playerHealth << " HP" << endl;
cout << monsterName << ": " << monsterHealth << " HP" << endl;
cout << "==============================" << endl;
if (isPlayerTurn)
{
// Player's turn to attack
cout << "It's your turn, " << playerName << "! What do you do?" << endl;
cout << "1. Attack" << endl;
cout << "2. Run" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
// Player chooses to attack
int damage = rand() % playerAttackPower + 1;
monsterHealth -= damage;
cout << "You attack the " << monsterName << " for " << damage << " damage!" << endl;
}
else if (choice == 2)
{
// Player chooses to run
cout << "You attempt to run away!" << endl;
int escape = rand() % 2;
if (escape == 1)
{
cout << "You successfully escape from the " << monsterName << "!" << endl;
isGameOver = true;
}
else
{
cout << "You fail to escape from the " << monsterName << "!" << endl;
}
}
else
{
cout << "Invalid choice. Please choose 1 or 2." << endl;
}
isPlayerTurn = false; // Player's turn is over
}
else
{
// Monster's turn to attack
int damage = rand() % monsterAttackPower + 1;
playerHealth -= damage;
cout << "The " << monsterName << " attacks you for " << damage << " damage!" << endl;
isPlayerTurn = true; // Monster's turn is over
}
// Check if the game is over
if (playerHealth <= 0)
{
cout << "You have been defeated by the " << monsterName << "! Game over." << endl;
isGameOver = true;
}
else if (monsterHealth <= 0)
{
cout << "You have defeated the " << monsterName << "! Congratulations!" << endl;
isGameOver = true;
}
}
return 0;
}