103 lines
3.0 KiB
Zig
103 lines
3.0 KiB
Zig
const std = @import("std");
|
|
const assert = std.debug.assert;
|
|
|
|
const map = @import("map.zig");
|
|
|
|
const stdin = std.io.getStdIn().reader();
|
|
const stdout = std.io.getStdOut().writer();
|
|
|
|
// Since this only runs on my computer, we can do whatever we want here
|
|
// const allocator =
|
|
|
|
pub fn setup () void {
|
|
|
|
}
|
|
|
|
|
|
// Sim docs
|
|
// https://github.com/mackorone/mms?tab=readme-ov-file#example
|
|
|
|
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();
|
|
|
|
// var input = std.BoundedArray(u8, 128).init(0) catch unreachable;
|
|
// const w = input.writer();
|
|
// // _ = w.write("hi") catch unreachable;
|
|
// // std.debug.print("Reading stdin {}\n", .{ input.len });
|
|
// std.log.debug("Reading std in {}", .{input.capacity()});
|
|
// _ = stdin.streamUntilDelimiter(w, '\n', input.capacity()) catch unreachable;
|
|
// std.log.debug("Read {}", .{ input });
|
|
// std.log.debug("{s}", .{ input.slice()[0..input.len] });
|
|
// const got: []u8 = input.slice();
|
|
// std.log.debug("{s}", .{ got });
|
|
}
|
|
|
|
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
|
|
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?
|
|
// stdout.write("wallBack\n");
|
|
|
|
// 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");
|
|
std.debug.panic();
|
|
}
|