'F' → Fullscreen
Today you fight a dragon in a new program named 2-3-dragon-duel. A knight and a dragon trade blows until one of them falls, and every number in the fight — each hit, each roll of the dice, each potion — is worked out by a method that hands its answer back. The dragon is stronger than you. By the end, you will need a plan to beat it.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
Damage takes an attack and a defense and returns how much damage gets through: the attack minus the defense. A blow too weak to get through still does 1, so the first if returns early.return attack - defense; and run it. Write down the error code and what it said. Put the line back.int to void in the header of Damage and run it. Write down the error on line 4, and what the error on line 14 says about return. Then change it back to int.int knightAttack = 8;int dragonDefense = 3;
int hit = Damage(knightAttack, dragonDefense);Console.WriteLine($"The knight hits the dragon for {hit}.");
int scratch = Damage(2, dragonDefense);Console.WriteLine($"A pebble hits the dragon for {scratch}.");
int Damage(int attack, int defense){ if (attack <= defense) { return 1; }
return attack - defense;}Status turns a number of hit points into a word the player can read at a glance. It returns a string, and it checks from the bottom up with early returns.Status below Damage, and the four test lines to the main program. Before you run it, write down the word you expect for each of the four. Then run it and compare.Status(40) and Status(15), the two boundaries, and write down which band each one landed in.int knightAttack = 8;int dragonDefense = 3;
int hit = Damage(knightAttack, dragonDefense);Console.WriteLine($"The knight hits the dragon for {hit}.");
int scratch = Damage(2, dragonDefense);Console.WriteLine($"A pebble hits the dragon for {scratch}.");
Console.WriteLine($"At 60 HP: {Status(60)}");Console.WriteLine($"At 30 HP: {Status(30)}");Console.WriteLine($"At 10 HP: {Status(10)}");Console.WriteLine($"At 0 HP: {Status(0)}");
int Damage(int attack, int defense){ if (attack <= defense) { return 1; }
return attack - defense;}
string Status(int hp){ if (hp <= 0) { return "defeated"; }
if (hp < 15) { return "barely standing"; }
if (hp < 40) { return "wounded"; }
return "steady";}Roll at the bottom. Roll(6) returns a random number from 1 to 6, like a die — Activity 1.15's Next with its + 1.knightAttack + Roll(6) is worked out first, and that sum is the argument to Damage. A call inside a call, inside arithmetic.int knightHp = 50;int dragonHp = 80;int knightAttack = 8;int knightDefense = 5;int dragonAttack = 9;int dragonDefense = 3;
Console.WriteLine("A dragon blocks the mountain pass. Press Enter to swing your sword.");
while (knightHp > 0 && dragonHp > 0){ Console.ReadLine();
int hit = Damage(knightAttack + Roll(6), dragonDefense); dragonHp = dragonHp - hit; Console.WriteLine($"You hit the dragon for {hit}. Dragon: {dragonHp} HP, {Status(dragonHp)}.");
if (dragonHp > 0) { int bite = Damage(dragonAttack + Roll(6), knightDefense); knightHp = knightHp - bite; Console.WriteLine($"The dragon bites you for {bite}. Knight: {knightHp} HP, {Status(knightHp)}."); }}
if (knightHp > 0){ Console.WriteLine("The dragon is defeated. The pass is open!");}else{ Console.WriteLine("The knight falls. The dragon keeps the pass.");}
int Damage(int attack, int defense){ if (attack <= defense) { return 1; }
return attack - defense;}
string Status(int hp){ if (hp <= 0) { return "defeated"; }
if (hp < 15) { return "barely standing"; }
if (hp < 40) { return "wounded"; }
return "steady";}
int Roll(int sides){ return Random.Shared.Next(1, sides + 1);}Heal. It takes the knight's HP, the amount a potion heals, and the knight's maximum HP, and returns the new HP: the HP plus the amount, but never more than the maximum. A potion at 45 HP gives 50, not 60.int knightHp = 50;int dragonHp = 80;int knightAttack = 8;int knightDefense = 5;int dragonAttack = 9;int dragonDefense = 3;int potions = 3;
Console.WriteLine("A dragon blocks the mountain pass.");
while (knightHp > 0 && dragonHp > 0){ Console.Write($"Attack (1) or drink a potion (2)? Potions left: {potions}. "); int choice = int.Parse(Console.ReadLine());
if (choice == 2 && potions > 0) { knightHp = Heal(knightHp, 15, 50); potions = potions - 1; Console.WriteLine($"You drink a potion. Knight: {knightHp} HP, {Status(knightHp)}."); } else { int hit = Damage(knightAttack + Roll(6), dragonDefense); dragonHp = dragonHp - hit; Console.WriteLine($"You hit the dragon for {hit}. Dragon: {dragonHp} HP, {Status(dragonHp)}."); }
if (dragonHp > 0) { int bite = Damage(dragonAttack + Roll(6), knightDefense); knightHp = knightHp - bite; Console.WriteLine($"The dragon bites you for {bite}. Knight: {knightHp} HP, {Status(knightHp)}."); }}
if (knightHp > 0){ Console.WriteLine("The dragon is defeated. The pass is open!");}else{ Console.WriteLine("The knight falls. The dragon keeps the pass.");}
int Damage(int attack, int defense){ if (attack <= defense) { return 1; }
return attack - defense;}
string Status(int hp){ if (hp <= 0) { return "defeated"; }
if (hp < 15) { return "barely standing"; }
if (hp < 40) { return "wounded"; }
return "steady";}
int Roll(int sides){ return Random.Shared.Next(1, sides + 1);}
// Write a method named Heal here. It takes the knight's hp, the amount// a potion heals, and the knight's maxHp, and returns the new hp --// hp plus the amount, but never more than maxHp.Three changes, one at a time. Write down exactly what happens, and put each one back before the next.
Status, delete the last line, return "steady";.Damage, add Console.WriteLine("Ouch."); on a new line right after return attack - defense;, still inside the braces. Then play a few turns.Status, swap the hp < 15 block and the hp < 40 block, so 40 is checked first. Play until the knight drops below 15 HP, and read the word.One will not build, one builds with a warning, and one builds cleanly and gives a wrong answer. Say which is which. For the error, say which HP values would take the path through Status that has no return. For the wrong answer, say which HP values get the wrong word and why the rest are still right.
There is no code block for this task. When it is over, your program is exactly what it was at the end of Task 4.
IsCritical that takes no parameters and returns a bool: true one time in ten. Its body is one return line that uses Roll.IsCritical() is true, double the hit and print Critical hit! before the hit line.bool can go straight into an if, the same way a returned number went into arithmetic. Play a few fights and check that crits happen, but not often.The reference sheet writes a returning procedure with RETURN(expression) in its body, and captures the result with result ← procName(arg1, arg2, ...).
double total = AddTax(20);
double AddTax(double price){ return price * 1.08;}PROCEDURE addTax(price){ RETURN(price * 1.08)}
total ← addTax(20)RETURN(price * 1.08), never RETURN price * 1.08. C#'s return has no parentheses, so this is the easiest notation detail in the quarter to get backward. It is capitalized, like every exam keyword.
A returning procedure starts with PROCEDURE, exactly like one that only displays. The only way to tell them apart on the exam is to look inside for a RETURN. C# tells you in the header, with double or void.
The reference sheet says RETURN may appear at any point in a procedure and causes an immediate return. Early returns work the same way on the exam as in your Status, and a line after a RETURN never runs there either — but no warning will point it out.
One real fight from the finished program, won with one potion left. Your rolls will be different.
A dragon blocks the mountain pass.Attack (1) or drink a potion (2)? Potions left: 3. 1 [Enter]You hit the dragon for 8. Dragon: 72 HP, steady.The dragon bites you for 8. Knight: 42 HP, steady.Attack (1) or drink a potion (2)? Potions left: 3. 1 [Enter]You hit the dragon for 11. Dragon: 61 HP, steady.The dragon bites you for 5. Knight: 37 HP, wounded.Attack (1) or drink a potion (2)? Potions left: 3. 1 [Enter]You hit the dragon for 9. Dragon: 52 HP, steady.The dragon bites you for 6. Knight: 31 HP, wounded.Attack (1) or drink a potion (2)? Potions left: 3. 1 [Enter]You hit the dragon for 6. Dragon: 46 HP, steady.The dragon bites you for 9. Knight: 22 HP, wounded.Attack (1) or drink a potion (2)? Potions left: 3. 1 [Enter]You hit the dragon for 9. Dragon: 37 HP, wounded.The dragon bites you for 5. Knight: 17 HP, wounded.Attack (1) or drink a potion (2)? Potions left: 3. 2 [Enter]You drink a potion. Knight: 32 HP, wounded.The dragon bites you for 9. Knight: 23 HP, wounded.Attack (1) or drink a potion (2)? Potions left: 2. 1 [Enter]You hit the dragon for 11. Dragon: 26 HP, wounded.The dragon bites you for 10. Knight: 13 HP, barely standing.Attack (1) or drink a potion (2)? Potions left: 2. 1 [Enter]You hit the dragon for 7. Dragon: 19 HP, wounded.The dragon bites you for 7. Knight: 6 HP, barely standing.Attack (1) or drink a potion (2)? Potions left: 2. 2 [Enter]You drink a potion. Knight: 21 HP, wounded.The dragon bites you for 7. Knight: 14 HP, barely standing.Attack (1) or drink a potion (2)? Potions left: 1. 1 [Enter]You hit the dragon for 9. Dragon: 10 HP, barely standing.The dragon bites you for 8. Knight: 6 HP, barely standing.Attack (1) or drink a potion (2)? Potions left: 1. 1 [Enter]You hit the dragon for 10. Dragon: 0 HP, defeated.The dragon is defeated. The pass is open!Status gives the right word in all four bands, and you recorded where 40 and 15 landHeal never takes the knight above 50 HPAnswer the following questions before submitting your work.
Damage hands back a number instead of printing it. Name two things your duel does with that number that it could not do if Damage printed it instead.return "steady"; gave “not all code paths return a value.” Explain what a code path is, and which HP values would have taken the path with no return.Status gave the wrong word below 15 HP and the right word everywhere else. Explain why, and what that says about choosing values to test a method with.Submit four things to the dropbox: