Write a C command line program that generate moving ASCII art fractal pattern that responds to user input. The implementation should be self-contained apart from standard C functions.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a self-contained C program that generates a moving ASCII fractal pattern (the Sierpinski triangle) in the terminal. The pattern "moves" (shifts) in response to user input:
WASD keys move the fractal up, down, left, and right.
Q quits the program.
This program uses only standard C libraries. It uses ANSI escape codes for clearing the screen and moving the cursor, and simple non-blocking input for interactivity.
Note:
This code is for Unix-like systems (Linux, macOS) due to the use of termios for non-blocking input.
For Windows, the input handling would need to be adapted.
c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#define WIDTH 80
#define HEIGHT 24
#define FRACTAL_DEPTH 5
// Terminal input setup
struct termios orig_termios;
void reset_terminal_mode() {
tcsetattr(0, TCSANOW, &orig_termios);
}
void set_conio_terminal_mode() {
struct termios new_termios;
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
atexit(reset_terminal_mode);
new_termios.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &new_termios);
}
int kbhit() {
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
int getch() {
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) return 0;
else return c;
}
// Clear screen
void clear_screen() {
printf("\033[2J\033[H");
}
// Draw Sierpinski triangle
void draw_sierpinski(char canvas[HEIGHT][WIDTH], int x, int y, int size) {
if (size == 1) {
if (y >= 0 && y < HEIGHT && x >= 0 && x < WIDTH)
canvas[y][x] = '*';
} else {
int newSize = size / 2;
draw_sierpinski(canvas, x, y, newSize);
draw_sierpinski(canvas, x - newSize, y + newSize, newSize);
draw_sierpinski(canvas, x + newSize, y + newSize, newSize);
}
}
void print_canvas(char canvas[HEIGHT][WIDTH]) {
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
putchar(canvas[i][j]);
}
putchar('\n');
}
}
int main() {
set_conio_terminal_mode();
int offset_x = WIDTH / 2;
int offset_y = 0;
int running = 1;
while (running) {
char canvas[HEIGHT][WIDTH];
// Fill canvas with spaces
for (int i = 0; i < HEIGHT; ++i)
for (int j = 0; j < WIDTH; ++j)
canvas[i][j] = ' ';
// Draw fractal
int size = 1 << (FRACTAL_DEPTH - 1);
draw_sierpinski(canvas, offset_x, offset_y, size);
clear_screen();
print_canvas(canvas);
// Input handling
usleep(50000); // 50ms delay for animation
if (kbhit()) {
int c = getch();
if (c == 'q' || c == 'Q') running = 0;
else if (c == 'w' || c == 'W') offset_y = (offset_y > 0) ? offset_y - 1 : 0;
else if (c == 's' || c == 'S') offset_y = (offset_y < HEIGHT - size * 2) ? offset_y + 1 : offset_y;
else if (c == 'a' || c == 'A') offset_x = (offset_x > size) ? offset_x - 1 : offset_x;
else if (c == 'd' || c == 'D') offset_x = (offset_x < WIDTH - size) ? offset_x + 1 : offset_x;
}
}
reset_terminal_mode();
clear_screen();
printf("Exited.\n");
return 0;
}
How to compile and run:
sh
gcc -o fractal fractal.c
./fractal
Controls:
W - Move up
S - Move down
A - Move left
D - Move right
Q - Quit
Let me know if you want a Windows version or a different fractal!