Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-pro· GoogleHugging Face LMArena Human Preference 140K

Write a C command line program that generate moving ASCII art fractal pattern that…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-2025-04-14’s answer.
  1. User

    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.

  2. Gemini Assistant

    Of course. Here is a complete, self-contained C command-line program that generates a moving ASCII art Julia set fractal. It responds to user input for navigation and control, and it's written to be cross-platform (working on both Linux/macOS and Windows).

    Features

    • Animated Julia Set: The fractal's parameters change over time, creating a mesmerizing "moving" effect.
    • Interactive Controls:
      • w, a, s, d: Pan the view Up, Left, Down, and Right.
      • + or z: Zoom in.
      • - or x: Zoom out.
      • p: Pause/Resume the animation.
      • r: Reset the view and animation.
      • c: Cycle through different ASCII character sets ("color palettes").
      • q: Quit the program.
    • Cross-Platform: Uses preprocessor directives to handle terminal control and non-blocking input for both Unix-like systems (termios.h) and Windows (conio.h).
    • Self-Contained: Relies only on standard C libraries included by default with C compilers.
    • Flicker-Free: Renders each frame to an in-memory buffer before printing it to the console, preventing flickering.

    The Code (fractal.c)

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h>
    
    // Platform-specific includes and functions
    #ifdef _WIN32
    #include <windows.h>
    #include <conio.h>
    
    void clearScreen() {
        // A more robust way to clear the screen on Windows
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD coord = {0, 0};
        DWORD count;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo(hStdOut, &csbi);
        FillConsoleOutputCharacter(hStdOut, (TCHAR)' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
        SetConsoleCursorPosition(hStdOut, coord);
    }
    
    void getTerminalSize(int* width, int* height) {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
        *width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
        *height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    }
    
    // Windows doesn't need raw mode for _kbhit() and _getch()
    void setupTerminal() {}
    void restoreTerminal() {}
    
    #else
    #include <unistd.h>
    #include <termios.h>
    #include <sys/ioctl.h>
    
    struct termios orig_termios;
    
    void clearScreen() {
        // ANSI escape codes to clear screen and move cursor to top-left
        printf("\033[H\033[J");
    }
    
    void getTerminalSize(int* width, int* height) {
        struct winsize ws;
        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
            *width = 80;
            *height = 24;
        } else {
            *width = ws.ws_col;
            *height = ws.ws_row;
        }
    }
    
    void restoreTerminal() {
        tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
    }
    
    void setupTerminal() {
        tcgetattr(STDIN_FILENO, &orig_termios);
        atexit(restoreTerminal); // Ensure terminal is restored on exit
        struct termios raw = orig_termios;
        raw.c_lflag &= ~(ECHO | ICANON);
        tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
    }
    
    #endif
    
    // --- Global State and Constants ---
    
    #define MAX_ITERATIONS 100
    #define ASPECT_RATIO 2.0 // Characters are taller than they are wide
    
    // A struct to hold the state of our fractal view
    typedef struct {
        double centerX, centerY;
        double zoom;
        double time;
        int is_paused;
        double julia_c_real;
        double julia_c_imag;
        int charset_index;
    } FractalState;
    
    const char* CHARSETS[] = {
        " .:-=+*#%@",
        " .,:;i1tfLCG08@",
        "@%#*+=-:.",
        "█▓▒░ "
    };
    const int NUM_CHARSETS = sizeof(CHARSETS) / sizeof(char*);
    
    // --- Fractal Logic ---
    
    int calculate_julia(double z_real, double z_imag, double c_real, double c_imag) {
        int n;
        for (n = 0; n < MAX_ITERATIONS; n++) {
            if (z_real * z_real + z_imag * z_imag > 4.0) {
                return n;
            }
            double next_z_real = z_real * z_real - z_imag * z_imag + c_real;
            double next_z_imag = 2 * z_real * z_imag + c_imag;
            z_real = next_z_real;
            z_imag = next_z_imag;
        }
        return MAX_ITERATIONS;
    }
    
    // --- Main Program Logic ---
    
    void init_state(FractalState* state) {
        state->centerX = 0.0;
        state->centerY = 0.0;
        state->zoom = 1.0;
        state->time = 0.0;
        state->is_paused = 0;
        state->charset_index = 0;
    }
    
    void update_state(FractalState* state) {
        if (state->is_paused) return;
    
        state->time += 0.02;
        // These parameters create a nice looping animation for the Julia set
        state->julia_c_real = 0.7885 * cos(state->time);
        state->julia_c_imag = 0.7885 * sin(state->time);
    }
    
    void render(FractalState* state) {
        int width, height;
        getTerminalSize(&width, &height);
        height--; // Leave space for the info line
    
        // Create a buffer to avoid flicker
        char* buffer = (char*)malloc((width + 1) * height);
        if (!buffer) return;
    
        const char* charset = CHARSETS[state->charset_index];
        int charset_len = strlen(charset);
    
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Map screen coordinates to complex plane
                double real = state->centerX + (x - width / 2.0) * (4.0 / (width * state->zoom));
                double imag = state->centerY + (y - height / 2.0) * (4.0 / (height * state->zoom)) / ASPECT_RATIO;
    
                int iterations = calculate_julia(real, imag, state->julia_c_real, state->julia_c_imag);
    
                char pixel;
                if (iterations == MAX_ITERATIONS) {
                    pixel = ' ';
                } else {
                    pixel = charset[iterations % charset_len];
                }
                buffer[y * (width + 1) + x] = pixel;
            }
            buffer[y * (width + 1) + width] = '\n';
        }
        buffer[(width + 1) * height - 1] = '\0';
    
        clearScreen();
        puts(buffer);
        free(buffer);
    
        // Print info/help line
        printf("Pos: (%.2f, %.2f) Zoom: %.2fx | [WASD] Pan [+/- or z/x] Zoom [p] Pause [r] Reset [c] Charset [q] Quit",
               state->centerX, state->centerY, state->zoom);
        fflush(stdout);
    }
    
    void handle_input(FractalState* state, int* running) {
    #ifdef _WIN32
        if (_kbhit()) {
            char c = _getch();
    #else
        // A simple non-blocking read for Unix-like systems
        char c = '\0';
        struct timeval tv = {0L, 0L};
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(0, &fds);
        if (select(1, &fds, NULL, NULL, &tv) > 0) {
            read(0, &c, 1);
        }
        if (c != '\0') {
    #endif
            double pan_amount = 0.5 / state->zoom;
            switch (c) {
                case 'q': *running = 0; break;
                case 'w': state->centerY -= pan_amount; break;
                case 's': state->centerY += pan_amount; break;
                case 'a': state->centerX -= pan_amount; break;
                case 'd': state->centerX += pan_amount; break;
                case '+': case 'z': state->zoom *= 1.2; break;
                case '-': case 'x': state->zoom /= 1.2; break;
                case 'p': state->is_paused = !state->is_paused; break;
                case 'r': init_state(state); break;
                case 'c': state->charset_index = (state->charset_index + 1) % NUM_CHARSETS; break;
            }
        }
    }
    
    int main() {
        FractalState state;
        init_state(&state);
        
        setupTerminal();
    
        int running = 1;
        while (running) {
            handle_input(&state, &running);
            update_state(&state);
            render(&state);
    
            // Frame rate control
    #ifdef _WIN32
            Sleep(16); // ~60 FPS
    #else
            usleep(16000); // ~60 FPS
    #endif
        }
        
        // restoreTerminal is called by atexit on Unix, but we call it explicitly for neatness
        restoreTerminal();
        clearScreen(); // Clean up the screen on exit
    
        return 0;
    }
    

    How to Compile and Run

    On Linux or macOS

    1. Save the code as fractal.c.
    2. Open your terminal.
    3. Compile the program using gcc. The -lm flag is crucial for linking the math library.
      gcc fractal.c -o fractal -lm
      
    4. Run the executable:
      ./fractal
      

    On Windows (with a C compiler like MinGW-w64)

    1. Save the code as fractal.c.
    2. Open a command prompt or PowerShell where your compiler (like gcc from MinGW) is in the PATH.
    3. Compile the program. The math library is usually linked by default, but adding -lm doesn't hurt.
      gcc fractal.c -o fractal.exe
      
    4. Run the executable:
      .\fractal.exe
      

    You will see the animated fractal fill your terminal window. You can then use the keys described above to interact with it. Enjoy the show

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.