Initial commit
This commit is contained in:
commit
f24e990a4c
20
LICENSE
Normal file
20
LICENSE
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
MIT License
|
||||||
|
Copyright 2019 Bruce Hill <bruce@bruce-hill.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
40
Makefile
Normal file
40
Makefile
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
PREFIX=
|
||||||
|
CC=cc
|
||||||
|
CFLAGS=-O3 -std=gnu99
|
||||||
|
LIBS=-ltermbox
|
||||||
|
NAME=conway
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm $(NAME)
|
||||||
|
|
||||||
|
run: $(NAME)
|
||||||
|
./$(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(NAME).c
|
||||||
|
$(CC) $(NAME).c $(LIBS) $(CFLAGS) -o $(NAME)
|
||||||
|
|
||||||
|
install: $(NAME)
|
||||||
|
@prefix="$(PREFIX)"; \
|
||||||
|
if [[ ! $$prefix ]]; then \
|
||||||
|
read -p $$'\033[1mWhere do you want to install? (default: /usr/local) \033[0m' prefix; \
|
||||||
|
fi; \
|
||||||
|
if [[ ! $$prefix ]]; then \
|
||||||
|
prefix="/usr/local"; \
|
||||||
|
fi; \
|
||||||
|
mkdir -pv $$prefix/bin $$prefix/share/man/man1 \
|
||||||
|
&& cp -v $(NAME) $$prefix/bin/ \
|
||||||
|
&& cp -v doc/$(NAME).1 $$prefix/share/man/man1/
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
@prefix="$(PREFIX)"; \
|
||||||
|
if [[ ! $$prefix ]]; then \
|
||||||
|
read -p $$'\033[1mWhere do you want to uninstall from? (default: /usr/local) \033[0m' prefix; \
|
||||||
|
fi; \
|
||||||
|
if [[ ! $$prefix ]]; then \
|
||||||
|
prefix="/usr/local"; \
|
||||||
|
fi; \
|
||||||
|
echo "Deleting..."; \
|
||||||
|
rm -rvf $$prefix/bin/$(NAME) $$prefix/share/man/man1/$(NAME).1
|
||||||
|
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Conway
|
||||||
|
A termbox program for running Conway's Game of Life.
|
||||||
|
Different rulesets can be used with `conway -S23 -B3` syntax.
|
121
conway.c
Normal file
121
conway.c
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
// A console conway's game of life program by Bruce Hill
|
||||||
|
// Released under the MIT license, see LICENSE for details.
|
||||||
|
// This file contains the main code for the table viewer.
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <termbox.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifndef bool
|
||||||
|
typedef char bool;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static bool survive[9] = {0,0,1,1,0,0,0,0,0};
|
||||||
|
static bool birth[9] = {0,0,0,1,0,0,0,0,0};
|
||||||
|
|
||||||
|
static void update(const bool *cells, bool *future_cells)
|
||||||
|
{
|
||||||
|
int W = tb_width()/2, H = tb_height();
|
||||||
|
for (int y = 0; y < H; y++) {
|
||||||
|
for (int x = 0; x < W; x++) {
|
||||||
|
int neighbors = (
|
||||||
|
cells[(y-1 % H)*W + (x-1 % W)] +
|
||||||
|
cells[(y-1 % H)*W + (x % W)] +
|
||||||
|
cells[(y-1 % H)*W + (x+1 % W)] +
|
||||||
|
cells[y*W + (x-1 % W)] +
|
||||||
|
cells[y*W + (x+1 % W)] +
|
||||||
|
cells[(y+1 % H)*W + (x-1 % W)] +
|
||||||
|
cells[(y+1 % H)*W + (x % W)] +
|
||||||
|
cells[(y+1 % H)*W + (x+1 % W)]
|
||||||
|
);
|
||||||
|
future_cells[y*W + x] = cells[y*W + x] ? survive[neighbors] : birth[neighbors];
|
||||||
|
if (future_cells[y*W + x] != cells[y*W + x]) {
|
||||||
|
tb_change_cell(2*x, y, ' ', 0, future_cells[y*W + x] ? 8 : 0);
|
||||||
|
tb_change_cell(2*x+1, y, ' ', 0, future_cells[y*W + x] ? 8 : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (strcmp(argv[i], "--help") == 0) {
|
||||||
|
printf("Conway's Game of Life Viewer\nUsage: conway [-S012345678] [-B012345678]\n");
|
||||||
|
return 0;
|
||||||
|
} else if (strncmp(argv[i], "-B", 2) == 0) {
|
||||||
|
memset(birth, 0, sizeof(birth));
|
||||||
|
for (char *p = &argv[i][2]; *p; p++) {
|
||||||
|
if ('0' <= *p && *p <= '8')
|
||||||
|
birth[*p - '0'] = 1;
|
||||||
|
}
|
||||||
|
} else if (strncmp(argv[i], "-S", 2) == 0) {
|
||||||
|
memset(survive, 0, sizeof(survive));
|
||||||
|
for (char *p = &argv[i][2]; *p; p++) {
|
||||||
|
if ('0' <= *p && *p <= '8')
|
||||||
|
survive[*p - '0'] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = tb_init();
|
||||||
|
if (ret) {
|
||||||
|
fprintf(stderr, "tb_init() failed with error code %d\n", ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
tb_select_input_mode(TB_INPUT_MOUSE);
|
||||||
|
|
||||||
|
int flipflop = 0;
|
||||||
|
int W = tb_width()/2, H = tb_height();
|
||||||
|
bool *buffers[2];
|
||||||
|
resize:
|
||||||
|
buffers[0] = calloc(W*H, sizeof(bool));
|
||||||
|
buffers[1] = calloc(W*H, sizeof(bool));
|
||||||
|
for (int x = 0; x < W; x++) {
|
||||||
|
for (int y = 0; y < H; y++) {
|
||||||
|
buffers[flipflop][y*W+x] = random() % 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tb_clear();
|
||||||
|
while (1) {
|
||||||
|
update(buffers[flipflop], buffers[!flipflop]);
|
||||||
|
flipflop = !flipflop;
|
||||||
|
tb_present();
|
||||||
|
usleep(60000);
|
||||||
|
struct tb_event ev;
|
||||||
|
while (tb_peek_event(&ev, 0)) {
|
||||||
|
switch (ev.type) {
|
||||||
|
case TB_EVENT_RESIZE:
|
||||||
|
W = tb_width()/2, H = tb_height();
|
||||||
|
goto resize;
|
||||||
|
|
||||||
|
case TB_EVENT_KEY:
|
||||||
|
switch (ev.key) {
|
||||||
|
case TB_KEY_ESC:
|
||||||
|
if (tb_peek_event(&ev, 50) == 0)
|
||||||
|
goto done;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
switch (ev.ch) {
|
||||||
|
case 'q':
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TB_EVENT_MOUSE:
|
||||||
|
buffers[flipflop][ev.y*W + ev.x/2] = !buffers[flipflop][ev.y*W + ev.x/2];
|
||||||
|
tb_change_cell(2*(ev.x/2), ev.y, ' ', 0, buffers[flipflop][ev.y*W + ev.x/2] ? 8 : 0);
|
||||||
|
tb_change_cell(2*(ev.x/2)+1, ev.y, ' ', 0, buffers[flipflop][ev.y*W + ev.x/2] ? 8 : 0);
|
||||||
|
tb_present();
|
||||||
|
usleep(10000);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done:
|
||||||
|
tb_shutdown();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user