Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve expanding chars #8

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ const Column = struct {
active: bool = false,
chars: std.ArrayList(?Char),

fn init(allocator: std.mem.Allocator) Column {
fn init(allocator: std.mem.Allocator, size: usize) Column {
return .{
.chars = std.ArrayList(?Char).init(allocator),
.chars = std.ArrayList(?Char).initCapacity(allocator, size) catch undefined,
};
}

Expand Down Expand Up @@ -300,10 +300,9 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {
},
.rain => {
// init columns
if (core.columns.?.items.len != core.width) {
if (core.columns.?.items.len == 0) {
for (0..@intCast(core.width)) |_| {
var column = Column.init(core.allocator);
try column.addNull();
const column = Column.init(core.allocator, @intCast(core.height));
try core.columns.?.append(column);
}

Expand All @@ -314,19 +313,17 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {
}
}

if (core.active_columns > 2) {
if (core.active_columns >= 2) {
core.columns.?.items[rand.uintLessThan(u32, @intCast(core.width))].?.deactivate(core);
} else {
core.columns.?.items[rand.uintLessThan(u32, @intCast(core.width))].?.activate(core);
}

for (0..@intCast(core.width)) |w| {
if (core.columns.?.items[w].?.active) {
if (rand.boolean()) continue;
if (core.columns.?.items[w].?.chars.items.len == core.height) {
_ = core.columns.?.items[w].?.chars.pop();
}
if (rand.boolean()) continue;
_ = core.columns.?.items[w].?.chars.pop();

if (core.columns.?.items[w].?.active) {
if (rand.uintLessThan(u3, 7) < 3) {
try core.columns.?.items[w].?.addNull();
continue;
Expand Down Expand Up @@ -543,7 +540,7 @@ test "column" {
var core = Core{ .allocator = std.testing.allocator };
try core.start(Style.default);

const column = Column.init(core.allocator);
const column = Column.init(core.allocator, @intCast(core.height));
try core.columns.?.append(column);
try core.columns.?.items[0].?.addChar(&core, Mode.decimal, rand);
try core.columns.?.items[0].?.addChar(&core, Mode.decimal, rand);
Expand Down