Kodewick
Commencer à apprendre →
Mini-défi←→0 / 70

Bois une potion

#067Mini-défi6 min

Le héros a 50 PV et 2 potions. drink(h) en utilise une : une potion de moins et 30 PV de plus, mais jamais au-dessus de 100. Sans potion, la fonction ne fait rien (c’est déjà écrit). main boit trois fois.

Devrait afficher
HP: 100, potions: 0
main.c
#include <stdio.h>

struct Hero {
    int hp;
    int potions;
};

void drink(struct Hero *h) {
    if (h->potions == 0) {
        return;
    }
    // ton code ici: une potion de moins, 30 hp de plus, et retour à 100 si au-dessus
}

int main(void) {
    struct Hero hero = {50, 2};
    drink(&hero);
    drink(&hero);
    drink(&hero);
    printf("HP: %d, potions: %d\n", hero.hp, hero.potions);
    return 0;
}
Ctrl + Entrée lance aussi le code
Résultat

Exécuter teste les exemples. Quand ils sont bons, appuie sur Envoyer.