Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- using namespace std;
- class Player {
- public:
- int health = 100;
- bool isAlive = true;
- };
- class Computer {
- public:
- int health = 100;
- bool isAlive = true;
- };
- void playerAttack(int x)
- {
- Player player{};
- Computer computer{};
- srand((unsigned)time(0));
- int damageTaken;
- damageTaken = (rand() % 41) + 15;
- if (damageTaken == 41)
- {
- damageTaken = 40;
- }
- x = computer.health = computer.health - damageTaken;
- cout << "you have taken " << damageTaken << " damage from the computer. Its health is now " << computer.health << endl;
- }
- void playerHeal(int x)
- {
- Player player{};
- srand((unsigned)time(0));
- int healthHealed;
- healthHealed = (rand() % 30) + 15;
- x = player.health = player.health + healthHealed;
- if (player.health > 100)
- {
- player.health = 100;
- }
- cout << "You have healed " << healthHealed << " damage. Your health is now " << player.health << endl;
- }
- void computerAttack(int x)
- {
- Computer computer{};
- Player player{};
- int computerHealth = computer.health;
- srand((unsigned)time(0));
- int damageTaken;
- damageTaken = (rand() % 40) + 15;
- x = player.health = player.health - damageTaken;
- cout << "the computer has taken " << damageTaken << " damage from you. Your health is now " << player.health << endl;
- }
- void computerHeal(int x)
- {
- Computer computer{};
- int computerHealth = computer.health;
- srand((unsigned)time(0));
- int healthHealed;
- healthHealed = (rand() % 30) + 15;
- int convertTo100 = healthHealed + computer.health;
- x = computer.health = computer.health + healthHealed;
- if (computer.health > 100)
- {
- computer.health = 100;
- }
- cout << "The computer has healed " << healthHealed << " damage. Its health is now " << computer.health << endl;
- }
- void computerRandomChoose()
- {
- Computer computer{};
- Player player{};
- srand((unsigned)time(0));
- int randomMove;
- randomMove = (rand() % 3) + 1;
- if (computer.health == 100)
- {
- computerAttack(player.health);
- }
- else if (randomMove == 2)
- {
- computerAttack(player.health);
- }
- else if (randomMove == 3)
- {
- computerAttack(player.health);
- }
- else
- {
- computerHeal(computer.health);
- }
- }
- void askPlayerWhatToDo()
- {
- Computer computer{};
- Player player{};
- cout << "Will you attack (a), or heal (h): ";
- char choice;
- cin >> choice;
- if (choice == 'a')
- {
- playerAttack(computer.health);
- }
- else if (choice == 'h')
- {
- playerHeal(player.health);
- }
- else
- {
- cout << "Invalid choice, please retry" << endl;
- askPlayerWhatToDo();
- }
- }
- void theGame()
- {
- Player player{};
- Computer computer{};
- while (player.health > 0 && computer.health > 0) {
- askPlayerWhatToDo();
- computerRandomChoose();
- }
- if (player.health <= 0)
- {
- player.isAlive = false;
- cout << "The computer has won. " << endl;
- }
- else if (computer.health <= 0)
- {
- computer.isAlive = false;
- cout << "You have won" << endl;
- }
- else if (computer.health == 0 && player.health == 0)
- {
- cout << "You and the computer have died. So you tied." << endl;
- }
- if (player.health == 0 || computer.health == 0)
- {
- cout << "Do you want to restart? (y = yes, n = no): ";
- char choice;
- cin >> choice;
- if (choice == 'y')
- {
- theGame();
- }
- }
- }
- int main()
- {
- cout << "Do you want to start a new game? (y = yes, n = no): ";
- char choice;
- cin >> choice;
- if (choice == 'y')
- {
- theGame();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement