Page 1 of 1

Unfinished ca editor

Posted: November 10th, 2022, 4:17 am
by pzq_alex

Code: Select all

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <curses.h>

struct cell
{
    int state;
};
char statechar[3] = {' ', 'o'};

const size_t N = 20;
cell patt[N][N];

inline void upd()
{
    int x, y; getyx(stdscr, y, x);
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            move(i, j);
            addch(statechar[patt[i][j].state]);
        }
    refresh();
    move(y, x);
}

inline cell nxt(cell a, cell b, cell c, cell d, cell e, cell f, cell g, cell h, cell i)
{
    int cnt = int(a.state) + b.state + c.state + d.state + e.state + f.state + g.state + h.state + i.state;
    if (e.state) return (cell){cnt == 3 || cnt == 4};
    else return (cell){cnt == 3};
}

int main()
{
    initscr(); cbreak(); noecho();
    nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE);

    int x = 0, y = 0; int gen = 0;
    bool repl = false;
    while (true)
    {
        move(y, x);
        upd();

        int c = getch();
        if (c == ERR)
            continue;

        if (repl)
        {
            if (c == 'b' || c == '.' || isspace(c))
                patt[y][x].state = false;
            else
                patt[y][x].state = true;
            repl = false;
            continue;
        }

        if (c == 'q') break;
        if (c == 'h') if (x > 0) x--;
        if (c == 'l') if (x < N - 1) x++;
        if (c == 'j') if (y < N - 1) y++;
        if (c == 'k') if (y > 0) y--;
        if (c == 'r') repl = true;
    }

    nocbreak(); echo();
    endwin();
}

Re: Unfinished ca editor

Posted: November 10th, 2022, 11:39 pm
by Citation needed
The computer programs should be posted here. By the way, if you really wish to talk to ColorfulGalaxy without me as a medium, please beware that he has gone to another website. He is still a Life enthusiast at the moment.