mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-08 11:20:46 -05:00
047-055 completed
This commit is contained in:
parent
1a1b7937f6
commit
0b00b44e51
10 changed files with 32 additions and 26 deletions
|
@ -88,7 +88,7 @@ pub fn main() void {
|
|||
for (&aliens) |*alien| {
|
||||
|
||||
// *** Zap the alien with the heat ray here! ***
|
||||
???.zap(???);
|
||||
heat_ray.zap(alien);
|
||||
|
||||
// If the alien's health is still above 0, it's still alive.
|
||||
if (alien.health > 0) aliens_alive += 1;
|
||||
|
|
|
@ -54,7 +54,7 @@ fn visitElephants(first_elephant: *Elephant) void {
|
|||
|
||||
// This gets the next elephant or stops:
|
||||
// which method do we want here?
|
||||
e = if (e.hasTail()) e.??? else break;
|
||||
e = if (e.hasTail()) e.getTail() else break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,13 @@ const Elephant = struct {
|
|||
// Your Elephant trunk methods go here!
|
||||
// ---------------------------------------------------
|
||||
|
||||
???
|
||||
pub fn getTrunk(self: *Elephant) *Elephant {
|
||||
return self.trunk.?;
|
||||
}
|
||||
|
||||
pub fn hasTrunk(self: *Elephant) bool {
|
||||
return (self.trunk != null);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
|
||||
|
|
|
@ -65,10 +65,10 @@ const std = @import("std");
|
|||
const Err = error{Cthulhu};
|
||||
|
||||
pub fn main() void {
|
||||
var first_line1: *const [16]u8 = ???;
|
||||
var first_line1: *const [16]u8 = undefined;
|
||||
first_line1 = "That is not dead";
|
||||
|
||||
var first_line2: Err!*const [21]u8 = ???;
|
||||
var first_line2: Err!*const [21]u8 = undefined;
|
||||
first_line2 = "which can eternal lie";
|
||||
|
||||
// Note we need the "{!s}" format for the error union string.
|
||||
|
@ -77,8 +77,8 @@ pub fn main() void {
|
|||
printSecondLine();
|
||||
}
|
||||
|
||||
fn printSecondLine() ??? {
|
||||
var second_line2: ?*const [18]u8 = ???;
|
||||
fn printSecondLine() void {
|
||||
var second_line2: ?*const [18]u8 = null;
|
||||
second_line2 = "even death may die";
|
||||
|
||||
std.debug.print("And with strange aeons {s}.\n", .{second_line2.?});
|
||||
|
|
|
@ -87,7 +87,7 @@ pub fn main() void {
|
|||
// Let's assign the std.debug.print function to a const named
|
||||
// "print" so that we can use this new name later!
|
||||
|
||||
const print = ???;
|
||||
const print = std.debug.print;
|
||||
|
||||
// Now let's look at assigning and pointing to values in Zig.
|
||||
//
|
||||
|
@ -152,13 +152,13 @@ pub fn main() void {
|
|||
print("XP before:{}, ", .{glorp.experience});
|
||||
|
||||
// Fix 1 of 2 goes here:
|
||||
levelUp(glorp, reward_xp);
|
||||
levelUp(&glorp, reward_xp);
|
||||
|
||||
print("after:{}.\n", .{glorp.experience});
|
||||
}
|
||||
|
||||
// Fix 2 of 2 goes here:
|
||||
fn levelUp(character_access: Character, xp: u32) void {
|
||||
fn levelUp(character_access: *Character, xp: u32) void {
|
||||
character_access.experience += xp;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,8 +32,8 @@ pub fn main() void {
|
|||
var cards = [8]u8{ 'A', '4', 'K', '8', '5', '2', 'Q', 'J' };
|
||||
|
||||
// Please put the first 4 cards in hand1 and the rest in hand2.
|
||||
const hand1: []u8 = cards[???];
|
||||
const hand2: []u8 = cards[???];
|
||||
const hand1: []u8 = cards[0..4];
|
||||
const hand2: []u8 = cards[4..8];
|
||||
|
||||
std.debug.print("Hand1: ", .{});
|
||||
printHand(hand1);
|
||||
|
@ -43,7 +43,7 @@ pub fn main() void {
|
|||
}
|
||||
|
||||
// Please lend this function a hand. A u8 slice hand, that is.
|
||||
fn printHand(hand: ???) void {
|
||||
fn printHand(hand: []u8) void {
|
||||
for (hand) |h| {
|
||||
std.debug.print("{u} ", .{h});
|
||||
}
|
||||
|
|
|
@ -17,19 +17,19 @@ const std = @import("std");
|
|||
pub fn main() void {
|
||||
const scrambled = "great base for all your justice are belong to us";
|
||||
|
||||
const base1: []u8 = scrambled[15..23];
|
||||
const base2: []u8 = scrambled[6..10];
|
||||
const base3: []u8 = scrambled[32..];
|
||||
const base1: []const u8 = scrambled[15..23];
|
||||
const base2: []const u8 = scrambled[6..10];
|
||||
const base3: []const u8 = scrambled[32..];
|
||||
printPhrase(base1, base2, base3);
|
||||
|
||||
const justice1: []u8 = scrambled[11..14];
|
||||
const justice2: []u8 = scrambled[0..5];
|
||||
const justice3: []u8 = scrambled[24..31];
|
||||
const justice1: []const u8 = scrambled[11..14];
|
||||
const justice2: []const u8 = scrambled[0..5];
|
||||
const justice3: []const u8 = scrambled[24..31];
|
||||
printPhrase(justice1, justice2, justice3);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
fn printPhrase(part1: []u8, part2: []u8, part3: []u8) void {
|
||||
fn printPhrase(part1: []const u8, part2: []const u8, part3: []const u8) void {
|
||||
std.debug.print("'{s} {s} {s}.' ", .{ part1, part2, part3 });
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ pub fn main() void {
|
|||
// we can CONVERT IT TO A SLICE. (Hint: we do know the length!)
|
||||
//
|
||||
// Please fix this line so the print statement below can print it:
|
||||
const zen12_string: []const u8 = zen_manyptr;
|
||||
const zen12_string: []const u8 = zen_manyptr[0..21];
|
||||
|
||||
// Here's the moment of truth!
|
||||
std.debug.print("{s}\n", .{zen12_string});
|
||||
|
|
|
@ -59,8 +59,8 @@ pub fn main() void {
|
|||
std.debug.print("Insect report! ", .{});
|
||||
|
||||
// Oops! We've made a mistake here.
|
||||
printInsect(ant, AntOrBee.c);
|
||||
printInsect(bee, AntOrBee.c);
|
||||
printInsect(ant, AntOrBee.a);
|
||||
printInsect(bee, AntOrBee.b);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
|
|
@ -44,14 +44,14 @@ pub fn main() void {
|
|||
std.debug.print("Insect report! ", .{});
|
||||
|
||||
// Could it really be as simple as just passing the union?
|
||||
printInsect(???);
|
||||
printInsect(???);
|
||||
printInsect(ant);
|
||||
printInsect(bee);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
fn printInsect(insect: Insect) void {
|
||||
switch (???) {
|
||||
switch (insect) {
|
||||
.still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}),
|
||||
.flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue