Lesson 12 of 34 · Methods
Boss: The Echoing Hydra
The Echoing Hydra blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
Deep in the flooded vaults beneath the Citadel lurks the Echoing Hydra. Its heads repeat every spell cast at them, and when one is struck down, its echo returns. Only a precise warrior — one who has mastered methods — can break its rhythm. Every method you complete silences one of its heads.
Battle plan
This fight combines every tool from Chapter 2: recursive methods, overloads, optional and named parameters, and out parameters.
- The Hydra's heads attack in synchronised waves. The greatest common divisor of their rhythms tells you when they all strike together. Euclid found the recursive rule 2,300 years ago:
gcd(a, 0) = aandgcd(a, b) = gcd(b, a % b). - Each head follows the Collatz pattern: if its strength
nis even it halves, if it is odd it becomes3n + 1. Every head eventually reaches 1 — count how many steps it takes. - Strike orders arrive as text like
"neck:25"— a target and a damage value separated by a colon.
Assignment
Complete the methods in Program:
Gcd(a, b)returns the greatest common divisor of two non-negative numbers, recursively:Gcd(12, 18)is6,Gcd(17, 5)is1andGcd(0, 9)is9.- An overload
Gcd(a, b, c)returns the GCD of three numbers by reusing the first one:Gcd(24, 36, 60)is12. CollatzSteps(n)returns the number of steps untiln(≥ 1) reaches 1, recursively:CollatzSteps(1)is0,CollatzSteps(6)is8.Damage(baseDamage, combo = 1, critical = false)returnsbaseDamage * combo, doubled whencriticalis true:Damage(10)is10,Damage(10, combo: 3)is30,Damage(10, critical: true)is20.TryParseStrike(text, out target, out damage)parses"neck:25"intotarget = "neck"anddamage = 25and returnstrue. Text without a colon, with an empty target or with a damage that is not a positive whole number returnsfalsewithtarget = ""anddamage = 0.FormatHeads(count)returns"1 head"for one and"<count> heads"otherwise ("0 heads","9 heads").
Premium lesson
Unlock the full Learn C# course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .