about summary refs log tree commit diff stats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/main.c b/main.c
index 8eafc11..5b00d0c 100644
--- a/main.c
+++ b/main.c
@@ -121,10 +121,9 @@ setup()
 	seltag[0] = True;
 
 	/* style */
-	dc.bg[0] = getcolor(NORMBGCOLOR);
-	dc.fg[0] = getcolor(NORMFGCOLOR);
-	dc.bg[1] = getcolor(SELBGCOLOR);
-	dc.fg[1] = getcolor(SELFGCOLOR);
+	dc.bg = getcolor(BGCOLOR);
+	dc.fg = getcolor(FGCOLOR);
+	dc.border = getcolor(BORDERCOLOR);
 	setfont(FONT);
 
 	sx = sy = 0;
href='#n42'>42

                              
                                                                                                


























                                                                                            




                                                                                         






                                                              
export type Grid = number[][];

// FIXME: I'm exporting way more than is necessary to support testing. What if I didn't do that?

export function countNeighbors(grid: Grid, x: number, y: number): number {
    const neighborOffsets = [
        [-1, -1], [-1, 0], [-1, 1],
        [0, -1],           [0, 1],
        [1, -1],  [1, 0],  [1, 1]
    ];

    return neighborOffsets.reduce((count, [dx, dy]) => {
        const nx = x + dx;
        const ny = y + dy;
        if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) {
            return count + 1;
        }
        return count;
    }, 0);
}

export function step(grid: Grid): Grid {
    return grid.map((row, x) =>
        row.map((cell, y) => {
            const neighbors = countNeighbors(grid, x, y);
            return neighbors === 3 || (neighbors === 2 && cell) ? 1 : 0;
        })
    );
}

export function printGrid(grid: Grid): void {
    grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').join('')));
    console.log('\n');
}

export function simulate(initial: Grid, steps: number): void {
    let grid = initial;
    Array.from({ length: steps }).forEach(() => {
        printGrid(grid);
        grid = step(grid);
    });
}