diff options
| author | emmadoth <emmadoth@disroot.org> | 2023-05-09 02:01:09 +0200 |
|---|---|---|
| committer | emmadoth <emmadoth@disroot.org> | 2023-05-09 02:01:09 +0200 |
| commit | c4d2a0f2c7d3bfafb952ac6f59857dbbbd708a92 (patch) | |
| tree | 47f9e2c51e5d0db2e17eb090d2555df4f323957c /main.c | |
| parent | c1a7f4c5384613efe17a356719112bcb1234e1a4 (diff) | |
use ncurses
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 107 |
1 files changed, 62 insertions, 45 deletions
@@ -1,57 +1,53 @@ #include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ncurses.h> -#define WIDTH 213 -#define HEIGHT 56 +#define F_ARGS _Size* size, _Bool (*board)[size->width], char (*neighbours)[size->width], int y, int x -#define F_ARGS _Bool board[HEIGHT][WIDTH], int neighbours[HEIGHT][WIDTH], int y, int x +typedef struct { + int height; + int width; +} _Size; -int get_neighbours(_Bool board[HEIGHT][WIDTH], int y, int x) -{ - int neighbours = 0; - if(y > 0) +void iter(void (*f)(F_ARGS), _Size *size, _Bool (*board)[size->width], char (*neighbours)[size->width]) +{ + for(int y = 0; y < size->height; ++y) { - if(board[y - 1][x]) ++neighbours; // n - - if((x + 1) < WIDTH && board[y - 1][x + 1]) ++neighbours; // ne - if( x > 0 && board[y - 1][x - 1]) ++neighbours; // nw + for(int x = 0; x < size->width; ++x) + { + (*f)(size, board, neighbours, y, x); + } } +} - if((x + 1) < WIDTH && board[y][x + 1]) ++neighbours; // e - if( x > 0 && board[y][x - 1]) ++neighbours; // w - - if((y + 1) < HEIGHT) +void get_neighbours(F_ARGS) +{ + if(y > 0) { - if(board[y + 1][x]) ++neighbours; // s + if(board[y - 1][x]) neighbours[y][x] += 1; // n - if((x + 1) < WIDTH && board[y + 1][x + 1]) ++neighbours; // se - if( x > 0 && board[y + 1][x - 1]) ++neighbours; // sw + if((x + 1) < size->width && board[y - 1][x + 1]) neighbours[y][x] += 1; // ne + if( x > 0 && board[y - 1][x - 1]) neighbours[y][x] += 1; // nw } - return neighbours; -} + if((x + 1) < size->width && board[y][x + 1]) neighbours[y][x] += 1; // e + if( x > 0 && board[y][x - 1]) neighbours[y][x] += 1; // w -void iter(void (*f)(F_ARGS), _Bool board[HEIGHT][WIDTH], int neighbours[HEIGHT][WIDTH]) -{ - for(int y = 0; y < HEIGHT; ++y) + if((y + 1) < size->height) { - for(int x = 0; x < WIDTH; ++x) - { - (*f)(board, neighbours, y, x); - } + if(board[y + 1][x]) neighbours[y][x] += 1; // s + + if((x + 1) < size->height && board[y + 1][x + 1]) neighbours[y][x] += 1; // se + if( x > 0 && board[y + 1][x - 1]) neighbours[y][x] += 1; // sw } } void print_board_pos(F_ARGS) { - board[y][x] ? putc('*', stdout) : putc('.', stdout); - if(x + 1 == WIDTH) - putc('\n', stdout); -} - -void calculate_neighbours(F_ARGS) -{ - neighbours[y][x] = get_neighbours(board, y, x); + if(board[y][x]) + mvaddch(y, x, '*'); } void apply_rules(F_ARGS) @@ -72,6 +68,8 @@ int handle_input(int input) { case 'q': return 0; + case KEY_RESIZE: + return 0; default: return 1; } @@ -79,8 +77,18 @@ int handle_input(int input) int main(void) { - _Bool board[HEIGHT][WIDTH] = { 0 }; - int neighbours[HEIGHT][WIDTH] = { 0 }; + initscr(); + cbreak(); + noecho(); + + _Size size; + getmaxyx(stdscr, size.height, size.width); + + _Bool (*board )[size.width] = malloc(size.height * size.width * sizeof(_Bool)); + char (*neighbours)[size.width] = malloc(size.height * size.width * sizeof(char )); + + memset(board , 0, size.height * size.width * sizeof(_Bool)); + memset(neighbours, 0, size.height * size.width * sizeof(char )); board[0][1] = 1; board[1][2] = 1; @@ -88,22 +96,31 @@ int main(void) board[2][1] = 1; board[2][2] = 1; - iter(&print_board_pos, board, neighbours); - - int input = getchar(); + iter(&print_board_pos, &size, board, neighbours); + + int input = getch(); _Bool run = handle_input(input); while(run) { - iter(&calculate_neighbours, board, neighbours); + iter(&get_neighbours, &size, board, neighbours); - iter(&apply_rules, board, neighbours); + iter(&apply_rules, &size, board, neighbours); - iter(&print_board_pos, board, neighbours); + clear(); + iter(&print_board_pos, &size, board, neighbours); + refresh(); - input = getchar(); + input = getch(); run = handle_input(input); + + memset(neighbours, 0, size.height * size.width * sizeof(char )); } - return 0; + free(board); + free(neighbours); + + endwin(); + + return run; } |
