snek

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 0a8a200c33b471c62dfa2039fd27291b98314d21
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 27 Jul 2021 23:17:50 +0100

initial: snek head moves

Diffstat:
A.gitignore | 1+
AMakefile | 9+++++++++
Amain.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 125 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +snek diff --git a/Makefile b/Makefile @@ -0,0 +1,9 @@ +DEBUG=-ggdb3 -Og +CFLAGS+=-Wall -Wextra -Wpedantic + +snek: main.c + $(CC) $(CFLAGS) $(DEBUG) -o snek main.c + +#%.c : +# $(CC) $(CFLAGS) $(DEBUG) $@ + diff --git a/main.c b/main.c @@ -0,0 +1,115 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <time.h> +#include <unistd.h> + +//#define XCOLS 100 +//#define YROWS 100 +#define GRID_EMPTY 0 +#define GRID_SNAKE 1 +#define GRID_TRGET 2 + +#define MAX_GENS 100 + +char v = 0; + +struct Snake { + /* position of head */ + int head[2]; + /* velocity of head */ + int v[2]; + /* body length */ + size_t len; +}; + +void +print_grid(int x, int y, char grid[x][y]){ + for (int j = 0; j < y; j++){ + for (int i = 0; i < x; i++) + printf("%c", grid[i][j] == GRID_SNAKE ? '#' : grid[i][j] == GRID_TRGET ? '@' : ' '); + printf("\n"); + } +} + +void +new_target(int x, int y, char grid[x][y]){ + int randx, randy; +freshvalues: + randx = rand() % x; + randy = rand() % y; + if (!grid[randx][randy]) + grid[randx][randy] = GRID_TRGET; + else goto freshvalues; +} + + +void +plog(const char *msg, ...){ + if (v){ + va_list l; + va_start(l, msg); + vfprintf(stderr, msg, l); + va_end(l); + } +} + +void +die(const int err, const char *msg){ + fprintf(stderr, msg); + exit(err); +} + +int +main(int argc, char **argv){ + if (argc == 2 && !strcmp("-v", argv[1])) v = 1; + plog("Verbose logging enabled\n"); + + /* find terminal size */ + struct winsize w; + if (ioctl(1, TIOCGWINSZ, &w) < 0){ + perror("ioctl"); + die(1, NULL); + } + + int XCOLS = (int) w.ws_col; + int YROWS = (int) w.ws_row; + + plog("found terminal size %dx%d\n", XCOLS, YROWS); + + char grid[XCOLS][YROWS]; + struct Snake snek; + snek.v[0] = 1; + snek.v[1] = 0; + + /* initialise rand with a fresh seed */ + srand((unsigned) time(NULL)); + + /* initialise grid to be all 0's to begin with. */ + for (int i = 0; i < XCOLS; i++) + for (int j = 0; j < YROWS; j++) + grid[i][j] = GRID_EMPTY; + + /* choose a random starting point */ + snek.head[0] = rand() % XCOLS; + snek.head[1] = rand() % YROWS; + grid[snek.head[0]][snek.head[1]] = GRID_SNAKE; + + /* and a random point for the target */ + new_target(XCOLS, YROWS, grid); + + int gen = MAX_GENS; + while (gen--){ + print_grid(XCOLS, YROWS, grid); + plog("snek head is %d,%d\nmoving at [%d,%d]\n", snek.head[0], snek.head[1], snek.v[0], snek.v[1]); + grid[snek.head[0]][snek.head[1]] = GRID_EMPTY; + snek.head[0] += snek.v[0]; + snek.head[1] += snek.v[1]; + grid[snek.head[0]][snek.head[1]] = GRID_SNAKE; + printf("hi\r"); + sleep(1); + } + +}