Initial commit. After PURGING old/bad commits.
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*.o
|
||||
maze
|
||||
12
Makefile
Normal file
12
Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
CC=gcc
|
||||
CFLAGS=-O2 -lm
|
||||
BIN=maze
|
||||
|
||||
$(BIN): $(BIN).c
|
||||
$(CC) $(BIN).c -o $(BIN) $(CFLAGS)
|
||||
|
||||
clean: $(BIN)
|
||||
rm $(BIN)
|
||||
|
||||
run: $(BIN)
|
||||
./$(BIN) $(MAP)
|
||||
21
map.d/21x21.map
Normal file
21
map.d/21x21.map
Normal file
@@ -0,0 +1,21 @@
|
||||
111111111111111111111
|
||||
401000100000001000001
|
||||
101011111110101110111
|
||||
100000000000100010001
|
||||
111110111111111110111
|
||||
100010101000101010001
|
||||
101010101011101010101
|
||||
101010000010101000101
|
||||
101110111110101110101
|
||||
100010000000000000101
|
||||
111010111111111110111
|
||||
100000000010001000101
|
||||
111110111110101010101
|
||||
101010000000101010001
|
||||
101011101111111111101
|
||||
100000100010100000101
|
||||
101011111110101111101
|
||||
101010000000100000001
|
||||
101111101110101110111
|
||||
100000001000000010003
|
||||
111111111111111111111
|
||||
33
map.d/33x33.map
Normal file
33
map.d/33x33.map
Normal file
@@ -0,0 +1,33 @@
|
||||
111111111111111111111111111111111
|
||||
400000001000000010001000000000001
|
||||
111011111110111011101010101110111
|
||||
101000101010101010001010101000001
|
||||
101011101010101010111010111110111
|
||||
100000100000100000100010100000001
|
||||
111010111110111110101111111110111
|
||||
100010100010000010001010101000101
|
||||
101011101111111111101010101011101
|
||||
101010000000100000000010000000101
|
||||
101110111010111110101111101011101
|
||||
100000101010100000100010001000001
|
||||
111010101111101010111011111111111
|
||||
100010001000001010100000000000001
|
||||
101011101010111110101011111110101
|
||||
101010100010001000101000001000101
|
||||
101010111011101111111111111111111
|
||||
101000100010001010001000000000001
|
||||
111111111011111011101110111010111
|
||||
100000100010101010000000001010001
|
||||
101110101010101011111111111110111
|
||||
100010101000100000101010001000001
|
||||
111010111010111010101010111010111
|
||||
100010001010100010001000100010001
|
||||
111011111110111110111110101010111
|
||||
100000101000000010000010001010101
|
||||
101111101010111110111110111110101
|
||||
100010000010000000100000001000001
|
||||
101111111011111110111111101111101
|
||||
100010000000100000100010000000101
|
||||
101011101011101010101010101111101
|
||||
101000001010001010001000100000101
|
||||
111111111111111111111111111111131
|
||||
49
map.d/49x97.map
Normal file
49
map.d/49x97.map
Normal file
@@ -0,0 +1,49 @@
|
||||
4011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
|
||||
0010001000001010000010101000100000100000100000001010000010001000001000000010000010101000100000101
|
||||
1010111110111010111110101010101111101110111011111010101111101111101011111010111010101011111010101
|
||||
1000001000000010100010000010000010001000000000000010100010000000001000001000101010101000101010001
|
||||
1011101110111110101111111110101011111010101110111011101111111111101110111010101010101110101011101
|
||||
1000100000001010000010101000101010100010101010001000000000100000000010100010100000101000000010101
|
||||
1010111111101010111110101011111110101010111011101010111011101011111111101111101110101111111110101
|
||||
1010001010000000101010001000101000001010001000001010100000101000100000000000101000001010000000001
|
||||
1110111011111111101010111011101111111011111111101110101111111010111110111111101111111011101011101
|
||||
1000101000001000100000100010000000001010000000000010101010000010100010101010000010000000001000101
|
||||
1110101110101011111011101010111110111111111011101011111010101011101011101011101111101011111111101
|
||||
1010001000101010100000101000001000101010000000101000001010101000101010000010000010101010101010001
|
||||
1011101111101010101111111110111111101011101111111110111011101111111011101111111010111110101010111
|
||||
1000101010100000100010000000001000001000101000100000000010000000000000001010100010000010000000001
|
||||
1011101010111010111011111111101010101110101110101011111010111110101010111010101111101110111010101
|
||||
1000000000100010100000100000000010100000100000101000100000000010101010100010000000001000001010101
|
||||
1010111010101111101010111111101111111010111110111110111111101110101111101011111110111011111111101
|
||||
1010101010000010101010101010100000100010000000000010000000100010101010001000100000000000101010001
|
||||
1011101111101110111010101010101111111110111111101011111110101111111010111111101011111011101010111
|
||||
1010001010001000000010101000001010101010001000001010100010100010001000001000001000001000100010001
|
||||
1011101011111011111011101011101010101010111111111110101110111111101011101011101010111011111010101
|
||||
1010001000000010101000100010000010000010100010001010000010100000101010000010001010001000000010101
|
||||
1011101011101010101011101111101011111011101110111011101110101111101110111110111110111110111111111
|
||||
1000000010001010100010000010101010000000101010000000000010000000101010100010001010001000000000001
|
||||
1011111111111010111110101110101011101110101010111011111011101110101011101010111010111110111011111
|
||||
1010100000000000100010101010001010000010001000100010000000001010000010001010100000100000001010001
|
||||
1110101011111011101011101010111010111111111110111111101011101011111010101111111011111110111011101
|
||||
1000001000100000001010001010001000100000001000101000101010001000000000100010000000001000001000001
|
||||
1111111110111011101110101010111110111011111011101011101010101110111011111110101111101110111110101
|
||||
1000100000001000100010100010001010101010001000001000001010101000001000001000101000101010100000101
|
||||
1010101111111011101111101111111011101011101011101110111110111011111111101111111110111011111010111
|
||||
1010000000101010100000101000100000100000001010101000001000100000001000000000100010101000101010001
|
||||
1010111110101010111111101010101111101110111010111111111111111111111010101111101110101010101110111
|
||||
1010100000001010000000000010101000001000001000001000000000000010100010101010101000100010100010101
|
||||
1111111010101111111110111011101010101110111111101110101111101110101110101010101110111011101110101
|
||||
1010001010100010001000101000100010101010000000000010101010000000101010100010001000100010100000001
|
||||
1011101110101111101010101110101110101011101111111010111010111010111010111110101010111010101011101
|
||||
1000001010101000101010101010100010101000001010101010101010101010000010000010101010000010101010001
|
||||
1110101010111011101011101011111111111111111010101010101011101110101111111111101010111010101011101
|
||||
1000100010000000001000101000100000001000000000000000101010000000100000001000101010001000101000101
|
||||
1010101110111110101011101011101010111011111011101110101011111111101110111110101110111010111011111
|
||||
1010100010001000100010000010001010101010000000100010000010100000000010001000000000100010101000001
|
||||
1011111110111110111111111011101011101110111111111110111110101010101111101110111011111111101011111
|
||||
1000000000001000000000001000101010000010001000100010101010001010101000000010001000000000001010101
|
||||
1011111011101011111010101011111010111010111011101010101010111111111010111011101111111011101010101
|
||||
1000001010001000001010100000100000001010000000101010000010100010000010100000000010100010000010101
|
||||
1111101110101110101111111110111111101111101011111010101111111010111010101111111110101111111010101
|
||||
1000001000100010100010000000000000000000101000100000100000100000100010100000000000101000000000000
|
||||
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111103
|
||||
271
maze.c
Normal file
271
maze.c
Normal file
@@ -0,0 +1,271 @@
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <threads.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UP 3
|
||||
#define LEFT 1
|
||||
#define DOWN 2
|
||||
#define RIGHT 4
|
||||
|
||||
typedef struct MAPSTRUCT {
|
||||
size_t x_size;
|
||||
size_t y_size;
|
||||
size_t p_x_cord;
|
||||
size_t p_y_cord;
|
||||
size_t s_x_cord;
|
||||
size_t s_y_cord;
|
||||
size_t f_x_cord;
|
||||
size_t f_y_cord;
|
||||
uint8_t **pntr;
|
||||
} MAPSTRUCT;
|
||||
|
||||
typedef struct termios TERMSTATE;
|
||||
|
||||
typedef uint8_t DIRECTION;
|
||||
|
||||
TERMSTATE TSTATE;
|
||||
MAPSTRUCT *MAP;
|
||||
|
||||
bool QUIT = false;
|
||||
bool WIN = false;
|
||||
|
||||
size_t X_SIZE;
|
||||
size_t Y_SIZE;
|
||||
|
||||
void get_size(const char *path) {
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
char ch;
|
||||
size_t x_cnt = 0;
|
||||
size_t y_cnt = 0;
|
||||
bool cnt_x = true;
|
||||
while ((ch = getc(file)) != EOF) {
|
||||
if (ch == '\n') {
|
||||
++y_cnt;
|
||||
cnt_x = false;
|
||||
} else if (cnt_x) {
|
||||
++x_cnt;
|
||||
}
|
||||
}
|
||||
|
||||
X_SIZE = x_cnt;
|
||||
Y_SIZE = y_cnt;
|
||||
}
|
||||
|
||||
void move_cursor() {
|
||||
printf("\033[%zdD", Y_SIZE);
|
||||
printf("\033[%zdA", X_SIZE);
|
||||
}
|
||||
|
||||
void move(DIRECTION dir) {
|
||||
switch (dir) {
|
||||
case 1:
|
||||
if (MAP->p_x_cord != 0 && MAP->pntr[MAP->p_y_cord][MAP->p_x_cord - 1] != 1) {
|
||||
if (MAP->p_x_cord == MAP->s_x_cord && MAP->p_y_cord == MAP->s_y_cord) {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 2;
|
||||
} else {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 0;
|
||||
}
|
||||
--MAP->p_x_cord;
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 4;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (MAP->p_y_cord != MAP->y_size - 1 && MAP->pntr[MAP->p_y_cord + 1][MAP->p_x_cord] != 1) {
|
||||
if (MAP->p_x_cord == MAP->s_x_cord && MAP->p_y_cord == MAP->s_y_cord) {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 2;
|
||||
} else {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 0;
|
||||
}
|
||||
++MAP->p_y_cord;
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 4;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (MAP->p_y_cord != 0 && MAP->pntr[MAP->p_y_cord - 1][MAP->p_x_cord] != 1) {
|
||||
if (MAP->p_x_cord == MAP->s_x_cord && MAP->p_y_cord == MAP->s_y_cord) {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 2;
|
||||
} else {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 0;
|
||||
}
|
||||
--MAP->p_y_cord;
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 4;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
if (MAP->p_x_cord != MAP->x_size - 1 && MAP->pntr[MAP->p_y_cord][MAP->p_x_cord + 1] != 1) {
|
||||
if (MAP->p_x_cord == MAP->s_x_cord && MAP->p_y_cord == MAP->s_y_cord) {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 2;
|
||||
} else {
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 0;
|
||||
}
|
||||
++MAP->p_x_cord;
|
||||
MAP->pntr[MAP->p_y_cord][MAP->p_x_cord] = 4;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void free_map() {
|
||||
for (size_t y_cord = 0; y_cord < MAP->y_size; ++y_cord) {
|
||||
free(MAP->pntr[y_cord]);
|
||||
}
|
||||
free(MAP->pntr);
|
||||
free(MAP);
|
||||
}
|
||||
|
||||
void alloc_map() {
|
||||
MAP = (MAPSTRUCT *)malloc(sizeof(MAPSTRUCT));
|
||||
MAP->x_size = X_SIZE;
|
||||
MAP->y_size = Y_SIZE;
|
||||
MAP->pntr = (uint8_t **)malloc(MAP->y_size * sizeof(uint8_t *));
|
||||
for (size_t y_cord = 0; y_cord < MAP->y_size; ++y_cord) {
|
||||
MAP->pntr[y_cord] = (uint8_t *)malloc(MAP->x_size * sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
|
||||
void load_map(const char *path) {
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
char cur;
|
||||
size_t y_cord = 0, x_cord = 0;
|
||||
while (EOF != (cur = getc(file))) {
|
||||
if ('\n' == cur) {
|
||||
x_cord = 0;
|
||||
++y_cord;
|
||||
continue;
|
||||
} else if ('4' == cur) {
|
||||
MAP->p_x_cord = x_cord;
|
||||
MAP->p_y_cord = y_cord;
|
||||
MAP->s_x_cord = x_cord;
|
||||
MAP->s_y_cord = y_cord;
|
||||
} else if ('3' == cur) {
|
||||
MAP->f_x_cord = x_cord;
|
||||
MAP->f_y_cord = y_cord;
|
||||
}
|
||||
MAP->pntr[y_cord][x_cord] = (uint8_t)(cur - (char)'0');
|
||||
++x_cord;
|
||||
}
|
||||
}
|
||||
|
||||
void print_map() {
|
||||
const char sprites[] = " XO#@";
|
||||
for (size_t y_cord = 0; y_cord < MAP->y_size; ++y_cord) {
|
||||
for (size_t x_cord = 0; x_cord < MAP->x_size; ++x_cord) {
|
||||
printf("%lc ", sprites[MAP->pntr[y_cord][x_cord]]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void set_term_def();
|
||||
void set_term_raw();
|
||||
|
||||
void clear() {
|
||||
for (size_t y_cord = 0; y_cord < MAP->y_size; ++y_cord) {
|
||||
for (size_t x_cord = 0; x_cord < MAP->x_size; ++x_cord) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void check_win() {
|
||||
if (MAP->p_y_cord == MAP->f_y_cord && MAP->p_x_cord == MAP->f_x_cord) {
|
||||
WIN = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads keyboard input
|
||||
void *capture() {
|
||||
set_term_raw(); // local function: Enable Raw Mode
|
||||
char ch;
|
||||
while ((ch = getchar()) != 'q') {
|
||||
switch (ch) {
|
||||
case 'w':
|
||||
move(UP);
|
||||
break;
|
||||
case 'a':
|
||||
move(LEFT);
|
||||
break;
|
||||
case 's':
|
||||
move(DOWN);
|
||||
break;
|
||||
case 'd':
|
||||
move(RIGHT);
|
||||
break;
|
||||
}
|
||||
|
||||
check_win();
|
||||
if (WIN) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QUIT = 1;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void *print() {
|
||||
while (!QUIT) { // When ESC is not pressed
|
||||
print_map();
|
||||
usleep(41667);
|
||||
move_cursor();
|
||||
}
|
||||
|
||||
if (WIN) {
|
||||
clear();
|
||||
move_cursor();
|
||||
printf("Congratulations! You have won the game.\n");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void set_term_raw() {
|
||||
struct termios raw;
|
||||
tcgetattr(STDIN_FILENO, &raw); // Save the state of the terminal to struct raw
|
||||
// STDIN_FILENO is from <stdlib.h>
|
||||
// tcgetattr() from <termios.h>
|
||||
tcgetattr(STDIN_FILENO, &TSTATE);
|
||||
atexit(&set_term_def); // Revert to canonical mode when exiting the program
|
||||
// atext() from <stdlib.h>
|
||||
raw.c_lflag &= ~(ECHO | ICANON); // Turn off canonical mode
|
||||
// Turn off ECHO mode so that keyboard is not
|
||||
// printing to terminal
|
||||
// ICANON and ECHO is bitflag. ~ is binary NOT operator
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH,
|
||||
&raw); // Set the terminal to be in raw mode
|
||||
// tcsetattr() from <termios.h>
|
||||
}
|
||||
|
||||
void set_term_def() {
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH,
|
||||
&TSTATE); // Set terminal to TSTATE state
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc > 1 && argc < 3) {
|
||||
get_size(argv[1]);
|
||||
alloc_map();
|
||||
load_map(argv[1]);
|
||||
|
||||
// Start Multithreading
|
||||
pthread_t id_print, id_read;
|
||||
|
||||
pthread_create(&id_print, NULL, print, NULL);
|
||||
pthread_create(&id_read, NULL, capture, NULL);
|
||||
|
||||
pthread_join(id_print, NULL);
|
||||
pthread_join(id_read, NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user