221V 2 days ago
parent
commit
adeb2bca80
1 changed files with 56 additions and 55 deletions
  1. 56 55
      exercises/043_pointers5.zig

+ 56 - 55
exercises/043_pointers5.zig

@@ -34,66 +34,67 @@
 //
 //
 const std = @import("std");
 const std = @import("std");
 
 
-const Class = enum {
-    wizard,
-    thief,
-    bard,
-    warrior,
+const Class = enum{
+  wizard,
+  thief,
+  bard,
+  warrior,
 };
 };
 
 
-const Character = struct {
-    class: Class,
-    gold: u32,
-    health: u8 = 100, // You can provide default values
-    experience: u32,
-
-    // I need to use the '?' here to allow for a null value. But
-    // I don't explain it until later. Please don't tell anyone.
-    mentor: ?*Character = null,
+const Character = struct{
+  class: Class,
+  gold: u32,
+  health: u8 = 100, // You can provide default values
+  experience: u32,
+  
+  // I need to use the '?' here to allow for a null value. But
+  // I don't explain it until later. Please don't tell anyone.
+  mentor: ?*Character = null,
 };
 };
 
 
-pub fn main() void {
-    var mighty_krodor = Character{
-        .class = Class.wizard,
-        .gold = 10000,
-        .experience = 2340,
-    };
-
-    var glorp = Character{ // Glorp!
-        .class = Class.wizard,
-        .gold = 10,
-        .experience = 20,
-        .mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor
-    };
-
-    // FIX ME!
-    // Please pass Glorp to printCharacter():
-    printCharacter(???);
+pub fn main() void{
+  var mighty_krodor = Character{
+    .class = Class.wizard,
+    .gold = 10000,
+    .experience = 2340,
+  };
+  
+  var glorp = Character{ // Glorp!
+    .class = Class.wizard,
+    .gold = 10,
+    .experience = 20,
+    .mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor
+  };
+  
+  // FIX ME!
+  // Please pass Glorp to printCharacter():
+  printCharacter(&glorp);
 }
 }
 
 
 // Note how this function's "c" parameter is a pointer to a Character struct.
 // Note how this function's "c" parameter is a pointer to a Character struct.
-fn printCharacter(c: *Character) void {
-    // Here's something you haven't seen before: when switching an enum, you
-    // don't have to write the full enum name. Zig understands that ".wizard"
-    // means "Class.wizard" when we switch on a Class enum value:
-    const class_name = switch (c.class) {
-        .wizard => "Wizard",
-        .thief => "Thief",
-        .bard => "Bard",
-        .warrior => "Warrior",
-    };
-
-    std.debug.print("{s} (G:{} H:{} XP:{})\n", .{
-        class_name,
-        c.gold,
-        c.health,
-        c.experience,
-    });
-
-    // Checking an "optional" value and capturing it will be
-    // explained later (this pairs with the '?' mentioned above.)
-    if (c.mentor) |mentor| {
-        std.debug.print("  Mentor: ", .{});
-        printCharacter(mentor);
-    }
+fn printCharacter(c: *Character) void{
+  // Here's something you haven't seen before: when switching an enum, you
+  // don't have to write the full enum name. Zig understands that ".wizard"
+  // means "Class.wizard" when we switch on a Class enum value:
+  const class_name = switch(c.class){
+    .wizard => "Wizard",
+    .thief => "Thief",
+    .bard => "Bard",
+    .warrior => "Warrior",
+  };
+  
+  std.debug.print("{s} (G:{} H:{} XP:{})\n", .{
+    class_name,
+    c.gold,
+    c.health,
+    c.experience,
+  });
+  
+  // Checking an "optional" value and capturing it will be
+  // explained later (this pairs with the '?' mentioned above.)
+  if(c.mentor) |mentor|{
+    std.debug.print("  Mentor: ", .{});
+    printCharacter(mentor);
+  }
 }
 }
+