101 lines
2.8 KiB
Zig
101 lines
2.8 KiB
Zig
const std = @import("std");
|
|
const assert = std.debug.assert;
|
|
|
|
const map = @import("map.zig");
|
|
const Point = map.Point;
|
|
const Cardinal = map.Cardinal;
|
|
|
|
const stdin = std.io.getStdIn().reader();
|
|
const stdout = std.io.getStdOut().writer();
|
|
|
|
// Sim docs
|
|
// https://github.com/mackorone/mms?tab=readme-ov-file#example
|
|
|
|
// Since this only runs on my computer, we can do whatever we want here
|
|
|
|
pub fn setup () void {}
|
|
|
|
// TODO: create a utility function that takes an allocator and reads from std in and returns a &[]u8
|
|
|
|
fn waitForAck () void {
|
|
var input = std.BoundedArray(u8, 128).init(0) catch unreachable;
|
|
_ = stdin.streamUntilDelimiter(input.writer(), '\n', input.capacity()) catch unreachable;
|
|
assert(std.mem.eql(u8, input.slice(), "ack"));
|
|
}
|
|
|
|
pub fn moveForward() void {
|
|
// print "moveForward" to std out
|
|
_ = stdout.write("moveForward\n") catch unreachable;
|
|
// wait for "ack" on std in
|
|
waitForAck();
|
|
}
|
|
|
|
pub fn turn90(direction: map.TurningDirection) void {
|
|
_ = stdout.write(switch (direction) {
|
|
.right => "turnRight\n",
|
|
.left => "turnLeft\n"
|
|
}) catch unreachable;
|
|
|
|
// wait for "ack" on std in
|
|
waitForAck();
|
|
}
|
|
|
|
pub fn turn180() void {
|
|
turn90(.left);
|
|
turn90(.left);
|
|
}
|
|
|
|
pub fn stall() noreturn {
|
|
std.debug.print("Program has completed...", .{});
|
|
while (true) {
|
|
std.time.sleep(500_000000);
|
|
std.debug.print(".", .{});
|
|
}
|
|
}
|
|
|
|
|
|
// TODO: Style all of these
|
|
// TODO: figure out what the above comment means. They're pretty stylish already
|
|
pub fn readRight() bool {
|
|
_ = stdout.write("wallRight\n") catch unreachable;
|
|
|
|
var input = std.BoundedArray(u8, 128).init(0) catch unreachable;
|
|
_ = stdin.streamUntilDelimiter(input.writer(), '\n', input.capacity()) catch unreachable;
|
|
|
|
return std.mem.eql(u8, input.slice(), "true");
|
|
}
|
|
pub fn readLeft() bool {
|
|
_ = stdout.write("wallLeft\n") catch unreachable;
|
|
|
|
var input = std.BoundedArray(u8, 128).init(0) catch unreachable;
|
|
_ = stdin.streamUntilDelimiter(input.writer(), '\n', input.capacity()) catch unreachable;
|
|
|
|
return std.mem.eql(u8, input.slice(), "true");
|
|
}
|
|
pub fn readFront() bool {
|
|
_ = stdout.write("wallFront\n") catch unreachable;
|
|
|
|
var input = std.BoundedArray(u8, 128).init(0) catch unreachable;
|
|
_ = stdin.streamUntilDelimiter(input.writer(), '\n', input.capacity()) catch unreachable;
|
|
|
|
return std.mem.eql(u8, input.slice(), "true");
|
|
}
|
|
pub fn readBack() bool {
|
|
// What?
|
|
std.debug.panic();
|
|
}
|
|
|
|
pub fn showWall(p: Point, direction: Cardinal) void {
|
|
const dir :u8 = switch (direction) {
|
|
.north => 'n',
|
|
.south => 's',
|
|
.east => 'e',
|
|
.west => 'w',
|
|
};
|
|
stdout.print("setWall {d} {d} {c}\n", .{ p.x, p.y, dir }) catch unreachable;
|
|
}
|
|
|
|
pub fn writeCellScore(p: Point, score: usize) void {
|
|
stdout.print("setText {d} {d} {d}\n", .{p.x, p.y, score}) catch unreachable;
|
|
}
|