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 |
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; // Function to simulate a player's attack int playerAttack() { return rand() % 10 + 1; // Random attack damage between 1 and 10 } // Function to simulate a monster's attack int monsterAttack() { return rand() % 8 + 1; // Random attack damage between 1 and 8 } int main() { srand(static_cast<unsigned>(time(0))); // Seed for random number generation int playerHealth = 100; int monsterHealth = 100; char action; cout << "Welcome to the Simple RPG Game!" << endl; cout << "You are fighting a monster. Try to defeat it before it defeats you!" << endl; while (playerHealth > 0 && monsterHealth > 0) { cout << "\nYour health: " << playerHealth << " | Monster's health: " << monsterHealth << endl; cout << "Choose your action: (a)ttack or (r)un: "; cin >> action; if (action == 'a') { // Player attacks the monster int damage = playerAttack(); monsterHealth -= damage; cout << "You attack the monster for " << damage << " damage." << endl; if (monsterHealth <= 0) { cout << "You have defeated the monster!" << endl; break; } // Monster attacks the player damage = monsterAttack(); playerHealth -= damage; cout << "The monster attacks you for " << damage << " damage." << endl; if (playerHealth <= 0) { cout << "You have been defeated by the monster!" << endl; break; } } else if (action == 'r') { cout << "You have chosen to run away!" << endl; break; } else { cout << "Invalid action. Please choose 'a' to attack or 'r' to run." << endl; } } return 0; } |
Explanation
- Function Definitions:
playerAttack()
: Simulates the player’s attack, returning a random damage value between 1 and 10.monsterAttack()
: Simulates the monster’s attack, returning a random damage value between 1 and 8.
- Main Function:
- Initialization:
- Seeds the random number generator using the current time.
- Initializes player and monster health to 100.
- Game Loop:
- Continuously runs while both the player and monster have health.
- Displays current health of the player and the monster.
- Prompts the user to choose an action (
'a'
to attack or'r'
to run). - If Attacking:
- The player attacks the monster, reducing the monster’s health.
- Checks if the monster’s health drops to 0 or below to determine if the player wins.
- If the monster is still alive, it attacks the player, reducing the player’s health.
- Checks if the player’s health drops to 0 or below to determine if the player loses.
- If Running:
- Ends the game, indicating the player has chosen to run away.
- Invalid Action:
- Displays an error message for invalid actions.
- Initialization:
Notes:
- This basic RPG game demonstrates a simple turn-based combat system.
- It features random damage values for attacks and allows the player to choose between attacking and running.
- Enhancements can include multiple monsters, inventory systems, more complex combat mechanics, and a more detailed storyline.