-
-
Save seeschloss/5466dadee068c07548e84173fce8ef86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <termios.h> | |
#define WIDTH 80 | |
#define HEIGHT 25 | |
const char WALL = '#'; | |
const char SPIDER = 'O'; | |
const char EMPTY = '.'; | |
char maze[HEIGHT][WIDTH]; | |
int getch() { | |
struct termios oldt, newt; | |
int ch; | |
tcgetattr(STDIN_FILENO, &oldt); | |
newt = oldt; | |
newt.c_lflag &= ~(ICANON | ECHO); | |
tcsetattr(STDIN_FILENO, TCSANOW, &newt); | |
ch = getchar(); | |
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); | |
return ch; | |
} | |
void initMaze() { | |
int i, j; | |
// Fill the maze with walls | |
for (i = 0; i < HEIGHT; i++) { | |
for (j = 0; j < WIDTH; j++) { | |
maze[i][j] = WALL; | |
} | |
} | |
// Generate a random maze | |
srand(time(NULL)); | |
for (i = 1; i < HEIGHT - 1; i += 2) { | |
for (j = 1; j < WIDTH - 1; j += 2) { | |
maze[i][j] = EMPTY; | |
if (rand() % 2) { | |
maze[i - 1][j] = EMPTY; | |
} else { | |
maze[i][j - 1] = EMPTY; | |
} | |
} | |
} | |
// Place the spider in a random location | |
i = rand() % HEIGHT; | |
j = rand() % WIDTH; | |
maze[i][j] = SPIDER; | |
} | |
void printMaze() { | |
int i, j; | |
// Print the maze | |
for (i = 0; i < HEIGHT; i++) { | |
for (j = 0; j < WIDTH; j++) { | |
putchar(maze[i][j]); | |
} | |
putchar('\n'); | |
} | |
} | |
int isInBounds(int x, int y) { | |
return x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT; | |
} | |
int isWall(int x, int y) { | |
return maze[y][x] == WALL; | |
} | |
void moveSpider(int dx, int dy) { | |
int x, y; | |
// Find the current position of the spider | |
for (y = 0; y < HEIGHT; y++) { | |
for (x = 0; x < WIDTH; x++) { | |
if (maze[y][x] == SPIDER) { | |
goto found; | |
} | |
} | |
} | |
// If the spider is not found, do nothing | |
return; | |
found: | |
// Clear the old position of the spider | |
maze[y][x] = EMPTY; | |
// Move the spider | |
x += dx; | |
y += dy; | |
// If the new position is out of bounds or a wall, do not move the spider | |
if (!isInBounds(x, y) || isWall(x, y)) { | |
x -= dx; | |
y -= dy; | |
} | |
// Place the spider in the new position | |
maze[y][x] = SPIDER; | |
} | |
int main() { | |
char ch; | |
// Initialize the maze | |
initMaze(); | |
// Loop until the user quits | |
while (1) { | |
// Print the maze | |
printMaze(); | |
// Read a character from the keyboard | |
ch = getch(); | |
// Quit if the user presses 'q' | |
if (ch == 'q') { | |
break; | |
} | |
// Move the spider based on the key pressed | |
switch (ch) { | |
case 'w': | |
moveSpider(0, -1); | |
break; | |
case 'a': | |
moveSpider(-1, 0); | |
break; | |
case 's': | |
moveSpider(0, 1); | |
break; | |
case 'd': | |
moveSpider(1, 0); | |
break; | |
default: | |
break; | |
} | |
// Clear the screen | |
printf("\033[H\033[J"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment