diff options
author | Dave Gauer <dave@ratfactor.com> | 2022-07-31 11:35:01 -0400 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2022-07-31 11:35:01 -0400 |
commit | 0e64778f3c27a4b42cc94f12c8c7029daed1bd8a (patch) | |
tree | b3fc17bbf9656e2a1b4dbf3befe4ae11c3b68905 /exercises/047_methods.zig | |
parent | 98ebdcbff84deb9168d5670dceeea6dc9ea7993c (diff) | |
parent | ae78706f45b618a71fea2d73130b0aadb8bbd910 (diff) | |
download | ziglings-0e64778f3c27a4b42cc94f12c8c7029daed1bd8a.tar.gz ziglings-0e64778f3c27a4b42cc94f12c8c7029daed1bd8a.tar.bz2 ziglings-0e64778f3c27a4b42cc94f12c8c7029daed1bd8a.tar.xz ziglings-0e64778f3c27a4b42cc94f12c8c7029daed1bd8a.zip |
Merge branch 'heatray'
Diffstat (limited to 'exercises/047_methods.zig')
-rw-r--r-- | exercises/047_methods.zig | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index b967ab8..048cfa0 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -55,10 +55,15 @@ const Alien = struct { .health = strength * 5, }; } +}; + +// Your trusty weapon. Zap those aliens! +const HeatRay = struct { + damage: u8, // We love this method: - pub fn zap(self: *Alien, damage: u8) void { - self.health -= if (damage >= self.health) self.health else damage; + pub fn zap(self: *HeatRay, alien: *Alien) void { + alien.health -= if (self.damage >= alien.health) alien.health else self.damage; } }; @@ -74,7 +79,7 @@ pub fn main() void { }; var aliens_alive = aliens.len; - var heat_ray_strength: u8 = 7; // We've been given a heat ray weapon. + var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon. // We'll keep checking to see if we've killed all the aliens yet. while (aliens_alive > 0) { @@ -83,8 +88,8 @@ pub fn main() void { // Loop through every alien... for (aliens) |*alien| { - // *** Zap the Alien Here! *** - ???.zap(heat_ray_strength); + // *** Zap the alien with the heat ray here! *** + ???.zap(???); // If the alien's health is still above 0, it's still alive. if (alien.health > 0) aliens_alive += 1; |