init
This commit is contained in:
commit
00c8591d17
5 changed files with 294 additions and 0 deletions
30
src/main.zig
Normal file
30
src/main.zig
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const sand = @import("sand");
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
var grid: sand.Grid = try .init(4, 4, init.gpa);
|
||||
var gridCopy: sand.Grid = try .init(4, 4, init.gpa);
|
||||
defer grid.deinit(init.gpa);
|
||||
defer gridCopy.deinit(init.gpa);
|
||||
|
||||
grid.arr[2][0].alive = true;
|
||||
grid.arr[2][1].alive = true;
|
||||
grid.arr[2][2].alive = true;
|
||||
grid.arr[2][3].alive = true;
|
||||
|
||||
grid.arr[0][1].alive = true;
|
||||
grid.arr[1][1].alive = true;
|
||||
grid.arr[1][2].alive = true;
|
||||
|
||||
std.debug.print("{f}\n", .{grid});
|
||||
try grid.copy(&gridCopy);
|
||||
grid.updateAll();
|
||||
|
||||
while (!grid.eql(gridCopy)) {
|
||||
std.debug.print("{f}\n", .{grid});
|
||||
try grid.copy(&gridCopy);
|
||||
grid.updateAll();
|
||||
}
|
||||
}
|
||||
129
src/root.zig
Normal file
129
src/root.zig
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
//! The logic of the sand
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
pub const state = enum(u1) { Alive, Dead };
|
||||
pub const leaning = enum(u1) {
|
||||
Left = 0,
|
||||
Right = 1,
|
||||
pub fn opposite(self: leaning) leaning {
|
||||
return @enumFromInt(@intFromBool(self == .Left));
|
||||
}
|
||||
};
|
||||
pub const cell = struct {
|
||||
alive: bool,
|
||||
};
|
||||
|
||||
pub const position = packed struct {
|
||||
x: u16,
|
||||
y: u16,
|
||||
};
|
||||
|
||||
pub const Grid = struct {
|
||||
arr: [][]cell,
|
||||
max: position,
|
||||
leaning: leaning,
|
||||
pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
|
||||
for (self.arr) |line| {
|
||||
for (line) |grain| {
|
||||
try writer.writeAll(if (grain.alive) "" else "");
|
||||
}
|
||||
try writer.writeByte('\n');
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(x: u16, y: u16, allocator: std.mem.Allocator) std.mem.Allocator.Error!Grid {
|
||||
const arr: [][]cell = try allocator.alloc([]cell, y);
|
||||
for (arr) |*line| {
|
||||
line.* = try allocator.alloc(cell, x);
|
||||
for (line.*) |*c| {
|
||||
c.* = .{ .alive = false };
|
||||
}
|
||||
}
|
||||
|
||||
return .{
|
||||
.max = position{
|
||||
.x = x - 1,
|
||||
.y = y - 1,
|
||||
},
|
||||
.arr = arr,
|
||||
.leaning = .Left,
|
||||
};
|
||||
}
|
||||
pub fn deinit(self: Grid, allocator: std.mem.Allocator) void {
|
||||
for (self.arr) |line| {
|
||||
allocator.free(line);
|
||||
}
|
||||
allocator.free(self.arr);
|
||||
}
|
||||
|
||||
pub fn updateAll(self: *Grid) void {
|
||||
for (0..self.arr.len) |y| {
|
||||
for (0..self.arr[y].len) |x| {
|
||||
update(.{ .y = @truncate(y), .x = @truncate(x) }, self.*);
|
||||
}
|
||||
}
|
||||
self.leaning = self.leaning.opposite();
|
||||
}
|
||||
|
||||
pub const copyError = error{wrongDimensions};
|
||||
pub fn copy(self: Grid, dest: *Grid) copyError!void {
|
||||
if (self.arr.len != dest.arr.len or self.arr[0].len != dest.arr[0].len) return copyError.wrongDimensions;
|
||||
for (dest.arr, self.arr) |d, s| {
|
||||
@memcpy(d, s);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eql(self: Grid, dest: Grid) bool {
|
||||
if (!(self.max == dest.max)) return false;
|
||||
|
||||
for (self.arr, dest.arr) |ls, ld| {
|
||||
for (ls, ld) |cs, cd| {
|
||||
if (cs.alive != cd.alive) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn update(self: position, grid: Grid) void {
|
||||
if (self.y == grid.max.y) return;
|
||||
if (!grid.arr[self.y][self.x].alive) return;
|
||||
const nextline = grid.arr[self.y + 1];
|
||||
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x].alive, false, true, .seq_cst, .monotonic)) |_| {
|
||||
if (self.x == grid.max.x) {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x - 1].alive, false, true, .seq_cst, .monotonic)) |_| {} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
} else if (self.x == 0) {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x + 1].alive, false, true, .seq_cst, .monotonic)) |_| {} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
} else {
|
||||
switch (grid.leaning) {
|
||||
.Right => {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x + 1].alive, false, true, .seq_cst, .monotonic)) |_| {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x - 1].alive, false, true, .seq_cst, .monotonic)) |_| {} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
},
|
||||
.Left => {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x - 1].alive, false, true, .seq_cst, .monotonic)) |_| {
|
||||
if (@cmpxchgStrong(bool, &nextline[self.x + 1].alive, false, true, .seq_cst, .monotonic)) |_| {} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@atomicStore(bool, &grid.arr[self.y][self.x].alive, false, .seq_cst);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue