mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
Added Quiz 5 for pointers. Elephants!!!
This commit is contained in:
parent
55ad7c32f2
commit
961cf22b88
3 changed files with 47 additions and 2 deletions
|
@ -44,7 +44,7 @@ const Class = enum{
|
||||||
const Character = struct{
|
const Character = struct{
|
||||||
class: Class,
|
class: Class,
|
||||||
gold: u32,
|
gold: u32,
|
||||||
health: u8,
|
health: u8 = 100, // <--- You can also fields a default value!
|
||||||
experience: u32,
|
experience: u32,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -52,7 +52,6 @@ pub fn main() void {
|
||||||
var glorp = Character{
|
var glorp = Character{
|
||||||
.class = Class.wizard,
|
.class = Class.wizard,
|
||||||
.gold = 10,
|
.gold = 10,
|
||||||
.health = 100,
|
|
||||||
.experience = 20,
|
.experience = 20,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
45
exercises/44_quiz5.zig
Normal file
45
exercises/44_quiz5.zig
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
//
|
||||||
|
// "Elephants walking
|
||||||
|
// Along the trails
|
||||||
|
//
|
||||||
|
// Are holding hands
|
||||||
|
// By holding tails."
|
||||||
|
//
|
||||||
|
// from Holding Hands
|
||||||
|
// by Lenore M. Link
|
||||||
|
//
|
||||||
|
const std = @import("std"); // single quotes
|
||||||
|
|
||||||
|
const Elephant = struct{
|
||||||
|
letter: u8,
|
||||||
|
tail: *Elephant = undefined,
|
||||||
|
visited: bool = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var elephantA = Elephant{ .letter = 'A' };
|
||||||
|
// (Please add Elephant B here!)
|
||||||
|
var elephantC = Elephant{ .letter = 'C' };
|
||||||
|
|
||||||
|
// Link the elephants so that each tail "points" to the next elephant.
|
||||||
|
// They make a circle: A->B->C->A...
|
||||||
|
elephantA.tail = &elephantB;
|
||||||
|
// (Please link Elephant B's tail to Elephant C here!)
|
||||||
|
elephantC.tail = &elephantA;
|
||||||
|
|
||||||
|
visitElephants(&elephantA);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function visits all elephants once, starting with the
|
||||||
|
// first elephant and following the tails to the next elephant.
|
||||||
|
// If we did not "mark" the elephants as visited (by setting
|
||||||
|
// visited=true), then this would loop infinitely!
|
||||||
|
fn visitElephants(first_elephant: *Elephant) void {
|
||||||
|
var e = first_elephant;
|
||||||
|
|
||||||
|
while (!e.visited) {
|
||||||
|
std.debug.print("Elephant {u}. ", .{e.letter});
|
||||||
|
e.visited = true;
|
||||||
|
e = e.tail;
|
||||||
|
}
|
||||||
|
}
|
1
ziglings
1
ziglings
|
@ -115,6 +115,7 @@ check_it 40_pointers2.zig "a: 12, b: 12"
|
||||||
check_it 41_pointers3.zig "foo=6, bar=11"
|
check_it 41_pointers3.zig "foo=6, bar=11"
|
||||||
check_it 42_pointers4.zig "num: 5, more_nums: 1 1 5 1"
|
check_it 42_pointers4.zig "num: 5, more_nums: 1 1 5 1"
|
||||||
check_it 43_pointers5.zig "Wizard (G:10 H:100 XP:20)"
|
check_it 43_pointers5.zig "Wizard (G:10 H:100 XP:20)"
|
||||||
|
check_it 44_quiz5.zig "Elephant A. Elephant B. Elephant C." "Oh no! We forgot Elephant B!"
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo " __ __ _ "
|
echo " __ __ _ "
|
||||||
|
|
Loading…
Reference in a new issue