summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTanner Robison <[email protected]>2026-06-28 17:30:30 -0700
committerTanner Robison <[email protected]>2026-07-08 16:22:17 -0700
commitd3440b5a4b8a4c65e4c3a8c836f10586ce493851 (patch)
tree8c46df00cd7bb0725350086b9594060213c70910
parent85e57d82555e435365c58b25523104a4547fc1d7 (diff)
Functional ASCII grid initialization
-rw-r--r--README.md2
-rwxr-xr-xabellian_sandpilebin16176 -> 16544 bytes
-rw-r--r--abellian_sandpile.c92
3 files changed, 75 insertions, 19 deletions
diff --git a/README.md b/README.md
index 5ef0ae0..31fdbd4 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1 @@
# Abelian Sandpile model
-## compile command:
-gcc -Wall abellian_sandpile.c -o abellian_sandpile $(pkg-config --cflags --libs sdl2) -lm
diff --git a/abellian_sandpile b/abellian_sandpile
index 95132c3..127bf1b 100755
--- a/abellian_sandpile
+++ b/abellian_sandpile
Binary files differ
diff --git a/abellian_sandpile.c b/abellian_sandpile.c
index ec6966b..9d2e7b4 100644
--- a/abellian_sandpile.c
+++ b/abellian_sandpile.c
@@ -1,7 +1,9 @@
#include <asm-generic/ioctls.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/ioctl.h>
+#include <unistd.h>
#define CLEAR_GRID "\033[0m"
#define COLOR_RED "\033[31m" // cell value 4
@@ -9,36 +11,92 @@
#define COLOR_YELLOW "\033[31m" // cell value 2
#define COLOR_WHITE "\033[31m" // cell value 1
-#define WIDTH 40
-#define HEIGHT 40
-
-int init_grid(int positions[HEIGHT][WIDTH]);
-int draw_new_positions();
-int render();
+int init_grid(int height, int width, int (*positions)[width]);
+int get_new_positions(int height, int width, int (*positions)[width],
+ int (*new_positions)[width]);
+int render(int height, int width, int (*positions)[width],
+ int (*new_positions)[width]);
int main() {
+ int startup_status = 0;
// gets terminal window size
struct winsize w;
- // ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
+ fprintf(stderr, "Failed to get terminal window size");
+ return 1;
+ }
- int positions[HEIGHT][WIDTH];
- int (*ppositions)[] = positions;
- int new_positions[HEIGHT][WIDTH];
+ int terminal_height = w.ws_row;
+ int terminal_width = w.ws_col;
+
+ int (*positions)[terminal_width] =
+ malloc(terminal_height * terminal_width * sizeof(int));
+ if (positions == NULL) {
+ fprintf(stderr, "Failed to allocate for positions");
+ return 1;
+ }
+ // fill array with zeros
+ memset(positions, 0, terminal_height * terminal_width * sizeof(int));
+
+ int (*new_positions)[terminal_width] =
+ malloc(terminal_height * terminal_width * sizeof(int));
+ if (new_positions == NULL) {
+ fprintf(stderr, "Failed to allocate for new_positions");
+ free(positions);
+ return 1;
+ }
+ // fill array with zeros
+ memset(new_positions, 0, terminal_height * terminal_width * sizeof(int));
+
+ printf("The terminal size is: %d rows, %d columns\n", terminal_height,
+ terminal_width);
printf("starting abellian sandpile simulation\n");
- init_grid(ppositions);
- printf("\ninitialized grid");
+
+ startup_status = init_grid(terminal_height, terminal_width, positions);
+ if (startup_status) {
+ fprintf(stderr, "Failed to initialize grid");
+ }
+ get_new_positions(terminal_width, terminal_width, positions, new_positions);
+
+ free(positions);
+ free(new_positions);
return 0;
}
-int init_grid(int positions[HEIGHT][WIDTH]) {
- for (int i = 0; i < HEIGHT; i++) {
- for (int j = 0; j < WIDTH; j++) {
- positions[i][j] = 0;
- printf("%d", positions);
+int init_grid(int height, int width, int (*positions)[width]) {
+ printf("\033[H\033[J Starting abellian sandpile model.....\n");
+ for (int i = 0; i < height; i++) {
+ for (int j = 0; j < width; j++) {
+ printf(" ");
}
printf("\n");
}
+ return 0;
+}
+
+int get_new_positions(int height, int width, int (*positions)[width],
+ int (*new_positions)[width]) {
+ new_positions = positions;
+
+ // getting new positions
+ for (int i = 0; i < height; i++) {
+ for (int j = 0; j < width; i++) {
+ if (positions[i][j] >= 4) {
+ // add some logic here
+ new_positions[i][j] = 0;
+ new_positions[i][j] = positions[i + 1][j] + 1;
+ new_positions[i][j] = positions[i - 1][j] + 1;
+ new_positions[i][j] = positions[i][j + 1] + 1;
+ new_positions[i][j] = positions[i][j - 1] + 1;
+ }
+ }
+ }
+ return 0;
+}
+int render(int height, int width, int (*positions)[width],
+ int (*new_positions)[width]) {
return 0;
}