commit b30ce3a35ef5750eff362b3ca477f73cc8c6e276
parent 412eca38e2f2919ba0a11796065cf1c7ac9c0a08
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 26 May 2021 19:03:00 +0300
changed mail
Diffstat:
25 files changed, 280 insertions(+), 343 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-(c) 2019-Present Christos Margiolis <christos@christosmarg.xyz>
+(c) 2019-Present Christos Margiolis <christos@margiolis.net>
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
diff --git a/brainfuck/Makefile b/brainfuck/Makefile
@@ -1,4 +1,5 @@
-# brainfuck - a brainfuck interpreter
+# See LICENSE file for copyright and license details.
+# brainfuck - brainfuck interpreter
.POSIX:
include config.mk
@@ -25,28 +26,28 @@ ${BIN}: ${OBJ}
${CC} -c ${CFLAGS} $<
dist: clean
- ${MKDIR} ${DIST}
- ${CP} -R tests/ config.mk ${MAN1} ${SRC} LICENSE Makefile README.md ${DIST}
- ${TAR} ${DIST}.tar ${DIST}
- ${GZIP} ${DIST}.tar
- ${RM_DIR} ${DIST}
+ 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} ${DESTDIR}${BIN_DIR} ${DESTDIR}${MAN_DIR}
- ${CP} ${BIN} ${BIN_DIR}
- ${CP} ${MAN1} ${DESTDIR}${MAN_DIR}
- sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MAN_DIR}/${MAN1}
- chmod 755 ${DESTDIR}${BIN_DIR}/${BIN}
- chmod 644 ${DESTDIR}${MAN_DIR}/${MAN1}
+ 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} ${DESTDIR}${BIN_DIR}/${BIN}
- ${RM} ${DESTDIR}${MAN_DIR}/${MAN1}
+ rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \
+ ${DESTDIR}${MANPREFIX}/man1/${MAN1}
clean:
- ${RM} ${BIN} ${OBJ} ${DIST}.tar.gz
+ rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core
.PHONY: all options clean dist install uninstall run
diff --git a/brainfuck/brainfuck.1 b/brainfuck/brainfuck.1
@@ -22,4 +22,4 @@ Alternatively, you can pipe to
.Dl $ cat src.bf | brainfuck
.Pp
.Sh AUTHORS
-.An Christos Margiolis Aq Mt christos@christosmarg.xyz
+.An Christos Margiolis Aq Mt christos@margiolis.net
diff --git a/brainfuck/brainfuck.c b/brainfuck/brainfuck.c
@@ -19,7 +19,7 @@ main(int argc, char *argv[])
if ((src = malloc(len)) == NULL) {
perror("malloc");
- exit(EXIT_FAILURE);
+ exit(1);
}
strcpy(src, buf);
memset(buf, 0, len);
diff --git a/brainfuck/config.mk b/brainfuck/config.mk
@@ -1,13 +1,13 @@
+# See LICENSE file for copyright and license details.
# brainfuck version
-VERSION = 0
+VERSION = 0.1
# paths
PREFIX = /usr/local
-MAN_DIR = ${PREFIX}/share/man/man1
-BIN_DIR = ${PREFIX}/bin
+MANPREFIX = ${PREFIX}/share/man
# includes and libs
-INCS = -Iinclude
+INCS = -Iinclude
LIBS = -Llib
# flags
@@ -16,15 +16,5 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = ${LIBS}
-# utils
-CP = cp -f
-RM = rm -f
-RM_DIR = rm -rf
-MV = mv
-MKDIR = mkdir -p
-RM_DIR = rm -rf
-TAR = tar -cf
-GZIP = gzip
-
# compiler
CC = cc
diff --git a/bytepusher/Makefile b/bytepusher/Makefile
@@ -1,4 +1,5 @@
-# bytepusher - a Bytepusher VM implementation
+# See LICENSE file for copyright and license details.
+# bytepusher - bytepusher VM implementation
.POSIX:
include config.mk
@@ -25,28 +26,29 @@ ${BIN}: ${OBJ}
${CC} -c ${CFLAGS} $<
dist: clean
- ${MKDIR} ${DIST}
- ${CP} -R roms/ ${MAN1} ${SRC} config.mk LICENSE Makefile README.md ${DIST}
- ${TAR} ${DIST}.tar ${DIST}
- ${GZIP} ${DIST}.tar
- ${RM_DIR} ${DIST}
+ mkdir -p ${DIST}
+ cp -R roms bytepusher.1 bytepusher.c config.mk Makefile \
+ README.md ${DIST}
+ tar -cf ${DIST}.tar ${DIST}
+ gzip ${DIST}.tar
+ rm -rf ${DIST}
run:
./${BIN}
install: all
- ${MKDIR} ${DESTDIR}${BIN_DIR} ${DESTDIR}${MAN_DIR}
- ${CP} ${BIN} ${BIN_DIR}
- ${CP} ${MAN1} ${DESTDIR}${MAN_DIR}
- sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MAN_DIR}/${MAN1}
- chmod 755 ${DESTDIR}${BIN_DIR}/${BIN}
- chmod 644 ${DESTDIR}${MAN_DIR}/${MAN1}
+ 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} ${DESTDIR}${BIN_DIR}/${BIN}
- ${RM} ${DESTDIR}${MAN_DIR}/${MAN1}
+ rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \
+ ${DESTDIR}${MANPREFIX}/man1/${MAN1}
clean:
- ${RM} ${BIN} ${OBJ} ${DIST}.tar.gz
+ rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core
.PHONY: all options clean dist install uninstall run
diff --git a/bytepusher/bytepusher.1 b/bytepusher/bytepusher.1
@@ -12,4 +12,4 @@
reads a Bytepusher source file and executes it. It
uses the SDL2 library as a rendering API.
.Sh AUTHORS
-.An Christos Margiolis Aq Mt christos@christosmarg.xyz
+.An Christos Margiolis Aq Mt christos@margiolis.net
diff --git a/bytepusher/bytepusher.c b/bytepusher/bytepusher.c
@@ -89,11 +89,11 @@ main(int argc, char *argv[])
if (argc != 2) {
fprintf(stderr, "usage: %s rom\n", argv[0]);
- return EXIT_FAILURE;
+ return 1;
}
if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "fopen: %s\n", argv[1]);
- return EXIT_FAILURE;
+ 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 EXIT_FAILURE;
+ return 1;
}
for (r = 0; r < 6; r++)
@@ -132,5 +132,5 @@ main(int argc, char *argv[])
SDL_DestroyWindow(win);
SDL_Quit();
- return EXIT_SUCCESS;
+ return 0;
}
diff --git a/bytepusher/config.mk b/bytepusher/config.mk
@@ -1,30 +1,20 @@
+# See LICENSE file for copyright and license details.
# bytepusher version
-VERSION = 0
+VERSION = 0.1
# paths
PREFIX = /usr/local
-MAN_DIR = ${PREFIX}/share/man/man1
-BIN_DIR = ${PREFIX}/bin
+MANPREFIX = ${PREFIX}/share/man
# includes and libs
-INCS = -Iinclude
-LIBS = -Llib -lSDL2
+INCS = -Iinclude -I${PREFIX}/include
+LIBS = -Llib -L${PREFIX}/lib -lSDL2
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
-D_XOPEN_SOURCE=700 -DVERSION=\"${VERSION}\"
-CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os ${INCS} ${CPPFLAGS}
+CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = ${LIBS}
-# utils
-CP = cp -f
-RM = rm -f
-RM_DIR = rm -rf
-MV = mv
-MKDIR = mkdir -p
-RM_DIR = rm -rf
-TAR = tar -cf
-GZIP = gzip
-
# compiler
-CC = gcc
+CC = cc
diff --git a/fnc/fnc.1 b/fnc/fnc.1
@@ -25,4 +25,4 @@ The dates must also not have spaces between them
.It Fl d Ar date
prints only the fields matching the specified date.
.Sh AUTHORS
-.An Christos Margiolis Aq Mt christos@christosmarg.xyz
+.An Christos Margiolis Aq Mt christos@margiolis.net
diff --git a/minecurses/Makefile b/minecurses/Makefile
@@ -1,4 +1,5 @@
-# minecurses - a terminal minesweeper game
+# See LICENSE file for copyright and license details.
+# minecurses - curses(3) minesweeper game
.POSIX:
include config.mk
@@ -25,29 +26,29 @@ ${BIN}: ${OBJ}
${CC} -c ${CFLAGS} $<
dist: clean
- ${MKDIR} ${DIST}
- ${CP} -R log/ res/ config.mk defs.h LICENSE Makefile ${MAN} ${SRC} \
- README.md ${DIST}
- ${TAR} ${DIST}.tar ${DIST}
- ${GZIP} ${DIST}.tar
- ${RM_DIR} ${DIST}
+ mkdir -p ${DIST}
+ cp -R config.mk defs.h Makefile minecurses.6 minecurses.c \
+ README.md ${DIST}
+ tar -cf ${DIST}.tar ${DIST}
+ gzip ${DIST}.tar
+ rm -rf ${DIST}
run:
./${BIN}
install: all
- ${MKDIR} ${DESTDIR}${BIN_DIR} ${DESTDIR}${MAN_DIR}
- ${CP} ${BIN} ${BIN_DIR}
- ${CP} ${MAN6} ${DESTDIR}${MAN_DIR}
- sed "s/VERSION/${VERSION}/g" < ${MAN6} > ${DESTDIR}${MAN_DIR}/${MAN6}
- chmod 755 ${DESTDIR}${BIN_DIR}/${BIN}
- chmod 644 ${DESTDIR}${MAN_DIR}/${MAN6}
+ mkdir -p ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANPREFIX}/man6
+ cp -f ${BIN} ${DESTDIR}${PREFIX}/bin
+ cp -f ${MAN6} ${DESTDIR}${MANPREFIX}/man6
+ sed "s/VERSION/${VERSION}/g" < ${MAN6} > ${DESTDIR}${MANPREFIX}/man6/${MAN6}
+ chmod 755 ${DESTDIR}${PREFIX}/bin/${BIN}
+ chmod 644 ${DESTDIR}${MANPREFIX}/man6/${MAN6}
uninstall:
- ${RM} ${DESTDIR}${BIN_DIR}/${BIN}
- ${RM} ${DESTDIR}${MAN_DIR}/${MAN6}
+ rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \
+ ${DESTDIR}${MANPREFIX}/man6/${MAN6}
clean:
- ${RM} ${BIN} ${OBJ} ${DIST}.tar.gz
+ rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core
.PHONY: all options clean dist install uninstall run
diff --git a/minecurses/README.md b/minecurses/README.md
@@ -1,17 +1,17 @@
# minecurses
-A minesweeper game using `ncurses`. The game was originally made as a
+A minesweeper game using `curses`. The game was originally made as a
university assignment of mine, but I decided to take it a bit further afterwards.
## How to play
The objective is to simply find and defuse all the mines, not open all the non-mine
-cells, like in most minesweeper games; this make the game relatively harder.
+cells, like in most minesweeper games; this makes the game relatively harder.
* Enter number of columns
* Enter number of rows
* Enter number of mines
-* Move with `w`/`s`/`a`/`d` or Vim keys (`k`/`j`/`h`/`l`)
+* Move with `w`/`s`/`a`/`d` keys
* Open cell with `[ENTER]`, or `o`
* Flag cell with `f`
* Defuse mine (only if flagged) with `g`
@@ -21,18 +21,16 @@ cells, like in most minesweeper games; this make the game relatively harder.
Additional controls
* `m`: Open control menu
-* `r`: Restart the game
* `q`: Quit
## Usage
```shell
-$ cd path/to/minecurses
$ make && make run
$ make clean # optional
```
-You can install `minecurses` by running `sudo make install clean`.
-The binary will be installed in `/usr/local/bin`.
+You can install `minecurses` by running `make install`. The binary will be
+installed in `/usr/local/bin`.
## Preview
@@ -44,4 +42,3 @@ The binary will be installed in `/usr/local/bin`.
## To Do
* Add colors
-* Fix occasional wrong mine spawning bug
diff --git a/minecurses/config.mk b/minecurses/config.mk
@@ -1,14 +1,14 @@
+# See LICENSE file for copyright and license details.
# minecurses version
-VERSION = 0
+VERSION = 0.1
# paths
PREFIX = /usr/local
-MAN_DIR = ${PREFIX}/share/man/man6
-BIN_DIR = ${PREFIX}/bin
+MANPREFIX = ${PREFIX}/share/man
# includes and libs
-INCS = -Iinclude
-LIBS = -Llib -lncurses
+INCS = -Iinclude
+LIBS = -Llib -lncursesw
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
@@ -16,15 +16,5 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = ${LIBS}
-# utils
-CP = cp -f
-RM = rm -f
-RM_DIR = rm -rf
-MV = mv
-MKDIR = mkdir -p
-RM_DIR = rm -rf
-TAR = tar -cf
-GZIP = gzip
-
# compiler
-CC = gcc
+CC = cc
diff --git a/minecurses/defs.h b/minecurses/defs.h
@@ -1,20 +1,15 @@
#ifndef DEFS_H
#define DEFS_H
-#define MOVE_UP_NORM 'k'
-#define MOVE_UP_VIM 'w'
-#define MOVE_DOWN_NORM 's'
-#define MOVE_DOWN_VIM 'j'
-#define MOVE_LEFT_NORM 'a'
-#define MOVE_LEFT_VIM 'h'
-#define MOVE_RIGHT_NORM 'd'
-#define MOVE_RIGHT_VIM 'l'
+#define MOVE_UP 'w'
+#define MOVE_DOWN 's'
+#define MOVE_LEFT 'a'
+#define MOVE_RIGHT 'd'
#define MOVE_ENTER '\n'
#define MOVE_OPEN_CELL 'o'
#define MOVE_FLAG_CELL 'f'
#define MOVE_DEFUSE_CELL 'g'
-#define MOVE_OPEN_MENU 'm'
-#define MOVE_RESTART 'r'
+#define MOVE_OPEN_MENU 'c'
#define MOVE_QUIT 'q'
#define MINE_DEFUSED 'D'
@@ -23,9 +18,8 @@
#define CELL_MINE '*'
#define GRID_BOX "[ ]"
-#define OPT_CTRLS "m Controls"
+#define OPT_CTRLS "c Controls"
#define OPT_QUIT "q Quit"
-#define OPT_RESTART "r Restart"
#define OPT_MOVE_UP "w/k Move up"
#define OPT_MOVE_DOWN "s/j Move down"
#define OPT_MOVE_LEFT "a/h Move left"
@@ -49,8 +43,6 @@
#define GAME_LOST 0
#define GAME_WON 1
-#define YMAX(x) (getmaxy((x)))
-#define XMAX(x) (getmaxx((x)))
#define YMID(x) (getmaxy((x)) >> 1)
#define XMID(x) (getmaxx((x)) >> 1)
#define SCRSPACE_X(x) ((x) * 3 + 2)
@@ -58,11 +50,11 @@
#define ARRSPACE_X(x) (((x) - 2) / 3)
#define ARRSPACE_Y(y) ((y) - 1)
#define CENTER(x, y) (((x) >> 1) - ((y) >> 1))
-#define CURS_UPDATE(m, y, x) (wmove((m)->gamewin, (y), (x)))
+#define CURS_UPDATE(m, y, x) (wmove((m)->gw, (y), (x)))
#define IS_MINE(m, r, c) ((m)->mineboard[(r)][(c)] == CELL_MINE)
#define IS_FLAGGED(m, r, c) ((m)->dispboard[(r)][(c)] == CELL_FLAGGED)
#define IS_BLANK(m, r, c) ((m)->dispboard[(r)][(c)] == CELL_BLANK)
-#define OUT_OF_BOUNDS(m, r, c) ((r) < 0 || (r) > ((m)->rows - 1) || \
- (c) < 0 || (c) > ((m)->cols - 1))
+#define OUT_OF_BOUNDS(m, r, c) \
+ ((r) < 0 || (r) > ((m)->rows - 1) || (c) < 0 || (c) > ((m)->cols - 1))
#endif /* DEFS_H */
diff --git a/minecurses/minecurses.6 b/minecurses/minecurses.6
@@ -8,24 +8,23 @@
.Nm
.Sh DESCRIPTION
.Nm
-starts a minesweeper game using the
-.Xr ncurses 3
-library to draw the TUI.
+starts a
+.Xr curses 3
+minesweeper game. The objective is to find and defuse all the mines,
+not open all the non-mine cells, like in most minesweeper games; this
+makes the game relatively harder.
.Sh OPTIONS
-The following option are used during runtime in the TUI,
-not the command line.
+The following option are used on runtime:
.Bl -tag -width 8n
.It Sy q
quit game
-.It Sy r
-restart game
-.It Sy w/k
+.It Sy w
move up on the Y axis
-.It Sy s/j
+.It Sy s
move down on the Y axis
-.It Sy a/h
+.It Sy a
move left on the X axis
-.It Sy d/l
+.It Sy d
move right on the X axis
.It Sy f
flag cell
@@ -34,6 +33,6 @@ defuse mine (if flagged only)
.It Sy ENTER/o
open cell
.Sh SEE ALSO
-.Xr ncurses 3
+.Xr curses 3
.Sh AUTHORS
-.An Christos Margiolis Aq Mt christos@christosmarg.xyz
+.An Christos Margiolis Aq Mt christos@margiolis.net
diff --git a/minecurses/minecurses.c b/minecurses/minecurses.c
@@ -1,56 +1,93 @@
+#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include <ncurses.h>
+#include <curses.h>
#include "defs.h"
+/* TODO:
+ * - play again
+ * - win fix
+ * - first open
+ */
+
struct minecurses {
- WINDOW *gamewin;
+ WINDOW *gw;
char **dispboard;
char **mineboard;
+ int x;
+ int y;
+ int xmax;
+ int ymax;
+ int wxmax;
+ int wymax;
int rows;
int cols;
int nmines;
- int ndefused;
+ int ndef;
int move;
- int x;
- int y;
int gameover;
};
+static void cursesinit(struct minecurses *);
+static void gwinit(struct minecurses *);
static void gamereset(struct minecurses *);
-static void gamerestart(struct minecurses *);
static void gamestart(struct minecurses *);
-static int valset(int, const char *, int, int);
+static int valset(int, int, const char *, int, int);
static int adjcount(const struct minecurses *, int, int);
static void boardsdealloc(struct minecurses *);
static void cellreveal(const struct minecurses *);
static void boardprint(const struct minecurses *);
-static WINDOW *gamewininit(int, int);
static void menuopts(void);
static void endscreen(struct minecurses *, int);
static void *emalloc(size_t);
static void
+cursesinit(struct minecurses *m)
+{
+ if (!initscr())
+ errx(1, "initscr");
+ noecho();
+ cbreak();
+
+ m->xmax = getmaxx(stdscr);
+ m->ymax = getmaxy(stdscr);
+}
+
+static void
+gwinit(struct minecurses *m)
+{
+ int wr, wc, wy, wx;
+
+ wr = SCRSPACE_Y(m->rows);
+ wc = SCRSPACE_X(m->cols);
+ wy = CENTER(m->ymax, wr);
+ wx = CENTER(m->xmax, wc);
+ if ((m->gw = newwin(wr, wc, wy, wx)) == NULL)
+ errx(1, "newwin");
+
+ m->wxmax = getmaxx(m->gw);
+ m->wymax = getmaxy(m->gw);
+}
+
+static void
gamereset(struct minecurses *m)
{
size_t i, j, r, c;
echo();
- m->cols = valset(4, MSG_COLS, 5, ARRSPACE_X(XMAX(stdscr)) - 2);
- m->rows = valset(3, MSG_ROWS, 5, ARRSPACE_Y(YMAX(stdscr)) - 3);
- m->nmines = valset(2, MSG_MINES, 1, m->rows * m->cols - 15);
+ m->cols = valset(m->ymax, 4, MSG_COLS, 5, ARRSPACE_X(m->xmax) - 2);
+ m->rows = valset(m->ymax, 3, MSG_ROWS, 5, ARRSPACE_Y(m->ymax) - 3);
+ m->nmines = valset(m->ymax, 2, MSG_MINES, 1, m->rows * m->cols - 15);
m->x = m->y = 0;
- m->ndefused = 0;
+ m->ndef = 0;
m->gameover = 0;
noecho();
+ gwinit(m);
menuopts();
- if (m->gamewin == NULL)
- m->gamewin = gamewininit(m->rows, m->cols);
-
/* allocate memory for the boards */
m->dispboard = emalloc(m->rows * sizeof(char *));
m->mineboard = emalloc(m->rows * sizeof(char *));
@@ -85,13 +122,6 @@ gamereset(struct minecurses *m)
m->mineboard[i][j] = '-';
}
-static void
-gamerestart(struct minecurses *m)
-{
- boardsdealloc(m);
- gamereset(m);
-}
-
#define bx m->x
#define by m->y
@@ -101,50 +131,44 @@ gamestart(struct minecurses *m)
static int y = 1, x = 2;
for (;;) {
+ werase(m->gw);
erase();
- // is this necessary?
- delwin(m->gamewin);
- m->gamewin = gamewininit(m->rows, m->cols);
- refresh();
- boardprint(m);
+ boardprint(m);
CURS_UPDATE(m, y, x);
bx = ARRSPACE_X(x);
by = ARRSPACE_Y(y);
/* session info */
mvprintw(0, 0, MSG_CURPOS, bx, by);
- mvprintw(0, XMID(stdscr) - ((strlen(MSG_NDEFUSED) - 2) >> 1),
- MSG_NDEFUSED, m->ndefused, m->nmines);
- mvprintw(0, XMAX(stdscr) - strlen(OPT_CTRLS), OPT_CTRLS);
+ mvprintw(0, CENTER(m->xmax, strlen(MSG_NDEFUSED) - 2),
+ MSG_NDEFUSED, m->ndef, m->nmines);
+ mvprintw(0, m->xmax - strlen(OPT_CTRLS), OPT_CTRLS);
refresh();
+ wrefresh(m->gw);
/* handle moves */
- switch (m->move = wgetch(m->gamewin)) {
- case MOVE_UP_NORM: /* FALLTHROUGH */
- case MOVE_UP_VIM:
+ switch (m->move = wgetch(m->gw)) {
+ case MOVE_UP:
if (--y < 1)
y = 1;
break;
- case MOVE_DOWN_NORM: /* FALLTHROUGH */
- case MOVE_DOWN_VIM:
- if (++y > XMAX(m->gamewin) - 2)
- y = YMAX(m->gamewin) - 2;
+ case MOVE_DOWN:
+ if (++y > m->wxmax - 2)
+ y = m->wymax - 2;
break;
- case MOVE_LEFT_NORM: /* FALLTHROUGH */
- case MOVE_LEFT_VIM:
+ case MOVE_LEFT:
x -= 3;
if (x < 2)
x = 2;
break;
- case MOVE_RIGHT_NORM: /* FALLTHROUGH */
- case MOVE_RIGHT_VIM:
+ case MOVE_RIGHT:
x += 3;
- if (x > XMAX(m->gamewin) - 3)
- x = XMAX(m->gamewin) - 3;
+ if (x > m->wxmax - 3)
+ x = m->wxmax - 3;
break;
- case MOVE_ENTER: /* FALLTHROUGH */
+ case MOVE_ENTER: /* FALLTHROUGH */
case MOVE_OPEN_CELL:
m->dispboard[by][bx] = m->mineboard[by][bx];
m->gameover = IS_MINE(m, by, bx);
@@ -161,7 +185,7 @@ gamestart(struct minecurses *m)
break;
case MOVE_DEFUSE_CELL:
if (IS_FLAGGED(m, by, bx) && IS_MINE(m, by, bx)) {
- m->ndefused++;
+ m->ndef++;
m->dispboard[by][bx] = MINE_DEFUSED;
m->mineboard[by][bx] = MINE_DEFUSED;
cellreveal(m);
@@ -170,24 +194,20 @@ gamestart(struct minecurses *m)
break;
case MOVE_OPEN_MENU:
menuopts();
- box(m->gamewin, 0, 0);
boardprint(m);
break;
- case MOVE_RESTART:
- gamerestart(m);
- break;
}
if (OUT_OF_BOUNDS(m, by, bx)
- || m->ndefused > m->nmines
|| m->gameover
+ || m->ndef == m->nmines
|| m->move == MOVE_QUIT)
break;
}
if (m->gameover)
endscreen(m, GAME_LOST);
- else if (m->ndefused == m->nmines)
+ else if (m->ndef == m->nmines)
endscreen(m, GAME_WON);
}
@@ -195,14 +215,15 @@ gamestart(struct minecurses *m)
#undef by
static int
-valset(int offy, const char *msg, int min, int max)
+valset(int ymax, int offy, const char *msg, int min, int max)
{
int val;
do {
- mvprintw(YMAX(stdscr)-offy, 1, msg, min, max);
+ mvprintw(ymax - offy, 1, msg, min, max);
scanw("%d", &val);
} while (val < min || val > max);
+
return val;
}
@@ -228,7 +249,7 @@ boardsdealloc(struct minecurses *m)
{
size_t i = 0;
- if (!m->dispboard && !m->mineboard) {
+ if (m->dispboard && m->mineboard) {
for (; i < m->rows; i++) {
free(m->dispboard[i]);
free(m->mineboard[i]);
@@ -241,10 +262,7 @@ boardsdealloc(struct minecurses *m)
static void
cellreveal(const struct minecurses *m)
{
- int y = m->y + 1;
- int x = SCRSPACE_X(m->x);
-
- mvwaddch(m->gamewin, y, x, m->dispboard[m->y][m->x]);
+ mvwaddch(m->gw, m->y + 1, SCRSPACE_X(m->x), m->dispboard[m->y][m->x]);
}
static void
@@ -252,35 +270,18 @@ boardprint(const struct minecurses *m)
{
size_t i, j, x, y;
- wattroff(m->gamewin, A_BOLD);
+ wattroff(m->gw, A_BOLD);
+ box(m->gw, 0, 0);
for (i = 1; i <= m->rows; i++) {
- wmove(m->gamewin, i, 1);
+ wmove(m->gw, i, 1);
for (j = 0; j < m->cols; j++)
- wprintw(m->gamewin, GRID_BOX);
+ waddstr(m->gw, GRID_BOX);
}
-
- wattron(m->gamewin, A_BOLD);
+ wattron(m->gw, A_BOLD);
for (i = 0, y = 1; i < m->rows; i++, y++)
for (j = 0, x = 2; j < m->cols; j++, x += 3)
- mvwaddch(m->gamewin, y, x, m->dispboard[i][j]);
-}
-
-static WINDOW *
-gamewininit(int rows, int cols)
-{
- WINDOW *gw;
- int wr, wc, wy, wx;
-
- wr = SCRSPACE_Y(rows);
- wc = SCRSPACE_X(cols);
- wy = CENTER(YMAX(stdscr), wr);
- wx = CENTER(XMAX(stdscr), wc);
- gw = newwin(wr, wc, wy, wx);
- wattron(gw, A_BOLD);
- box(gw, 0, 0);
- wattroff(gw, A_BOLD);
-
- return gw;
+ mvwaddch(m->gw, y, x, m->dispboard[i][j]);
+ wattroff(m->gw, A_BOLD);
}
static void
@@ -290,25 +291,24 @@ menuopts(void)
int w, h, wy, wx;
w = 36;
- h = 13;
- wy = CENTER(YMAX(stdscr), h);
- wx = CENTER(XMAX(stdscr), w);
+ h = 12;
+ wy = CENTER(getmaxy(stdscr), h);
+ wx = CENTER(getmaxx(stdscr), w);
opts = newwin(h, w, wy, wx);
box(opts, 0, 0);
/* fill menu */
mvwprintw(opts, 1, 1, OPT_QUIT);
- mvwprintw(opts, 2, 1, OPT_RESTART);
- mvwprintw(opts, 3, 1, OPT_MOVE_UP);
- mvwprintw(opts, 4, 1, OPT_MOVE_DOWN);
- mvwprintw(opts, 5, 1, OPT_MOVE_LEFT);
- mvwprintw(opts, 6, 1, OPT_MOVE_RIGHT);
- mvwprintw(opts, 7, 1, OPT_FLAG_CELL);
- mvwprintw(opts, 8, 1, OPT_DEFUSE);
- mvwprintw(opts, 9, 1, OPT_OPEN_CELL);
- mvwprintw(opts, 11, 1, MSG_QUIT_MENU);
-
- wgetch(opts);
+ mvwprintw(opts, 2, 1, OPT_MOVE_UP);
+ mvwprintw(opts, 3, 1, OPT_MOVE_DOWN);
+ mvwprintw(opts, 4, 1, OPT_MOVE_LEFT);
+ mvwprintw(opts, 5, 1, OPT_MOVE_RIGHT);
+ mvwprintw(opts, 6, 1, OPT_FLAG_CELL);
+ mvwprintw(opts, 7, 1, OPT_DEFUSE);
+ mvwprintw(opts, 8, 1, OPT_OPEN_CELL);
+ mvwprintw(opts, 10, 1, MSG_QUIT_MENU);
+
+ (void)wgetch(opts);
delwin(opts);
}
@@ -316,8 +316,8 @@ static void
endscreen(struct minecurses *m, int state)
{
curs_set(0);
- wclear(m->gamewin);
- wrefresh(m->gamewin);
+ wclear(m->gw);
+ wrefresh(m->gw);
attron(A_BOLD);
switch (state) {
case GAME_WON:
@@ -334,7 +334,6 @@ endscreen(struct minecurses *m, int state)
refresh();
attroff(A_BOLD);
getchar();
- // TODO: restart option
}
static void *
@@ -342,31 +341,27 @@ emalloc(size_t nb)
{
void *p;
- if ((p = malloc(nb)) == NULL) {
- fputs("malloc", stderr);
- exit(EXIT_FAILURE);
- }
+ if ((p = malloc(nb)) == NULL)
+ err(1, "malloc");
+
return p;
}
int
main(int argc, char *argv[])
{
- struct minecurses m;
+ struct minecurses *m;
- if (!initscr()) {
- fputs("initscr", stderr);
- return 1;
- }
- noecho();
- cbreak();
+ m = emalloc(sizeof(struct minecurses));
+ cursesinit(m);
- gamereset(&m);
- gamestart(&m);
+ gamereset(m);
+ gamestart(m);
- boardsdealloc(&m);
- delwin(m.gamewin);
+ boardsdealloc(m);
+ delwin(m->gw);
endwin();
+ free(m);
- return EXIT_SUCCESS;
+ return 0;
}
diff --git a/morse/Makefile b/morse/Makefile
@@ -0,0 +1,9 @@
+all:
+ c++ morse.cpp -I/usr/local/include -L/usr/local/lib -lSDL2 -lSDL2_mixer \
+ -o morse
+
+run:
+ ./morse
+
+clean:
+ rm -f morse
diff --git a/morse/morse.cpp b/morse/morse.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <map>
#include <string>
+
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
@@ -56,7 +57,7 @@ main(int argc, char *argv[])
{'/', "-..-." }, {' ', " / " }
};
- if (SDL_Init(SDL_INIT_AUDIO) == -1)
+ if (SDL_Init(SDL_INIT_AUDIO) < 0)
return -1;
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096);
dot = Mix_LoadWAV("dot.wav");
diff --git a/other/bounce.c b/other/bounce.c
@@ -1,4 +1,4 @@
-#include <ncurses.h>
+#include <curses.h>
#include <stdio.h>
#include <unistd.h>
@@ -12,9 +12,10 @@ struct ball {
};
static void
-init_curses(void)
+cursesinit(void)
{
- initscr();
+ if (!initscr())
+ exit(1);
cbreak();
noecho();
curs_set(0);
@@ -36,11 +37,10 @@ main(int argc, char *argv[])
{
struct ball b = {1, 2, 1, 1, 0, 0};
- init_curses();
+ cursesinit();
getmaxyx(stdscr, b.ymax, b.xmax);
- for (;;)
- {
+ for (;;) {
erase();
collision(&b);
mvaddch(b.y, b.x, 'O');
diff --git a/other/mandelbrot.c b/other/mandelbrot.c
@@ -1,6 +1,5 @@
#include <math.h>
#include <stdio.h>
-#include <stdlib.h>
#define IMGW 1920
#define IMGH 1080
@@ -10,50 +9,43 @@
#define MINI (-1.0)
#define MAXI 1.0
-#define TOREAL(x, imgw, minr, maxr) (x * ((maxr - minr) / imgw) + minr)
-#define TOIMGN(y, imgh, mini, maxi) (y * ((maxi - mini) / imgh) + mini)
+#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;
- double zr = 0.0, zi = 0.0;
- double tmp;
- for ( ; i < MAXN && (zr * zr + zi * zi) < 4.0; i++) {
+ 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[])
{
- FILE *fp;
- int x, y, n, r, g, b;
double cr, ci;
+ int x, y, n, r, g, b;
- if ((fp = fopen("mandelbrot_output.ppm", "w")) == NULL) {
- fprintf(stderr, "Cannot open output file. Exiting...");
- return EXIT_FAILURE;
- }
- fprintf(fp, "P3\n%d %d\n256\n", IMGW, IMGH);
-
+ printf("P3\n%d %d\n256\n", IMGW, IMGH);
for (y = 0; y < IMGH; y++) {
for (x = 0; x < IMGW; x++) {
- cr = TOREAL(x, IMGW, MINR, MAXR);
- ci = TOIMGN(y, IMGH, MINI, MAXI);
+ cr = TOREAL(x);
+ ci = TOIMGN(y);
n = fmandelbrot(cr, ci);
r = (n % 256);
g = ((int)(n * sinf(M_PI)) % 256);
b = ((n * 3) % 256);
- fprintf(fp, "%d %d %d ", r, g, b);
+ printf("%d %d %d ", r, g, b);
}
- fprintf(fp, "\n");
+ printf("\n");
}
- fclose(fp);
return 0;
}
diff --git a/other/nnc.cpp b/other/nnc.cpp
@@ -3,14 +3,14 @@
#include <cstdlib>
#include <ctime>
#include <vector>
-#include <ncurses.h>
+#include <curses.h>
-static constexpr int ymax()
+static int ymax()
{
return getmaxy(stdscr);
}
-static constexpr int xmax()
+static int xmax()
{
return getmaxx(stdscr);
}
@@ -22,7 +22,7 @@ struct Color {
Color(int x, int y, int color)
:x(x), y(y), color(color) {}
- constexpr int dist(const Color& c)
+ int dist(const Color& c)
{
int dx = this->x - c.x;
int dy = this->y - c.y;
@@ -47,9 +47,10 @@ struct White: public Color {
};
static void
-init_curses()
+cursesinit()
{
- initscr();
+ if (!initscr())
+ exit(1);
cbreak();
noecho();
curs_set(0);
@@ -137,11 +138,12 @@ dealloc(std::vector<T *>& vec)
int
main(int argc, char **argv)
{
- init_curses();
- std::srand(std::time(nullptr));
-
std::vector<Color *> points;
std::vector<White *> whites;
+
+ cursesinit();
+ std::srand(std::time(nullptr));
+
makepts(points);
makewts(whites);
diff --git a/other/snake.cpp b/other/snake.cpp
@@ -4,7 +4,7 @@
#include <list>
#include <thread>
-#include <ncurses.h>
+#include <curses.h>
#define XMAX (getmaxx(stdscr))
#define YMAX (getmaxy(stdscr))
@@ -65,6 +65,8 @@ Snake::update(int key)
x++;
body.push_front({body.front().x + 1, body.front().y});
break;
+ default:
+ return;
}
body.pop_back();
}
@@ -117,7 +119,8 @@ Food::draw()
static void
initcurses()
{
- initscr();
+ if (!initscr())
+ exit(1);
cbreak();
noecho();
curs_set(0);
diff --git a/shitcoin/Makefile b/shitcoin/Makefile
@@ -1,4 +1,5 @@
-# shitcoin - a cryptocurrency made as an experiment
+# See LICENSE file for copyright and license details.
+# shitcoin
.POSIX:
include config.mk
@@ -25,28 +26,28 @@ ${BIN}: ${OBJ}
${CC} -c ${CFLAGS} $<
dist: clean
- ${MKDIR} ${DIST}
- ${CP} -R config.mk shitcoin.c Makefile LICENSE README ${DIST}
- ${TAR} ${DIST}.tar ${DIST}
- ${GZIP} ${DIST}.tar
- ${RM_DIR} ${DIST}
+ mkdir -p ${DIST}
+ cp -R config.mk Makefile README shitcoin.c ${DIST}
+ tar -cf ${DIST}.tar ${DIST}
+ gzip ${DIST}.tar
+ rm -rf ${DIST}
run:
./${BIN}
install: all
- ${MKDIR} ${DESTDIR}${BIN_DIR} ${DESTDIR}${MAN_DIR}
- ${CP} ${BIN} ${BIN_DIR}
- ${CP} ${MAN1} ${DESTDIR}${MAN_DIR}
- sed "s/VERSION/${VERSION}/g" < ${MAN1} > ${DESTDIR}${MAN_DIR}/${MAN1}
- chmod 755 ${DESTDIR}${BIN_DIR}/${BIN}
- chmod 644 ${DESTDIR}${MAN_DIR}/${MAN1}
+ 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} ${DESTDIR}${BIN_DIR}/${BIN}
- ${RM} ${DESTDIR}${MAN_DIR}/${MAN1}
+ rm -f ${DESTDIR}${PREFIX}/bin/${BIN} \
+ ${DESTDIR}${MANPREFIX}/man1/${MAN1}
clean:
- ${RM} ${BIN} ${OBJ} ${DIST}.tar.gz
+ rm -f ${BIN} ${OBJ} ${DIST}.tar.gz *.core
.PHONY: all options clean dist install uninstall run
diff --git a/shitcoin/config.mk b/shitcoin/config.mk
@@ -1,13 +1,13 @@
+# See LICENSE file for copyright and license details.
# shitcoin version
-VERSION = 0
+VERSION = 0.1
# paths
PREFIX = /usr/local
-MAN_DIR = ${PREFIX}/share/man/man1
-BIN_DIR = ${PREFIX}/bin
+MANPREFIX = ${PREFIX}/share/man
# includes and libs
-INCS = -Iinclude
+INCS = -Iinclude
LIBS = -Llib -lmd
# flags
@@ -16,15 +16,5 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L \
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = ${LIBS}
-# utils
-CP = cp -f
-RM = rm -f
-RM_DIR = rm -rf
-MV = mv
-MKDIR = mkdir -p
-RM_DIR = rm -rf
-TAR = tar -cf
-GZIP = gzip
-
# compiler
CC = cc
diff --git a/shitcoin/shitcoin.c b/shitcoin/shitcoin.c
@@ -1,3 +1,4 @@
+#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -44,7 +45,6 @@ static long balance(const char *);
static void printchain(void);
static void cleanchain(void);
static void *emalloc(size_t);
-static void die(const char *, ...);
static struct blockchain *chain;
@@ -55,7 +55,7 @@ transaction(const char *from, const char *to, long amount)
chain->pending[chain->npending++] =
newblock(from, to, amount, NULL);
else
- fprintf(stderr, "transaction array is full\n");
+ warnx("transaction array is full");
}
static struct block *
@@ -128,7 +128,7 @@ minepending(const char *rewaddr)
if ((chain->blocks = realloc(chain->blocks,
sizeof(struct block *) * (chain->nblocks + chain->npending + 1))) == NULL)
- die("realloc:");
+ err(1, "realloc");
for (; i < chain->npending; i++) {
b = chain->pending[i];
@@ -234,29 +234,11 @@ emalloc(size_t nb)
void *p;
if ((p = malloc(nb)) == NULL)
- die("malloc:");
+ err(1, "malloc");
return p;
}
-static void
-die(const char *fmt, ...)
-{
- va_list args;
-
- va_start(args, fmt);
- vfprintf(stderr, fmt, args);
- va_end(args);
-
- if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
- fputc(' ', stderr);
- perror(NULL);
- } else
- fputc('\n', stderr);
-
- exit(EXIT_FAILURE);
-}
-
int
main(int argc, char *argv[])
{