From 57a38303acce2221548ce44fb47a0b64b90c4625 Mon Sep 17 00:00:00 2001 From: Jonathan Lopez Date: Thu, 28 Apr 2022 14:22:58 -0400 Subject: Create new HeatRay struct to combat aliens --- exercises/047_methods.zig | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'exercises/047_methods.zig') diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index b967ab8..a6367e9 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) { @@ -84,7 +89,7 @@ pub fn main() void { for (aliens) |*alien| { // *** Zap the Alien Here! *** - ???.zap(heat_ray_strength); + ???.zap(???); // If the alien's health is still above 0, it's still alive. if (alien.health > 0) aliens_alive += 1; -- cgit v1.2.3-ZIG From ae78706f45b618a71fea2d73130b0aadb8bbd910 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Sun, 31 Jul 2022 11:32:51 -0400 Subject: Update ex 047 comment and patch --- exercises/047_methods.zig | 2 +- patches/patches/047_methods.patch | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'exercises/047_methods.zig') diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index a6367e9..048cfa0 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -88,7 +88,7 @@ pub fn main() void { // Loop through every alien... for (aliens) |*alien| { - // *** Zap the Alien Here! *** + // *** Zap the alien with the heat ray here! *** ???.zap(???); // If the alien's health is still above 0, it's still alive. diff --git a/patches/patches/047_methods.patch b/patches/patches/047_methods.patch index 81e733b..edd9db1 100644 --- a/patches/patches/047_methods.patch +++ b/patches/patches/047_methods.patch @@ -1,4 +1,4 @@ -87c87 -< ???.zap(heat_ray_strength); +92c92 +< ???.zap(???); --- -> alien.zap(heat_ray_strength); +> heat_ray.zap(alien); -- cgit v1.2.3-ZIG