random

:-)
git clone read: git://git.margiolis.net/random.git
Log | Files | Refs | LICENSE

commit 76b1065dd8f2e285c98c18282f1637abf2fd929c
parent b30ce3a35ef5750eff362b3ca477f73cc8c6e276
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu, 24 Mar 2022 02:20:41 +0200

foo

Diffstat:
Abf/Makefile | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Abf/README | 17+++++++++++++++++
Abf/bf.1 | 25+++++++++++++++++++++++++
Abf/bf.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abf/config.mk | 22++++++++++++++++++++++
Rbrainfuck/tests/hello.bf -> bf/tests/hello.bf | 0
Rbrainfuck/tests/triangle.bf -> bf/tests/triangle.bf | 0
Abounce.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dbrainfuck/Makefile | 53-----------------------------------------------------
Dbrainfuck/README | 16----------------
Dbrainfuck/brainfuck.1 | 25-------------------------
Dbrainfuck/brainfuck.c | 76----------------------------------------------------------------------------
Dbrainfuck/config.mk | 20--------------------
Mbytepusher/bytepusher.c | 12++++++------
Mbytepusher/config.mk | 2++
Mfnc/Makefile | 2++
Amandelbrot.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mminecurses/config.mk | 2++
Mminecurses/minecurses.c | 8++++----
Mmorse/morse.cpp | 4++--
Annc.cpp | 169+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dother/bounce.c | 55-------------------------------------------------------
Dother/mandelbrot.c | 51---------------------------------------------------
Dother/nnc.cpp | 169-------------------------------------------------------------------------------
Dother/snake.cpp | 187-------------------------------------------------------------------------------
Mshitcoin/README | 7++++---
Mshitcoin/config.mk | 2++
Mshitcoin/shitcoin.c | 18+++++++++---------
Asnake.cpp | 187+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 files changed, 688 insertions(+), 676 deletions(-)

