diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-02-06 15:54:56 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-02-06 15:54:56 -0500 |
commit | 507355ec3b1066c707e19816b86ac1fb56fc0385 (patch) | |
tree | 503c6e848879575646b39fab63d7888ee4d67576 /38_structs2.zig | |
parent | 2cded107cd75a4024d9d0f76055ef48432301fad (diff) | |
download | ziglings-507355ec3b1066c707e19816b86ac1fb56fc0385.tar.gz ziglings-507355ec3b1066c707e19816b86ac1fb56fc0385.tar.bz2 ziglings-507355ec3b1066c707e19816b86ac1fb56fc0385.tar.xz ziglings-507355ec3b1066c707e19816b86ac1fb56fc0385.zip |
Added ex. 37,38 structs
Diffstat (limited to '38_structs2.zig')
-rw-r--r-- | 38_structs2.zig | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/38_structs2.zig b/38_structs2.zig new file mode 100644 index 0000000..9a67c25 --- /dev/null +++ b/38_structs2.zig @@ -0,0 +1,53 @@ +// +// Grouping values in structs is not merely convenient. It also allows +// us to treat the values as a single item when storing them, passing +// them to functions, etc. +// +// This exercise demonstrates how we can store structs in an array and +// how doing so lets us print them all (both) using a loop. +// +const std = @import("std"); + +const Class = enum{ + wizard, + thief, + bard, + warrior, +}; + +const Character = struct{ + class: Class, + gold: u32, + health: u8, + experience: u32, +}; + +pub fn main() void { + var chars: [2]Character = undefined; + + // Glorp the Wise + chars[0] = Character{ + .class = Class.wizard, + .gold = 20, + .health = 100, + .experience = 10, + }; + + // Please add "Zump the Loud" with the following properties: + // + // class bard + // gold 10 + // health 100 + // experience 20 + // + // Feel free to run this program without adding Zump. What does + // it do and why? + + // Printing all RPG characters in a loop: + for (chars) |c, num| { + std.debug.print("Character {} - G:{} H:{} XP:{}\n", + .{num+1, c.gold, c.health, c.experience}); + } + + std.debug.print("\n", .{}); +} |