Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.3 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to perform an attack
int performAttack(int strength) {
    // Generating a random attack power between 1 and strength
    return rand() % strength + 1;
}

int main() {
    srand(time(NULL)); // Seed for random number generation

    int playerHealth = 100;
    int enemyHealth = 100;

    while (playerHealth > 0 && enemyHealth > 0) {
        // Player's turn
        printf("Choose your attack strength (1-10): ");
        int playerAttack;
        scanf("%d", &playerAttack);

        int damageDealtByPlayer = performAttack(playerAttack);
        enemyHealth -= damageDealtByPlayer;
        printf("You hit the enemy for %d damage. Enemy health: %d\n", damageDealtByPlayer, enemyHealth);

        // Enemy's turn
        int enemyAttack = performAttack(8); // Enemy's fixed attack strength
        int damageDealtByEnemy = performAttack(enemyAttack);
        playerHealth -= damageDealtByEnemy;
        printf("Enemy hits you for %d damage. Your health: %d\n", damageDealtByEnemy, playerHealth);
    }

    // Determine the winner
    if (playerHealth <= 0) {
        printf("You lost the fight. Game over!\n");
    } else {
        printf("Congratulations! You defeated the enemy!\n");
    }

    return 0;
}
Leave a Comment