diff --git a/bf/Makefile b/bf/Makefile @@ -0,0 +1,53 @@ +# See LICENSE file for copyright and license details. +# bf - bf interpreter +.POSIX: + +include config.mk + +BIN = bf +DIST = ${BIN}-${VERSION} +MAN1 = ${BIN}.1 + +SRC = bf.c +OBJ = ${SRC:.c=.o} + +all: options ${BIN} + +options: + @echo ${BIN} build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +${BIN}: ${OBJ} + ${CC} ${LDFLAGS} ${OBJ} -o $@ + +.c.o: + ${CC} -c ${CFLAGS} $< + +dist: clean + mkdir -p ${DIST} + cp -R tests bf.1 bf.c config.mk Makefile README ${DIST} + tar -cf ${DIST}.tar ${DIST} + gzip ${DIST}.tar + rm -rf ${DIST} + +run: + ./${BIN} + +install: all + mkdir -p ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANPREFIX}/man1 + cp -f ${BIN} ${DESTDIR}${PREFIX}/bin + cp -f ${MAN1} ${DESTDIR}${MANPREFIX}/man1 + sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MANPREFIX}/man1/${MAN1} + chmod 755 ${DESTDIR}${PREFIX}/bin/${BIN} + chmod 644 ${DESTDIR}${MANPREFIX}/man1/${MAN1} + +uninstall: + rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \ + ${DESTDIR}${MANPREFIX}/man1/${MAN1} + +clean: + rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core + +.PHONY: all options clean dist install uninstall run diff --git a/bf/README b/bf/README @@ -0,0 +1,17 @@ +bf +== + +A brainfuck interpreter. bf reads input from stdin and interprets it +as Brainfuck source code. + +Usage +----- + cd path/to/bf + make + ./bf < src.bf + +You can install bf by running: + + sudo make install clean + +The binary will be installed in usr/local/bin. diff --git a/bf/bf.1 b/bf/bf.1 @@ -0,0 +1,25 @@ +.Dd bf\-VERSION +.Dt BRAINFUCK 1 +.Os +.Sh NAME +.Nm bf +.Nd a simple brainfuck interpreter +.Sh SYNOPSIS +.Nm +stdin +.Sh DESCRIPTION +.Nm +reads input from stdin and interprets it +as Brainfuck source code. +.Sh EXAMPLES +To parse a Brainfuck source file +.Pp +.Dl $ bf < src.bf +.Pp +Alternatively, you can pipe to +.Nm +.Pp +.Dl $ cat src.bf | bf +.Pp +.Sh AUTHORS +.An Christos Margiolis Aq Mt christos@margiolis.net diff --git a/bf/bf.c b/bf/bf.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define BUFSIZE 50000 + +int +main(int argc, char *argv[]) +{ + size_t len = 0; + int closed, opened, pos = 0; + unsigned short *pc; + char buf[BUFSIZE], *src; + + while (read(STDIN_FILENO, &buf[len], 1) > 0) + len++; + buf[len] = '\0'; + + if ((src = malloc(len)) == NULL) { + perror("malloc"); + exit(1); + } + strcpy(src, buf); + memset(buf, 0, len); + + for (pc = (unsigned short *)buf; pos < len; pos++) { + switch (src[pos]) { + case '>': + pc++; + break; + case '<': + pc--; + break; + case '+': + (*pc)++; + break; + case '-': + (*pc)--; + break; + case '.': + putchar(*pc); + break; + case ',': + *pc = getchar(); + break; + case '[': + if (!(*pc)) { + for (opened = 0, pos++; pos < len; pos++) { + if (src[pos] == ']' && !opened) + break; + else if (src[pos] == '[') + opened++; + else if (src[pos] == ']') + opened--; + } + } + break; + case ']': + if (*pc) { + for (closed = 0, pos--; pos >= 0; pos--) { + if (src[pos] == '[' && !closed) + break; + else if (src[pos] == ']') + closed++; + else if (src[pos] == '[') + closed--; + } + } + break; + } + } + free(src); + + return (0); +} diff --git a/bf/config.mk b/bf/config.mk @@ -0,0 +1,22 @@ +# See LICENSE file for copyright and license details. +# bf version +VERSION = 0.1 + +# paths +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man +# OpenBSD +#MANPREFIX = ${PREFIX}/man + +# includes and libs +INCS = -Iinclude +LIBS = -Llib + +# flags +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \ + -D_XOPEN_SOURCE=700 -DVERSION=\"${VERSION}\" +CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} +LDFLAGS = ${LIBS} + +# compiler +CC = cc diff --git a/brainfuck/tests/hello.bf b/bf/tests/hello.bf diff --git a/brainfuck/tests/triangle.bf b/bf/tests/triangle.bf diff --git a/bounce.c b/bounce.c @@ -0,0 +1,55 @@ +#include <curses.h> +#include <stdio.h> +#include <unistd.h> + +struct ball { + int x; + int y; + int vx; + int vy; + int xmax; + int ymax; +}; + +static void +cursesinit(void) +{ + if (!initscr()) + exit(1); + cbreak(); + noecho(); + curs_set(0); +} + +static void +collision(struct ball *b) +{ + if (b->y < 2 || b->y > b->ymax-1) + b->vy *= -1.0f; + if (b->x < 1 || b->x > b->xmax-1) + b->vx *= -1.0f; + b->y += b->vy; + b->x += b->vx; +} + +int +main(int argc, char *argv[]) +{ + struct ball b = {1, 2, 1, 1, 0, 0}; + + cursesinit(); + getmaxyx(stdscr, b.ymax, b.xmax); + + for (;;) { + erase(); + collision(&b); + mvaddch(b.y, b.x, 'O'); + mvprintw(0, 0, "(x, y) = (%d, %d)", b.x, b.y); + mvhline(1, 0, ACS_HLINE, b.xmax); + refresh(); + usleep(15000); + } + endwin(); + + return (0); +} diff --git a/brainfuck/Makefile b/brainfuck/Makefile @@ -1,53 +0,0 @@ -# See LICENSE file for copyright and license details. -# brainfuck - brainfuck interpreter -.POSIX: - -include config.mk - -BIN = brainfuck -DIST = ${BIN}-${VERSION} -MAN1 = ${BIN}.1 - -SRC = brainfuck.c -OBJ = ${SRC:.c=.o} - -all: options ${BIN} - -options: - @echo ${BIN} build options: - @echo "CFLAGS = ${CFLAGS}" - @echo "LDFLAGS = ${LDFLAGS}" - @echo "CC = ${CC}" - -${BIN}: ${OBJ} - ${CC} ${LDFLAGS} ${OBJ} -o $@ - -.c.o: - ${CC} -c ${CFLAGS} $< - -dist: clean - mkdir -p ${DIST} - cp -R tests brainfuck.1 brainfuck.c config.mk Makefile README ${DIST} - tar -cf ${DIST}.tar ${DIST} - gzip ${DIST}.tar - rm -rf ${DIST} - -run: - ./${BIN} - -install: all - mkdir -p ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANPREFIX}/man1 - cp -f ${BIN} ${DESTDIR}${PREFIX}/bin - cp -f ${MAN1} ${DESTDIR}${MANPREFIX}/man1 - sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MANPREFIX}/man1/${MAN1} - chmod 755 ${DESTDIR}${PREFIX}/bin/${BIN} - chmod 644 ${DESTDIR}${MANPREFIX}/man1/${MAN1} - -uninstall: - rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \ - ${DESTDIR}${MANPREFIX}/man1/${MAN1} - -clean: - rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core - -.PHONY: all options clean dist install uninstall run diff --git a/brainfuck/README b/brainfuck/README @@ -1,16 +0,0 @@ -brainfuck - a brainfuck interpreter -========================================== -brainfuck reads input from stdin and interprets it -as Brainfuck source code. - -Usage ------ - cd path/to/brainfuck - make - ./brainfuck < src.bf - -You can install brainfuck by running: - - sudo make install clean - -The binary will be installed in usr/local/bin. diff --git a/brainfuck/brainfuck.1 b/brainfuck/brainfuck.1 @@ -1,25 +0,0 @@ -.Dd brainfuck\-VERSION -.Dt BRAINFUCK 1 -.Os -.Sh NAME -.Nm brainfuck -.Nd a simple brainfuck interpreter -.Sh SYNOPSIS -.Nm -stdin -.Sh DESCRIPTION -.Nm -reads input from stdin and interprets it -as Brainfuck source code. -.Sh EXAMPLES -To parse a Brainfuck source file -.Pp -.Dl $ brainfuck < src.bf -.Pp -Alternatively, you can pipe to -.Nm -.Pp -.Dl $ cat src.bf | brainfuck -.Pp -.Sh AUTHORS -.An Christos Margiolis Aq Mt christos@margiolis.net diff --git a/brainfuck/brainfuck.c b/brainfuck/brainfuck.c @@ -1,76 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#define BUFSIZE 50000 - -int -main(int argc, char *argv[]) -{ - size_t len = 0; - int closed, opened, pos = 0; - unsigned short *pc; - char buf[BUFSIZE], *src; - - while (read(STDIN_FILENO, &buf[len], 1) > 0) - len++; - buf[len] = '\0'; - - if ((src = malloc(len)) == NULL) { - perror("malloc"); - exit(1); - } - strcpy(src, buf); - memset(buf, 0, len); - - for (pc = (unsigned short *)buf; pos < len; pos++) { - switch (src[pos]) { - case '>': - pc++; - break; - case '<': - pc--; - break; - case '+': - (*pc)++; - break; - case '-': - (*pc)--; - break; - case '.': - putchar(*pc); - break; - case ',': - *pc = getchar(); - break; - case '[': - if (!(*pc)) { - for (opened = 0, pos++; pos < len; pos++) { - if (src[pos] == ']' && !opened) - break; - else if (src[pos] == '[') - opened++; - else if (src[pos] == ']') - opened--; - } - } - break; - case ']': - if (*pc) { - for (closed = 0, pos--; pos >= 0; pos--) { - if (src[pos] == '[' && !closed) - break; - else if (src[pos] == ']') - closed++; - else if (src[pos] == '[') - closed--; - } - } - break; - } - } - free(src); - - return 0; -} diff --git a/brainfuck/config.mk b/brainfuck/config.mk @@ -1,20 +0,0 @@ -# See LICENSE file for copyright and license details. -# brainfuck version -VERSION = 0.1 - -# paths -PREFIX = /usr/local -MANPREFIX = ${PREFIX}/share/man - -# includes and libs -INCS = -Iinclude -LIBS = -Llib - -# flags -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \ - -D_XOPEN_SOURCE=700 -DVERSION=\"${VERSION}\" -CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} -LDFLAGS = ${LIBS} - -# compiler -CC = cc diff --git a/bytepusher/bytepusher.c b/bytepusher/bytepusher.c @@ -33,7 +33,7 @@ evhandle(void) keybits = mem[0] << 8 | mem[1]; while (SDL_PollEvent(&ev)) { if (ev.type == SDL_QUIT || ev.key.keysym.sym == SDLK_ESCAPE) - return 0; + return (0); if (ev.type == SDL_KEYDOWN) for (i = 0; i < 16; i++) if (ev.key.keysym.sym == keys[i]) @@ -42,7 +42,7 @@ evhandle(void) } mem[0] = keybits >> 8; mem[1] = keybits & 0xFF; - return 1; + return (1); } static void @@ -89,11 +89,11 @@ main(int argc, char *argv[]) if (argc != 2) { fprintf(stderr, "usage: %s rom\n", argv[0]); - return 1; + return (1); } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "fopen: %s\n", argv[1]); - return 1; + return (1); } for (i = 0; (mem[i] = fgetc(fp)) != EOF && i < sizeof(mem); i++) ; @@ -108,7 +108,7 @@ main(int argc, char *argv[]) if (!win || !ren || !tex) { fprintf(stderr, "SDL error: %s\n", SDL_GetError()); - return 1; + return (1); } for (r = 0; r < 6; r++) @@ -132,5 +132,5 @@ main(int argc, char *argv[]) SDL_DestroyWindow(win); SDL_Quit(); - return 0; + return (0); } diff --git a/bytepusher/config.mk b/bytepusher/config.mk @@ -5,6 +5,8 @@ VERSION = 0.1 # paths PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man +# OpenBSD +#MANPREFIX = ${PREFIX}/man # includes and libs INCS = -Iinclude -I${PREFIX}/include diff --git a/fnc/Makefile b/fnc/Makefile @@ -4,6 +4,8 @@ PREFIX = /usr/local MAN_DIR = ${PREFIX}/share/man/man1 +# OpenBSD +#MAN_DIR = ${PREFIX}//man/man1 BIN_DIR = ${PREFIX}/bin BIN = fnc diff --git a/mandelbrot.c b/mandelbrot.c @@ -0,0 +1,51 @@ +#include <math.h> +#include <stdio.h> + +#define IMGW 1920 +#define IMGH 1080 +#define MAXN 1024 +#define MINR (-1.5) +#define MAXR 0.7 +#define MINI (-1.0) +#define MAXI 1.0 + +#define TOREAL(x) ((x) * ((MAXR - MINR) / IMGW) + MINR) +#define TOIMGN(y) ((y) * ((MAXI - MINI) / IMGH) + MINI) + +static int +fmandelbrot(double cr, double ci) +{ + double zr = 0.0, zi = 0.0, tmp; + int i = 0; + + for (; i < MAXN && (zr * zr + zi * zi) < 4.0; i++) { + tmp = zr * zr - zi * zi + cr; + zi = 2.0 * zr * zi + ci; + zr = tmp; + } + + return (i); +} + +int +main(int argc, char *argv[]) +{ + double cr, ci; + int x, y, n, r, g, b; + + printf("P3\n%d %d\n256\n", IMGW, IMGH); + for (y = 0; y < IMGH; y++) { + for (x = 0; x < IMGW; x++) { + cr = TOREAL(x); + ci = TOIMGN(y); + n = fmandelbrot(cr, ci); + r = (n % 256); + g = ((int)(n * sinf(M_PI)) % 256); + b = ((n * 3) % 256); + printf("%d %d %d ", r, g, b); + } + printf("\n"); + } + + return (0); +} diff --git a/minecurses/config.mk b/minecurses/config.mk @@ -5,6 +5,8 @@ VERSION = 0.1 # paths PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man +# OpenBSD +#MANPREFIX = ${PREFIX}/man # includes and libs INCS = -Iinclude diff --git a/minecurses/minecurses.c b/minecurses/minecurses.c @@ -224,7 +224,7 @@ valset(int ymax, int offy, const char *msg, int min, int max) scanw("%d", &val); } while (val < min || val > max); - return val; + return (val); } static int @@ -241,7 +241,7 @@ adjcount(const struct minecurses *m, int r, int c) if (!OUT_OF_BOUNDS(m, r+1, c+1) && IS_MINE(m, r+1, c+1)) n++; // south-east if (!OUT_OF_BOUNDS(m, r-1, c+1) && IS_MINE(m, r-1, c+1)) n++; // south-west - return n; + return (n); } static void @@ -344,7 +344,7 @@ emalloc(size_t nb) if ((p = malloc(nb)) == NULL) err(1, "malloc"); - return p; + return (p); } int @@ -363,5 +363,5 @@ main(int argc, char *argv[]) endwin(); free(m); - return 0; + return (0); } diff --git a/morse/morse.cpp b/morse/morse.cpp @@ -58,7 +58,7 @@ main(int argc, char *argv[]) }; if (SDL_Init(SDL_INIT_AUDIO) < 0) - return -1; + return (1); Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096); dot = Mix_LoadWAV("dot.wav"); dash = Mix_LoadWAV("dash.wav"); @@ -93,5 +93,5 @@ main(int argc, char *argv[]) Mix_CloseAudio(); SDL_Quit(); - return 0; + return (0); } diff --git a/nnc.cpp b/nnc.cpp @@ -0,0 +1,169 @@ +#include <algorithm> +#include <cmath> +#include <cstdlib> +#include <ctime> +#include <vector> +#include <curses.h> + +static int ymax() +{ + return (getmaxy(stdscr)); +} + +static int xmax() +{ + return (getmaxx(stdscr)); +} + +struct Color { + int x, y; + int color; + + Color(int x, int y, int color) + :x(x), y(y), color(color) {} + + int dist(const Color& c) + { + int dx = this->x - c.x; + int dy = this->y - c.y; + + return (std::sqrt(dx * dx + dy * dy)); + } +}; + +struct Blue: public Color { + Blue(int x, int y, int color) + :Color(x, y, color) {} +}; + +struct Green: public Color { + Green(int x, int y, int color) + :Color(x, y, color) {} +}; + +struct White: public Color { + White(int x, int y, int color) + :Color(x, y, color) {} +}; + +static void +cursesinit() +{ + if (!initscr()) + exit(1); + cbreak(); + noecho(); + curs_set(0); + start_color(); + init_pair(1, COLOR_BLUE, COLOR_BLUE); + init_pair(2, COLOR_GREEN, COLOR_GREEN); + init_pair(3, COLOR_WHITE, COLOR_WHITE); +} + +static void +makepts(std::vector<Color *>& points) +{ + int x, y; + + for (std::size_t i = 0; i < 5; i++) { + x = std::rand() % (xmax() - 1); + y = std::rand() % (ymax() - 1); + points.push_back(new Blue(x, y, 1)); + } + for (std::size_t i = 0; i < 5; i++) { + x = std::rand() % (xmax() - 1); + y = std::rand() % (ymax() - 1); + points.push_back(new Green(x, y, 2)); + } +} + +static void +makewts(std::vector<White *>& whites) +{ + int x, y; + + for (std::size_t i = 0; i < 5; i++) { + x = std::rand() % (xmax() - 1); + y = std::rand() % (ymax() - 1); + whites.push_back(new White(x, y, 3)); + } +} + +template<typename T> static void +print(const std::vector<T *>& vec) +{ + for (auto& v : vec) { + attron(COLOR_PAIR(v->color)); + mvaddch(v->y, v->x, ACS_CKBOARD); + attroff(COLOR_PAIR(v->color)); + } +} + +static std::vector<int> +calc_dists(const std::vector<Color *>& points, const White& w) +{ + std::vector<int> dists; + + for (auto& point : points) + dists.push_back(point->dist(w)); + return (dists); +} + +static void +find_nn(const std::vector<Color *>& points, std::vector<White *>& whites) +{ + std::vector<int> dists; + int mindist; + + for (const auto& point : points) { + for (auto&& white : whites) { + dists = calc_dists(points, *white); + mindist = *std::min_element(dists.begin(), dists.end()); + if (point->dist(*white) == mindist) + white->color = point->color; + } + } +} + +template<typename T> static void +dealloc(std::vector<T *>& vec) +{ + for (auto&& v : vec) + if (v != nullptr) + delete v; + if (!vec.empty()) + vec.clear(); +} + +int +main(int argc, char **argv) +{ + std::vector<Color *> points; + std::vector<White *> whites; + + cursesinit(); + std::srand(std::time(nullptr)); + + makepts(points); + makewts(whites); + + erase(); + print<Color>(points); + print<White>(whites); + refresh(); + getch(); + + find_nn(points, whites); + + erase(); + print<Color>(points); + print<White>(whites); + refresh(); + getch(); + + endwin(); + dealloc<Color>(points); + dealloc<White>(whites); + + return (0); +} diff --git a/other/bounce.c b/other/bounce.c @@ -1,55 +0,0 @@ -#include <curses.h> -#include <stdio.h> -#include <unistd.h> - -struct ball { - int x; - int y; - int vx; - int vy; - int xmax; - int ymax; -}; - -static void -cursesinit(void) -{ - if (!initscr()) - exit(1); - cbreak(); - noecho(); - curs_set(0); -} - -static void -collision(struct ball *b) -{ - if (b->y < 2 || b->y > b->ymax-1) - b->vy *= -1.0f; - if (b->x < 1 || b->x > b->xmax-1) - b->vx *= -1.0f; - b->y += b->vy; - b->x += b->vx; -} - -int -main(int argc, char *argv[]) -{ - struct ball b = {1, 2, 1, 1, 0, 0}; - - cursesinit(); - getmaxyx(stdscr, b.ymax, b.xmax); - - for (;;) { - erase(); - collision(&b); - mvaddch(b.y, b.x, 'O'); - mvprintw(0, 0, "(x, y) = (%d, %d)", b.x, b.y); - mvhline(1, 0, ACS_HLINE, b.xmax); - refresh(); - usleep(15000); - } - endwin(); - - return 0; -} diff --git a/other/mandelbrot.c b/other/mandelbrot.c @@ -1,51 +0,0 @@ -#include <math.h> -#include <stdio.h> - -#define IMGW 1920 -#define IMGH 1080 -#define MAXN 1024 -#define MINR (-1.5) -#define MAXR 0.7 -#define MINI (-1.0) -#define MAXI 1.0 - -#define TOREAL(x) ((x) * ((MAXR - MINR) / IMGW) + MINR) -#define TOIMGN(y) ((y) * ((MAXI - MINI) / IMGH) + MINI) - -static int -fmandelbrot(double cr, double ci) -{ - double zr = 0.0, zi = 0.0, tmp; - int i = 0; - - for (; i < MAXN && (zr * zr + zi * zi) < 4.0; i++) { - tmp = zr * zr - zi * zi + cr; - zi = 2.0 * zr * zi + ci; - zr = tmp; - } - - return i; -} - -int -main(int argc, char *argv[]) -{ - double cr, ci; - int x, y, n, r, g, b; - - printf("P3\n%d %d\n256\n", IMGW, IMGH); - for (y = 0; y < IMGH; y++) { - for (x = 0; x < IMGW; x++) { - cr = TOREAL(x); - ci = TOIMGN(y); - n = fmandelbrot(cr, ci); - r = (n % 256); - g = ((int)(n * sinf(M_PI)) % 256); - b = ((n * 3) % 256); - printf("%d %d %d ", r, g, b); - } - printf("\n"); - } - - return 0; -} diff --git a/other/nnc.cpp b/other/nnc.cpp @@ -1,169 +0,0 @@ -#include <algorithm> -#include <cmath> -#include <cstdlib> -#include <ctime> -#include <vector> -#include <curses.h> - -static int ymax() -{ - return getmaxy(stdscr); -} - -static int xmax() -{ - return getmaxx(stdscr); -} - -struct Color { - int x, y; - int color; - - Color(int x, int y, int color) - :x(x), y(y), color(color) {} - - int dist(const Color& c) - { - int dx = this->x - c.x; - int dy = this->y - c.y; - - return std::sqrt(dx * dx + dy * dy); - } -}; - -struct Blue: public Color { - Blue(int x, int y, int color) - :Color(x, y, color) {} -}; - -struct Green: public Color { - Green(int x, int y, int color) - :Color(x, y, color) {} -}; - -struct White: public Color { - White(int x, int y, int color) - :Color(x, y, color) {} -}; - -static void -cursesinit() -{ - if (!initscr()) - exit(1); - cbreak(); - noecho(); - curs_set(0); - start_color(); - init_pair(1, COLOR_BLUE, COLOR_BLUE); - init_pair(2, COLOR_GREEN, COLOR_GREEN); - init_pair(3, COLOR_WHITE, COLOR_WHITE); -} - -static void -makepts(std::vector<Color *>& points) -{ - int x, y; - - for (std::size_t i = 0; i < 5; i++) { - x = std::rand() % (xmax() - 1); - y = std::rand() % (ymax() - 1); - points.push_back(new Blue(x, y, 1)); - } - for (std::size_t i = 0; i < 5; i++) { - x = std::rand() % (xmax() - 1); - y = std::rand() % (ymax() - 1); - points.push_back(new Green(x, y, 2)); - } -} - -static void -makewts(std::vector<White *>& whites) -{ - int x, y; - - for (std::size_t i = 0; i < 5; i++) { - x = std::rand() % (xmax() - 1); - y = std::rand() % (ymax() - 1); - whites.push_back(new White(x, y, 3)); - } -} - -template<typename T> static void -print(const std::vector<T *>& vec) -{ - for (auto& v : vec) { - attron(COLOR_PAIR(v->color)); - mvaddch(v->y, v->x, ACS_CKBOARD); - attroff(COLOR_PAIR(v->color)); - } -} - -static std::vector<int> -calc_dists(const std::vector<Color *>& points, const White& w) -{ - std::vector<int> dists; - - for (auto& point : points) - dists.push_back(point->dist(w)); - return dists; -} - -static void -find_nn(const std::vector<Color *>& points, std::vector<White *>& whites) -{ - std::vector<int> dists; - int mindist; - - for (const auto& point : points) { - for (auto&& white : whites) { - dists = calc_dists(points, *white); - mindist = *std::min_element(dists.begin(), dists.end()); - if (point->dist(*white) == mindist) - white->color = point->color; - } - } -} - -template<typename T> static void -dealloc(std::vector<T *>& vec) -{ - for (auto&& v : vec) - if (v != nullptr) - delete v; - if (!vec.empty()) - vec.clear(); -} - -int -main(int argc, char **argv) -{ - std::vector<Color *> points; - std::vector<White *> whites; - - cursesinit(); - std::srand(std::time(nullptr)); - - makepts(points); - makewts(whites); - - erase(); - print<Color>(points); - print<White>(whites); - refresh(); - getch(); - - find_nn(points, whites); - - erase(); - print<Color>(points); - print<White>(whites); - refresh(); - getch(); - - endwin(); - dealloc<Color>(points); - dealloc<White>(whites); - - return 0; -} diff --git a/other/snake.cpp b/other/snake.cpp @@ -1,187 +0,0 @@ -#include <chrono> -#include <cstdlib> -#include <ctime> -#include <list> -#include <thread> - -#include <curses.h> - -#define XMAX (getmaxx(stdscr)) -#define YMAX (getmaxy(stdscr)) - -struct Snake { - struct Seg { - int x; - int y; - }; - std::list<Seg> body; - int x; - int y; - int score; - bool dead; - - Snake(); - void update(int key); - void grow(); - void draw(); - bool collided(); -}; - -struct Food { - int x; - int y; - - Food(); - void spawn(); - void draw(); -}; - -Snake::Snake() -{ - y = YMAX >> 1; - x = XMAX >> 1; - body = {{x, y}, {x + 1, y + 1}}; - score = 1; - dead = false; -} - -void -Snake::update(int key) -{ - switch (key) { - case KEY_UP: - y--; - body.push_front({body.front().x, body.front().y - 1}); - break; - case KEY_DOWN: - y++; - body.push_front({body.front().x, body.front().y + 1}); - break; - case KEY_LEFT: - x--; - body.push_front({body.front().x - 1, body.front().y}); - break; - case KEY_RIGHT: - x++; - body.push_front({body.front().x + 1, body.front().y}); - break; - default: - return; - } - body.pop_back(); -} - -void -Snake::grow() -{ - for (int i = 0; i < 3; i++) - body.push_back({body.back().x, body.back().y}); -} - -void -Snake::draw() -{ - for (const auto& b : body) - mvaddch(b.y, b.x, ACS_CKBOARD); -} - -bool -Snake::collided() -{ - dead = y < 2 || y > YMAX - 1 || x < 1 || x > XMAX - 1; - for (std::list<Seg>::iterator i = body.begin(); i != body.end(); i++) - if (i != body.begin() - && i->x == body.front().x - && i->y == body.front().y) - dead = true; - return dead; -} - -Food::Food() -{ - x = std::rand() % XMAX - 1; - y = std::rand() % (YMAX - 2) + 2; -} - -void -Food::spawn() -{ - x = std::rand() % XMAX - 1; - y = std::rand() % (YMAX - 2) + 2; -} - -void -Food::draw() -{ - mvaddch(y, x, 'O'); -} - -static void -initcurses() -{ - if (!initscr()) - exit(1); - cbreak(); - noecho(); - curs_set(0); - keypad(stdscr, true); - nodelay(stdscr, true); -} - -static bool -kbhit() -{ - int c; - - if ((c = getch()) != ERR) { - ungetch(c); - return true; - } - return false; -} - -int -main(int argc, char *argv[]) -{ - initcurses(); - std::srand(std::time(nullptr)); - - Snake snake; - Food food; - int key, nextkey; - - key = KEY_RIGHT; - - for (;;) { - erase(); - if (kbhit()) { - if ((nextkey = getch()) != key) - key = nextkey; - else - continue; - } - - snake.update(key); - if (snake.collided() || key == 'q') - break; - - if (snake.body.front().y == food.y - && snake.body.front().x == food.x) { - food.spawn(); - snake.grow(); - snake.score++; - } - - food.draw(); - snake.draw(); - - mvprintw(0, 0, "Score: %d", snake.score); - mvhline(1, 0, ACS_HLINE, XMAX); - refresh(); - std::this_thread::sleep_for(std::chrono::milliseconds(60)); - } - - endwin(); - - return 0; -} diff --git a/shitcoin/README b/shitcoin/README @@ -1,6 +1,7 @@ -shitcoin - a useless cryptocurrency -==================================== -don't use it, it's just for fun +shitcoin +======== + +A usless cryptocurrency. Don't use it, it's just for fun. Installation ------------- diff --git a/shitcoin/config.mk b/shitcoin/config.mk @@ -5,6 +5,8 @@ VERSION = 0.1 # paths PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man +# OpenBSD +#MANPREFIX = ${PREFIX}/man # includes and libs INCS = -Iinclude diff --git a/shitcoin/shitcoin.c b/shitcoin/shitcoin.c @@ -77,7 +77,7 @@ newblock(const char *saddr, const char *raddr, long amount, const char *prevhash strcpy(b->hash, calchash(b)); b->nonce = 0; - return b; + return (b); } static char * @@ -101,7 +101,7 @@ calchash(struct block *b) sprintf(&res[i << 1], "%02x", hash[i]); res[HASH_LEN] = '\0'; - return res; + return (res); } static void @@ -160,7 +160,7 @@ mineblock(struct block *b) static struct block * lastblock(void) { - return chain->blocks[chain->nblocks - 1]; + return (chain->blocks[chain->nblocks - 1]); } static int @@ -171,13 +171,13 @@ validchain(void) for (; i < chain->nblocks; i++) { if (i != 0 && strcmp(chain->blocks[i]->prevhash, chain->blocks[i-1]->hash)) - return 0; + return (0); if (i != 0 && strcmp(chain->blocks[i]->hash, calchash(chain->blocks[i]))) - return 0; + return (0); } - return 1; + return (1); } static long @@ -193,7 +193,7 @@ balance(const char *addr) bal += chain->blocks[i]->data->amount; } - return bal; + return (bal); } static void @@ -236,7 +236,7 @@ emalloc(size_t nb) if ((p = malloc(nb)) == NULL) err(1, "malloc"); - return p; + return (p); } int @@ -254,5 +254,5 @@ main(int argc, char *argv[]) cleanchain(); - return 0; + return (0); } diff --git a/snake.cpp b/snake.cpp @@ -0,0 +1,187 @@ +#include <chrono> +#include <cstdlib> +#include <ctime> +#include <list> +#include <thread> + +#include <curses.h> + +#define XMAX (getmaxx(stdscr)) +#define YMAX (getmaxy(stdscr)) + +struct Snake { + struct Seg { + int x; + int y; + }; + std::list<Seg> body; + int x; + int y; + int score; + bool dead; + + Snake(); + void update(int key); + void grow(); + void draw(); + bool collided(); +}; + +struct Food { + int x; + int y; + + Food(); + void spawn(); + void draw(); +}; + +Snake::Snake() +{ + y = YMAX >> 1; + x = XMAX >> 1; + body = {{x, y}, {x + 1, y + 1}}; + score = 1; + dead = false; +} + +void +Snake::update(int key) +{ + switch (key) { + case KEY_UP: + y--; + body.push_front({body.front().x, body.front().y - 1}); + break; + case KEY_DOWN: + y++; + body.push_front({body.front().x, body.front().y + 1}); + break; + case KEY_LEFT: + x--; + body.push_front({body.front().x - 1, body.front().y}); + break; + case KEY_RIGHT: + x++; + body.push_front({body.front().x + 1, body.front().y}); + break; + default: + return; + } + body.pop_back(); +} + +void +Snake::grow() +{ + for (int i = 0; i < 3; i++) + body.push_back({body.back().x, body.back().y}); +} + +void +Snake::draw() +{ + for (const auto& b : body) + mvaddch(b.y, b.x, ACS_CKBOARD); +} + +bool +Snake::collided() +{ + dead = y < 2 || y > YMAX - 1 || x < 1 || x > XMAX - 1; + for (std::list<Seg>::iterator i = body.begin(); i != body.end(); i++) + if (i != body.begin() + && i->x == body.front().x + && i->y == body.front().y) + dead = true; + return (dead); +} + +Food::Food() +{ + x = std::rand() % XMAX - 1; + y = std::rand() % (YMAX - 2) + 2; +} + +void +Food::spawn() +{ + x = std::rand() % XMAX - 1; + y = std::rand() % (YMAX - 2) + 2; +} + +void +Food::draw() +{ + mvaddch(y, x, 'O'); +} + +static void +initcurses() +{ + if (!initscr()) + exit(1); + cbreak(); + noecho(); + curs_set(0); + keypad(stdscr, true); + nodelay(stdscr, true); +} + +static bool +kbhit() +{ + int c; + + if ((c = getch()) != ERR) { + ungetch(c); + return (true); + } + return (false); +} + +int +main(int argc, char *argv[]) +{ + initcurses(); + std::srand(std::time(nullptr)); + + Snake snake; + Food food; + int key, nextkey; + + key = KEY_RIGHT; + + for (;;) { + erase(); + if (kbhit()) { + if ((nextkey = getch()) != key) + key = nextkey; + else + continue; + } + + snake.update(key); + if (snake.collided() || key == 'q') + break; + + if (snake.body.front().y == food.y + && snake.body.front().x == food.x) { + food.spawn(); + snake.grow(); + snake.score++; + } + + food.draw(); + snake.draw(); + + mvprintw(0, 0, "Score: %d", snake.score); + mvhline(1, 0, ACS_HLINE, XMAX); + refresh(); + std::this_thread::sleep_for(std::chrono::milliseconds(60)); + } + + endwin(); + + return (0); +}