diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
commit | 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch) | |
tree | d6c90700131d5b28e898881f13e2a05612e4703f /exercises/47_methods.zig | |
parent | be36352572ddb18218e1830e49316c259dea5e8c (diff) | |
download | ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2 ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip |
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate
state of soteriological release and no more exercises will be needed.
The cycle will be complete. All that will be left is perfect quietude,
freedom, and highest happiness.
Diffstat (limited to 'exercises/47_methods.zig')
-rw-r--r-- | exercises/47_methods.zig | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/exercises/47_methods.zig b/exercises/47_methods.zig deleted file mode 100644 index c8e5c17..0000000 --- a/exercises/47_methods.zig +++ /dev/null @@ -1,94 +0,0 @@ -// -// Help! Evil alien creatures have hidden eggs all over the Earth -// and they're starting to hatch! -// -// Before you jump into battle, you'll need to know four things: -// -// 1. You can attach functions to structs: -// -// const Foo = struct{ -// pub fn hello() void { -// std.debug.print("Foo says hello!\n", .{}); -// } -// } -// -// 2. A function that is a member of a struct is a "method" and is -// called with the "dot syntax" like so: -// -// Foo.hello(); -// -// 3. The NEAT feature of methods is the special parameter named -// "self" that takes an instance of that type of struct: -// -// const Bar = struct{ -// number: u32, -// -// pub fn printMe(self: *Bar) void { -// std.debug.print("{}\n", .{self.number}); -// } -// } -// -// 4. Now when you call the method on an INSTANCE of that struct -// with the "dot syntax", the instance will be automatically -// passed as the "self" parameter: -// -// const my_bar = Bar{ .number = 2000 }; -// my_bar.printMe(); // prints "2000" -// -// Okay, you're armed. -// -// Now, please zap the alien structs until they're all gone or -// Earth will be doomed! -// -const std = @import("std"); - -// Look at this hideous Alien struct. Know your enemy! -const Alien = struct { - health: u8, - - // We hate this method: - pub fn hatch(strength: u8) Alien { - return Alien{ - .health = strength * 5, - }; - } - - // We love this method: - pub fn zap(self: *Alien, damage: u8) void { - self.health -= if (damage >= self.health) self.health else damage; - } -}; - -pub fn main() void { - // Look at all of these aliens of various strengths! - var aliens = [_]Alien{ - Alien.hatch(2), - Alien.hatch(1), - Alien.hatch(3), - Alien.hatch(3), - Alien.hatch(5), - Alien.hatch(3), - }; - - var aliens_alive = aliens.len; - var heat_ray_strength: u8 = 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) { - aliens_alive = 0; - - // Loop through every alien... - for (aliens) |*alien| { - - // *** Zap the Alien Here! *** - ???.zap(heat_ray_strength); - - // If the alien's health is still above 0, it's still alive. - if (alien.health > 0) aliens_alive += 1; - } - - std.debug.print("{} aliens. ", .{aliens_alive}); - } - - std.debug.print("Earth is saved!\n", .{}); -} |