We’re still at war! The orcs are attacking and are looking very hungry! Look at them!
Let us try to model these filthy beasts. Ask the user for a number. That number is the amount of orcs in the army. Create as many struct
instances with random property values and print these on the screen. An orc has the following properties (both simple numbers, between 1 and 10, use rand() from stdlib
):
attack
life
INPUT: 3 OUTPUT: orc 1: attack 3, life 5. orc 2: attack 5, life 6. orc 3: attack 1, life 1.
Tips:
rand()
docs.generate_orcs()
will keep your main()
function short and clean. The function will return a list of orcs, the “army”, so to speak. Remember that returning an array is of type Orc*
.The generate method will look like this:
Orc* generate_orcs(int amount) {
Orc* army = malloc(sizeof(Orc) * amount);
// add stuff to army
return army;
}
Details on how the malloc()
function works will be explained later.
Vowels did not seem to fully satisfy them, now they are turning on each other!? All the better for us. Expand the program such that the first orc fights the next one. (life minus attack). Create a function Orc fight(Orc attacker, Orc defender)
. Is the defender still alive after the attack? Then he is victorious (and will be returned). Print the last man stending. Input stays the same.
INPUT: 3 OUTPUT: orc 1: attack 3, life 5. orc 2: attack 5, life 6. orc 3: attack 1, life 1. orc 1 VS 2: 2 wins (6 - 3 = 3 life left) orc 2 VS 3: 2 wins (1 - 5 = dead) orc 2 is victorious!
Tips:
Orc winner = army[0]
with the result of the fight()
function, within the loop.