Initial Commit

This commit is contained in:
2026-04-12 08:41:30 -05:00
commit f812b09c8c
7 changed files with 427 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
const std = @import("std");
const mem = std.mem;
const Allocator = mem.Allocator;
/// Matrix is a part of the game board
pub fn Matrix(comptime T: type, comptime width: usize, comptime height: usize) type {
const size = width * height;
const MatrixRowIterator = RowIterator(T, width);
return struct {
/// data is the game board itself.
data: []T,
/// init creates the chunk, storing the bytes in memory.
pub fn initZero(gpa: Allocator) !@This() {
const data = try gpa.alloc(T, size);
@memset(data, mem.zeroes(T));
return .{ .data = data };
}
/// init creates the chunk, storing the bytes in memory.
pub fn init(gpa: Allocator, zero: T) !@This() {
const data = try gpa.alloc(T, size);
@memset(data, zero);
return .{ .data = data };
}
/// deinit frees the memory utilized to create the chunk
pub fn deinit(self: *@This(), gpa: Allocator) void {
gpa.free(self.data);
}
// set sets the value of the specified location in memory
pub fn set(self: *@This(), x: usize, y: usize, val: T) void {
const index = (width * y) + x;
self.data[index] = val;
}
// set sets the value of the specified location in memory
pub fn get(self: *const @This(), x: usize, y: usize) T {
const index = (width * y) + x;
return self.data[index];
}
pub fn row_iterator(self: *@This()) MatrixRowIterator {
return .init(self.data);
}
};
}
test "TestMatrix" {
const testing = std.testing;
var mat = try Matrix(u64, 10, 10).init(testing.allocator);
defer mat.deinit(testing.allocator);
mat.set(1, 1, 100);
try testing.expectEqual(100, mat.get(1, 1));
}
pub fn RowIterator(comptime T: type, comptime width: usize) type {
return struct {
_index: usize,
/// data is a reference to the board game data, now we will return it as
/// each row
_data: []T,
fn init(data: []T) @This() {
return .{
._index = 0,
._data = data,
};
}
/// next returns the next row until we have exhausted the slice
pub fn next(self: *@This()) ?[]T {
if (self._index >= self._data.len) {
return null;
}
// increment a line at a time
defer self._index += width;
return self._data[self._index .. self._index + width];
}
};
}
test "TestRowIterator" {
const testing = std.testing;
var chunk = try Matrix(usize, 10, 10).init(testing.allocator);
defer chunk.deinit(testing.allocator);
for (0..10) |x| {
chunk.set(x, x, x);
}
var iter = chunk.row_iterator();
var x: usize = 0;
while (iter.next()) |row| {
const v: @Vector(10, usize) = row[0..10].*;
try testing.expectEqual(x, @reduce(.Add, v));
x += 1;
}
}
+45
View File
@@ -0,0 +1,45 @@
const std = @import("std");
const Io = std.Io;
const grome = @import("grome");
pub fn main(init: std.process.Init) !void {
// This is appropriate for anything that lives as long as the process.
const arena: std.mem.Allocator = init.arena.allocator();
// Initialize the stdout writer
var stdout_buffer: [1024]u8 = undefined;
var stdout_file_writer: Io.File.Writer = .init(.stdout(), init.io, &stdout_buffer);
const stdout_writer = &stdout_file_writer.interface;
// alright lets play with the matrix
const width = 50;
const height = 50;
var matrix = try grome.Matrix(u8, width, height).init(arena, ' ');
var iter = matrix.row_iterator();
const density = (width * height) / 100;
for (0..density) |_| {
var buf: [3]u8 = undefined;
init.io.random(&buf);
matrix.set(@intCast(buf[0] % width), @intCast(buf[1] % height), @intCast((buf[2] % 50) + 93));
}
_ = try stdout_writer.write(" ┌─");
for (0..width) |_| {
_ = try stdout_writer.write("");
}
_ = try stdout_writer.write("─┐ \n");
while (iter.next()) |row| {
_ = try stdout_writer.print(" │ {s} │ \n", .{row});
}
_ = try stdout_writer.write(" └─");
for (0..width) |_| {
_ = try stdout_writer.write("");
}
_ = try stdout_writer.write("─┘ \n");
try stdout_writer.flush();
}
+7
View File
@@ -0,0 +1,7 @@
const std = @import("std");
pub const matrix = @import("grome/matrix.zig");
pub const Matrix = matrix.Matrix;
test {
std.testing.refAllDecls(@This());
}