aboutsummaryrefslogtreecommitdiff
path: root/exercises/047_methods.zig
diff options
context:
space:
mode:
authorJonathan Lopez <jonathanglopez@gmail.com>2022-04-28 14:22:58 -0400
committerJonathan Lopez <jonathanglopez@gmail.com>2022-04-28 14:22:58 -0400
commit57a38303acce2221548ce44fb47a0b64b90c4625 (patch)
tree3b0f1d93fe01c4d17a1c49a2ea80e1fd014ea3b9 /exercises/047_methods.zig
parentf72c2aa5341ccbe259f7ac655953386e9441d9ee (diff)
downloadziglings-57a38303acce2221548ce44fb47a0b64b90c4625.tar.gz
ziglings-57a38303acce2221548ce44fb47a0b64b90c4625.tar.bz2
ziglings-57a38303acce2221548ce44fb47a0b64b90c4625.tar.xz
ziglings-57a38303acce2221548ce44fb47a0b64b90c4625.zip
Create new HeatRay struct to combat aliens
Diffstat (limited to 'exercises/047_methods.zig')
-rw-r--r--exercises/047_methods.zig13
1 files changed, 9 insertions, 4 deletions
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;