initial
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const std = @import("std");
|
||||
const io = std.io;
|
||||
const mem = std.mem;
|
||||
const os = std.os;
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
std.log.info("All your codebase are belong to us.", .{});
|
||||
|
||||
//Get input
|
||||
const stdIn = io.getStdIn();
|
||||
const stdOut = io.getStdOut();
|
||||
var quit: bool = false;
|
||||
|
||||
var buffer: [1000]u8 = undefined;
|
||||
// var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
||||
// const allocator = fba.allocator();
|
||||
|
||||
try stdOut.writer().print(">", .{});
|
||||
while (!quit) {
|
||||
const input = (try nextLine(stdIn.reader(), &buffer)).?;
|
||||
|
||||
try parser(input);
|
||||
try stdOut.writer().print(">", .{});
|
||||
}
|
||||
}
|
||||
|
||||
fn nextLine(reader: anytype, buffer: []u8) !?[]const u8 {
|
||||
var line = (try reader.readUntilDelimiterOrEof(
|
||||
buffer,
|
||||
'\n',
|
||||
)) orelse return null;
|
||||
// trim annoying windows-only carriage return character
|
||||
if (@import("builtin").os.tag == .windows) {
|
||||
return std.mem.trimRight(u8, line, "\r");
|
||||
} else {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Create command parser
|
||||
fn parser(query: []const u8) !void {
|
||||
if (mem.eql(u8, query, "quit")) {
|
||||
try quitApp();
|
||||
}
|
||||
}
|
||||
|
||||
fn quitApp() !void {
|
||||
const stdOut = io.getStdOut();
|
||||
try stdOut.writer().print("Bye!\n", .{});
|
||||
os.exit(0);
|
||||
}
|
||||
|
||||
//TODO: Create inital settings
|
||||
|
||||
test "basic test" {
|
||||
try std.testing.expectEqual(10, 3 + 7);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
const testing = @import("std").testing;
|
||||
// Snooker game rules
|
||||
// 15 red balls to start.
|
||||
|
||||
const ballValue = enum(u8) {
|
||||
red = 1,
|
||||
yellow = 2,
|
||||
green = 3,
|
||||
brown = 4,
|
||||
blue = 5,
|
||||
pink = 6,
|
||||
black = 7,
|
||||
};
|
||||
|
||||
const Table = struct {
|
||||
///Holds all balls in play.
|
||||
var balls: [21]ballValue = .{ ballValue.red ** 15, ballValue.yellow, ballValue.green, ballValue.brown, ballValue.blue, ballValue.pink, ballValue.black };
|
||||
var lastPottedBall: ballValue = undefined;
|
||||
var currentStriker: u8 = 0;
|
||||
|
||||
/// Removes a ball from play, and adds the score to the current player.
|
||||
pub fn potBall(self: Table, pottedBall: ballValue) void {
|
||||
//TODO: Remove Red ball from play.
|
||||
//TODO: increment current player's score
|
||||
|
||||
if (pottedBall == lastPottedBall) {
|
||||
//This is an illegal snooker play.
|
||||
//End the turn & Red ball is NOT respotted.
|
||||
}
|
||||
|
||||
if (pottedBall == ballValue.red) {
|
||||
//remove ball from play
|
||||
var ballIndex = findBall(pottedBall);
|
||||
self.balls.indexOfScalar(ballValue.red);
|
||||
self.balls[ballIndex] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn findBall(self: Table, ballScore: ballValue) error{notFound}!u8 {
|
||||
for (self.balls) |ball, index| {
|
||||
if (ball == ballScore) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return error.notFound;
|
||||
}
|
||||
};
|
||||
|
||||
test "potRed" {
|
||||
Table.potBall(ballValue.red);
|
||||
|
||||
//expect red ball to be removed.
|
||||
}
|
||||
Reference in New Issue
Block a user