2021-02-06 15:54:56 -05:00
|
|
|
//
|
|
|
|
// 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.
|
2021-02-15 16:55:44 -05:00
|
|
|
//
|
2021-02-06 15:54:56 -05:00
|
|
|
// This exercise demonstrates how we can store structs in an array and
|
2021-04-04 16:38:40 -04:00
|
|
|
// how doing so lets us print them using a loop.
|
2021-02-06 15:54:56 -05:00
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
2023-08-02 17:29:02 -04:00
|
|
|
const Role = enum {
|
2021-02-06 15:54:56 -05:00
|
|
|
wizard,
|
|
|
|
thief,
|
|
|
|
bard,
|
|
|
|
warrior,
|
|
|
|
};
|
|
|
|
|
2021-02-15 16:55:44 -05:00
|
|
|
const Character = struct {
|
2023-08-02 17:29:02 -04:00
|
|
|
role: Role,
|
2021-02-06 15:54:56 -05:00
|
|
|
gold: u32,
|
|
|
|
health: u8,
|
|
|
|
experience: u32,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() void {
|
|
|
|
var chars: [2]Character = undefined;
|
|
|
|
|
|
|
|
// Glorp the Wise
|
|
|
|
chars[0] = Character{
|
2023-08-02 17:29:02 -04:00
|
|
|
.role = Role.wizard,
|
2021-02-15 16:55:44 -05:00
|
|
|
.gold = 20,
|
|
|
|
.health = 100,
|
2021-02-06 15:54:56 -05:00
|
|
|
.experience = 10,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Please add "Zump the Loud" with the following properties:
|
|
|
|
//
|
2023-08-02 17:29:02 -04:00
|
|
|
// role bard
|
2021-02-06 15:54:56 -05:00
|
|
|
// 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:
|
2023-02-21 15:43:40 -05:00
|
|
|
for (chars, 0..) |c, num| {
|
2021-02-15 16:55:44 -05:00
|
|
|
std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
|
|
|
|
num + 1, c.gold, c.health, c.experience,
|
|
|
|
});
|
2021-02-06 15:54:56 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-04 16:38:40 -04:00
|
|
|
|
|
|
|
// If you tried running the program without adding Zump as mentioned
|
|
|
|
// above, you get what appear to be "garbage" values. In debug mode
|
|
|
|
// (which is the default), Zig writes the repeating pattern "10101010"
|
|
|
|
// in binary (or 0xAA in hex) to all undefined locations to make them
|
|
|
|
// easier to spot when debugging.
|