/media/sda-magnetic/david/Dok-15-2023-11-27/informatik/AUTOMAT-GENERATOR-2024-01-28/automat4.c


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define EMPTY_Z -1
#define UNINIT_Z -2

int main (void) {
    time_t t;
    int x0line, x1line, x2line, x3line;
    int nfollowed;
    int z0reached, z1reached, z2reached, z3reached;
    int i, j;
    int z [4][4];
    int k, l;
    int v;
    int nf;


    srand ((unsigned)time (&t));

    z0reached = 0;
    z1reached = 0;
    z2reached = 0;
    z3reached = 0;

    for (i = 0;  i < 4;  i++) {
        for (j = 0;  j < 4;  j++)
            z [i][j] = EMPTY_Z;
    }

    nfollowed = 0;
    for (i = 0;  i < 4;  i++) {
            x0line = rand () % 2;
            x1line = rand () % 2;
            x2line = rand () % 2;
            x3line = rand () % 2;
            nfollowed += x0line + x1line + x2line + x3line;
            if (x0line == 1)
                z [i][0] = UNINIT_Z;
            if (x1line == 1)
                z [i][1] = UNINIT_Z;
            if (x2line == 1)
                z [i][2] = UNINIT_Z;
            if (x3line == 1)
                z [i][3] = UNINIT_Z;
            if ((x0line + x1line + x2line + x3line) == 0) {
                z [i][0] = UNINIT_Z;
                nfollowed += 1;
            }
    }
    z0reached = 0;
    z1reached = 0;
    z2reached = 0;
    z3reached = 0;
    nf = nfollowed;
    for (i = 0, k = 0, l = 0;  i < nf;  i++) {
        v = rand () % 4;
        if (v == 0)
            z0reached = 1;
        if (v == 1)
            z1reached = 1;
        if (v == 2)
            z2reached = 1;
        if (v == 3)
            z3reached = 1;
        while ((k < 4) && (l < 4) && (z [k][l] != UNINIT_Z)) {
            while ((l < 4) && (z [k][l] != UNINIT_Z))
                l++;
            if (z [k][l] != UNINIT_Z) {
                l = 0;
                k++;
            }
        }
        nfollowed--;

        if (nfollowed == (z0reached + z1reached + z2reached + z3reached)) {
            if (z0reached == 0) {
                v = 0;
                z0reached = 1;
            }
            else if (z1reached == 0) {
                v = 1;
                z1reached = 1;
            }
            else if (z2reached == 0) {
                v = 2;
                z2reached = 1;
            }
            else if (z3reached == 0) {
                v = 3;
                z3reached = 1;
            }

        }
        z [k][l] = v;
        k += (l/4);
        l = (l+1) % 4;
    }
    printf ("Zustand\tEingabe\tAusgabe\tFolgezustand\n");

    for (i = 0;  i < 4;  i++) {
            if (z [i][0] != EMPTY_Z)
                printf ("%i\t\t00\t\t%i\t\t%i\n", i, rand () % 2, z [i][0]);
            if (z [i][1] != EMPTY_Z)
                printf ("%i\t\t01\t\t%i\t\t%i\n", i, rand () % 2, z [i][1]);
            if (z [i][2] != EMPTY_Z)
                printf ("%i\t\t10\t\t%i\t\t%i\n", i, rand () % 2, z [i][2]);
            if (z [i][3] != EMPTY_Z)
                printf ("%i\t\t11\t\t%i\t\t%i\n", i, rand () % 2, z [i][3]);

    }


return 0;
}