aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--drunkard.c77
-rw-r--r--main.c122
3 files changed, 203 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..da8a861
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,4 @@
+all: drunkard main
+
+main: main.c
+ $(CC) main.c -lcurses -o main
diff --git a/drunkard.c b/drunkard.c
new file mode 100644
index 0000000..2427249
--- /dev/null
+++ b/drunkard.c
@@ -0,0 +1,77 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define HEIGHT 50
+#define WIDTH 50
+#define MAX_STEPS 50
+#define NUM_WALKERS 100
+
+enum direction {
+ UP,
+ LEFT,
+ RIGHT,
+ DOWN,
+ NUM_DIRS,
+};
+
+struct point {
+ int x;
+ int y;
+};
+
+int main(void)
+{
+ bool map[HEIGHT][WIDTH];
+ for (int i = 0; i < HEIGHT; ++i) {
+ for (int j = 0; j < WIDTH; ++j) {
+ map[i][j] = false;
+ }
+ }
+
+ int start_x = WIDTH / 2;
+ int start_y = HEIGHT / 2;
+
+ int open_tiles = 1;
+ struct point open[HEIGHT * WIDTH];
+ struct point p = {.x = start_x, .y = start_y};
+ open[0] = p;
+
+ srand(time(NULL));
+
+ for (int i = 0; i < NUM_WALKERS; ++i) {
+ struct point curr_point = open[rand() % open_tiles];
+ int x_pos = curr_point.x;
+ int y_pos = curr_point.y;
+
+ for (int j = 0; j < MAX_STEPS && x_pos < WIDTH && x_pos >= 0 &&
+ y_pos < HEIGHT && y_pos >= 0;
+ ++j) {
+ if (!map[y_pos][x_pos]) {
+ map[y_pos][x_pos] = true;
+ struct point p = {.x = x_pos, .y = y_pos};
+ open[open_tiles++] = p;
+ }
+ enum direction dir = rand() % NUM_DIRS;
+ switch (dir) {
+ case UP : --y_pos; break;
+ case LEFT : --x_pos; break;
+ case RIGHT : ++x_pos; break;
+ case DOWN : ++y_pos; break;
+ default : exit(EXIT_FAILURE); // should not occur
+ }
+ }
+ }
+
+ for (int i = 0; i < HEIGHT; ++i) {
+ for (int j = 0; j < WIDTH; ++j) {
+ if (map[i][j]) {
+ printf("#");
+ } else {
+ printf(".");
+ }
+ }
+ printf("\n");
+ }
+}
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..6f646ec
--- /dev/null
+++ b/main.c
@@ -0,0 +1,122 @@
+#include <curses.h>
+#include <stdlib.h>
+
+#define MAIN_PANEL_WIDTH 100
+#define MAIN_PANEL_HEIGHT 41
+#define INSTRUCTION_PANEL_WIDTH 32
+#define INSTRUCTION_PANEL_HEIGHT 43
+#define MESSAGE_PANEL_WIDTH 100
+#define MESSAGE_PANEL_HEIGHT 3
+
+WINDOW *create_newwin(int height, int width, int starty, int startx)
+{
+ WINDOW *local_win = newwin(height, width, starty, startx);
+ box(local_win, 0, 0);
+ wrefresh(local_win);
+
+ return local_win;
+}
+
+void destroy_win(WINDOW *local_win)
+{
+ wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
+ wrefresh(local_win);
+ delwin(local_win);
+}
+
+int main(void)
+{
+ initscr(); // initialize curses
+
+ // exit on unsupported consoles
+ if (LINES < INSTRUCTION_PANEL_HEIGHT ||
+ COLS < MAIN_PANEL_WIDTH + INSTRUCTION_PANEL_WIDTH || !has_colors()) {
+ endwin();
+ fprintf(
+ stderr,
+ "a color terminal is required with at least %dx%d characters\n",
+ INSTRUCTION_PANEL_WIDTH + MAIN_PANEL_WIDTH, INSTRUCTION_PANEL_HEIGHT
+ );
+ exit(1);
+ }
+
+ // configure curses if startup was successful
+ raw(); // disable line buffering
+ keypad(stdscr, TRUE); // enable reading function keys
+ noecho(); // don't print input
+ curs_set(0); // disable the cursor
+ start_color(); // enable colours
+
+ int starty = 12;
+ int startx = 40;
+ init_pair(1, COLOR_WHITE, COLOR_BLACK);
+ wattron(stdscr, COLOR_PAIR(1));
+ refresh();
+
+ // create the windows
+ WINDOW *inst = create_newwin(
+ INSTRUCTION_PANEL_HEIGHT, INSTRUCTION_PANEL_WIDTH,
+ (LINES - INSTRUCTION_PANEL_HEIGHT) / 2,
+ (COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH) / 2 +
+ MAIN_PANEL_WIDTH - 1
+ );
+ WINDOW *msgs = create_newwin(
+ MESSAGE_PANEL_HEIGHT, MESSAGE_PANEL_WIDTH,
+ (LINES - MAIN_PANEL_HEIGHT - MESSAGE_PANEL_HEIGHT) / 2 +
+ MAIN_PANEL_HEIGHT,
+ (COLS - MESSAGE_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH) / 2
+ );
+ WINDOW *main_win = create_newwin(
+ MAIN_PANEL_HEIGHT, MAIN_PANEL_WIDTH,
+ (LINES - MAIN_PANEL_HEIGHT - MESSAGE_PANEL_HEIGHT) / 2 + 1,
+ (COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH) / 2
+ );
+
+ mvwprintw(inst, 1, 2, "h - move left");
+ mvwprintw(inst, 2, 2, "j - move down");
+ mvwprintw(inst, 3, 2, "k - move up");
+ mvwprintw(inst, 4, 2, "l - move right");
+ wrefresh(inst);
+
+ mvwprintw(msgs, 1, 1, "TODO: put messages here");
+ wrefresh(msgs);
+
+ wattron(main_win, COLOR_PAIR(1));
+ mvwaddch(main_win, starty, startx, '@');
+ wrefresh(main_win);
+
+ int ch;
+ while ((ch = getch()) != KEY_F(1)) {
+ switch (ch) {
+ case 'k' :
+ if (starty > 1) {
+ mvwaddch(main_win, starty, startx, ' ');
+ mvwaddch(main_win, --starty, startx, '@');
+ }
+ break;
+ case 'j' :
+ if (starty < MAIN_PANEL_HEIGHT - 2) {
+ mvwaddch(main_win, starty, startx, ' ');
+ mvwaddch(main_win, ++starty, startx, '@');
+ }
+ break;
+ case 'h' :
+ if (startx > 1) {
+ mvwaddch(main_win, starty, startx, ' ');
+ mvwaddch(main_win, starty, --startx, '@');
+ }
+ break;
+ case 'l' :
+ if (startx < MAIN_PANEL_WIDTH - 2) {
+ mvwaddch(main_win, starty, startx, ' ');
+ mvwaddch(main_win, starty, ++startx, '@');
+ }
+ break;
+ }
+ wrefresh(main_win);
+ }
+
+ endwin();
+
+ return 0;
+}