Tarkastele ratkaisua, joka tehdään ja haastetoiminnan aikana

Valmis

Seuraava koodi on yksi mahdollinen ratkaisu edellisen osion haasteeseen.

int hero = 10;
int monster = 10;

Random dice = new Random();

do
{
    int roll = dice.Next(1, 11);
    monster -= roll;
    Console.WriteLine($"Monster was damaged and lost {roll} health and now has {monster} health.");

    if (monster <= 0) continue;

    roll = dice.Next(1, 11);
    hero -= roll;
    Console.WriteLine($"Hero was damaged and lost {roll} health and now has {hero} health.");

} while (hero > 0 && monster > 0);

Console.WriteLine(hero > monster ? "Hero wins!" : "Monster wins!");

Tämä koodi on vain "yksi mahdollinen ratkaisu", koska hyökkäyslogiikan suorittamiseen on monia eri tapoja.

Siitä huolimatta tulostesi tulee olla samankaltainen kuin seuraavan esimerkin tuloste:

Monster was damaged and lost 1 health and now has 9 health.
Hero was damaged and lost 2 health and now has 8 health.
Monster was damaged and lost 1 health and now has 8 health.
Hero was damaged and lost 4 health and now has 4 health.
Monster was damaged and lost 7 health and now has 1 health.
Hero was damaged and lost 6 health and now has -2 health.
Monster wins!

Jos onnistuit, onnittelut! Jatka seuraavaan haasteeseen. Jos sinulla oli ongelmia, tarkastele ratkaisua ja yritä ymmärtää, miten se toimii. Haluat ehkä tarkastella edellisiä osioita ja yrittää sitten tätä haastetoimintoa uudelleen ennen jatkamista.