/***********************************************************

        LURI/C V.1.0
                (C)001 for ASPII 3  March 1984
                (C)001 in EMUZETTO  March 1987
                Public domain source for BDS C
                                    March 1988
                This program is in the public domain.
                You may use this program for any purpose
                                   except evil.

************************************************************/

#include        bdscio.h
#define         TR      1
#define         MBTR    2
#define         FS      3
#define         MBFS    4
#define         FUTEI   5

char    op1[30][5], op2[30][5],
        text1[80],
        text2[80],
        text3[80],
        maintext[100][40],
        opecode;

int     point,
        la, lb,
        addr[100],
        tfflag[100],
        defadr[100],
        lastpt,
        upadr[200],
        ope[200],
        downadr[200],
        textpoint,
        endpoint,
        ans,
        si,
        sj,
        siadr[100],
        sitff[100],
        sjadr[100],
        sjtff[100];



main()

{

        int     i, j, k;

        endpoint = maintext;
        textpoint = 0;
        lastpt = 0;

        printf("\n\n");

        printf("***** LURI Ver.1.0 *****\n");
        printf("---- Logical Unified Rank Interigence ----\n\n");
        printf("     (C) 001 in EMUZETTO  March 1987 \n");
        printf("      Public domain source March 1988 \n\n");

        printf("  あなたはこれから人工ちのうLURIと話しが出来ます.\n\n");

        printf("  LURIは,<・・・は,・・・です.>などの簡単な\n");
        printf("  言葉しか理解出来ません.これに不満な人は,そーすプ\n");
        printf("  ログラムをあげますのでがんばって下さい.それでは,\n");
        printf("  あなたの退屈な時間つぶしのお相手をLURIがお供し\n");
        printf("  ます.\n\n");

        printf("  こんにちは! わたしルウリイです.\n\n");

        opeinit();
        while(1) {
        printf("> ");
        scanf("%s", text1);
        la = -1;
        lb = -1;

        if ((i = index(text1, "は,")) != ERROR) {
                la = 1;
                for (j = 0; j < i; j++) {
                        text2[j] = text1[j];
                }
                point = i + 4;
                text2[i] = NULL;
        }

        if ((i = index(text1, "でなければ,")) != ERROR) {
                la = 0;
                for (j = 0; j < i; j++) {
                        text2[j] = text1[j];
                }
                point = i + 12;
                text2[i] = NULL;
        }
        printf("\n%s\n", text2);
        printf(" point = %d\n", point);

        if ((i = index(text1, "です.")) != ERROR) {
                        lb = 1;
                for (j = point; j < i; j++) {
                        text3[j - point] = text1[j];
                }
                text3[i - point] = NULL;
        }
        if ((i = index(text1, "ですか.")) != ERROR) {
                lb = 2;
                for (j = point; j < i; j++) {
                        text3[j - point] = text1[j];
                }
                text3[i - point] = NULL;
        }
        if ((i = index(text1, "ではありません.")) != ERROR) {
                lb = 0;
                for (j = point; j < i; j++) {
                        text3[j - point] = text1[j];
                }
                text3[i - point] = NULL;
        }
        printf("\n%s\n", text3);

        if (index(text1, "いちらん") != ERROR) {
                ichiran();
                continue;
        }

        if (index(text1, "どうしてですか") != ERROR ) {
                doushite();
                continue;
        }

        if (la == -1 || lb == -1) {
                continue;
        }

        /* ....naraba, ....desu. */
        if (la == 1 && lb == 1) {
                i = ttest(text2);
                j = ttest(text3);
                k = FUTEI;
                if ((i != ERROR) && (j != ERROR)) {
                        flagclr();
                        k = suiron(&i, &j, TR);
                }
                if ((k == FUTEI) || (k == MBTR) || (k == MBFS)) {
                        ques1();
                        while(kotae() == ERROR){
                                ques1();
                        }
                        if (ans == 3) continue;

                        if (ans == 1) {
                                ques2();
                                while(kotae() == ERROR) {
                                        ques2();
                                }
                                if (ans == 3) continue;

                                if (ans == 1) {
                                        ques3();
                                        while(kotae() == ERROR) {
                                                ques3();
                                        }
                                        if (ans == 3) continue;

                                        if (ans == 1) {
                                                opecode = 5;    /* = */
                                                wakatta();
                                        }
                                        else {
                                                opecode = 6;    /* <=> */
                                                wakatta();
                                        }
                                }
                                else {
                                        opecode = 1;            /* A=> */
                                        wakatta();
                                }
                        }
                        else {
                                ques2();
                                while(kotae() == ERROR) {
                                        ques3();
                                }
                                if (ans == 1) {
                                        opecode = 2;            /* A<= */
                                        wakatta();
                                }
                                else {
                                        ques4();
                                        while(kotae() == ERROR) {
                                                ques4();
                                        }
                                        if (ans == 1) {
                                                opecode = 3;    /* E=> */
                                                wakatta();
                                        }
                                        else{
                                                opecode = ERROR; /* ERROR */
                                                wakannai();
                                        }
                                }
                        }
                }
                else {
                        if ( k == TR) {
                                kotae1(1, 1, 1);
                        }
                        else {
                                kotae1(0, 1, 3);
                        }
                }
        }

        /*  ....namraba, ....denai.  */
        if (la == 1 && lb == 0) {
                i = ttest(text2);
                j = ttest(text3);
                k = FUTEI;
                if ((i != ERROR) && (j != ERROR)) {
                        flagclr();
                        tfflag[i] = 1;
                        k = suiron(&i, &j, TR);
                }
                if ((k == FUTEI) || (k == MBTR) || (k == MBFS)) {
                        ques5();
                        while(kotae() == ERROR){
                                ques5();
                        }
                        if (ans == 3) continue;

                        if (ans == 1) {
                                ques6();
                                while(kotae() == ERROR) {
                                        ques6();
                                }
                                if (ans == 3) continue;

                                if (ans == 1) {
                                        opecode = 11;           /* .FF. */
                                        wakatta();
                                }
                                else {
                                        opecode = 9;            /* A<> */
                                        wakatta();
                                }
                        }
                        else {
                                opecode = 7;                    /* E<> */
                                wakatta();
                        }
                }
                else {
                        if ( k == TR) {
                                kotae1(0, 0, 1);
                        }
                        else {
                                kotae1(1, 0, 3);
                        }
                }
        }

        /*  .... denakereba, .... desu. */
        if (la == 0 && lb == 1) {
                i = ttest(text2);
                j = ttest(text3);
                k = FUTEI;
                if ((i != ERROR) && (j != ERROR)) {
                        flagclr();
                        tfflag[i] = 1;
                        k = suiron(&i, &j, FS);
                }
                if ((k == FUTEI) || (k == MBTR) || (k == MBFS)) {
                        ques6();
                        while(kotae() == ERROR){
                                ques6();
                        }
                        if (ans == 3) continue;

                        if (ans == 1) {
                                ques5();
                                while(kotae() == ERROR) {
                                        ques5();
                                }
                                if (ans == 3) continue;

                                if (ans == 1) {
                                        opecode = 11;           /* .FF. */
                                        wakatta();
                                }
                                else {
                                        opecode = 9;            /* A<> */
                                        wakatta();
                                }
                        }
                        else {
                                opecode = 7;                    /* E<> */
                                wakatta();
                        }
                }
                else {
                        if ( k == TR) {
                                kotae1(1, 0, 1);
                        }
                        else {
                                kotae1(0, 0, 3);
                        }
                }
        }

        /*  ....denainara, ....denai.  */
        if (la == 0 && lb == 0) {
                i = ttest(text2);
                j = ttest(text3);
                k = FUTEI;
                if ((i != ERROR) && (j != ERROR)) {
                        flagclr();
                        k = suiron(&i, &j, FS);
                }
                if ((k == FUTEI) || (k == MBTR) || (k == MBFS)) {
                        ques1();
                        while(kotae() == ERROR){
                                ques1();
                        }
                        if (ans == 3) continue;

                        if (ans == 1) {
                                ques2();
                                while(kotae() == ERROR) {
                                        ques2();
                                }
                                if (ans == 3) continue;

                                if (ans == 1) {
                                        ques3();
                                        while(kotae() == ERROR) {
                                                ques3();
                                        }
                                        if (ans == 3) continue;

                                        if (ans == 1) {
                                                opecode = 5;    /* = */
                                                wakatta();
                                        }
                                        else {
                                                opecode = 6;    /* <=> */
                                                wakatta();
                                        }
                                }
                                else {
                                        opecode = 1;            /* A=> */
                                        wakatta();
                                }
                        }
                        else {
                                ques2();
                                while(kotae() == ERROR) {
                                        ques3();
                                }
                                if (ans == 3) continue;

                                if (ans == 1) {
                                        opecode = 2;            /* A<= */
                                        wakatta();
                                }
                                else {
                                        ques4();
                                        while(kotae() == ERROR) {
                                                ques4();
                                        }
                                        if (ans == 3) continue;

                                        if (ans == 1) {
                                                opecode = 3;    /* E=> */
                                                wakatta();
                                        }
                                        else{
                                                opecode = -1;
                                                wakannai();
                                        }
                                }
                        }
                }
                else {
                        if ( k == TR) {
                                kotae1(0, 0, 1);
                        }
                        else {
                                kotae1(1, 0, 3);
                        }
                }
        }
        if (lb != 2 && opecode != ERROR) {
                if (i == ERROR) {
                        i = touroku(text2);
                }
                if (j == ERROR) {
                        j = touroku(text3);
                }
                opetouroku(i, j);
                continue;
        }

        if (la == 1 && lb == 2) {
                i = ttest(text2);
                j = ttest(text3);
                k = suiron(&i, &j, TR);
                if (k == TR) {
                        kotae1(1, 1, 1);
                }
                if (k == MBTR) {
                        kotae1(1, 1, 2);
                }
                if (k == FS) {
                        kotae1(0, 1, 3);
                }
                if (k == MBFS) {
                        kotae1(0, 1, 4);
                }
                if (k == FUTEI) {
                        nnakoto();
                }
        }

        }
}


ttest(text)

char    *text;

{
        int i,j,k;

        for (i = 0; addr[i] != 0; i++) {
                if (strcmp(text, addr[i]) == 0) {
                        return(i);
                }
        }
        return(-1);
}

touroku(text)

char    *text;

{

        int     i;

        addr[textpoint] = endpoint;
        endpoint = endpoint + strlen(text) + 1;
        strcpy(addr[textpoint], text);
        i = textpoint;
        textpoint++;

        return(i);

}

opetouroku(i, j)

int     i, j;

{
        upadr[lastpt] = i;
        ope[lastpt] = opecode;
        downadr[lastpt] = j;
        lastpt++;
}

ichiran()

{
        int i, j, k;

        for (i = 0; addr[i] != 0; i++) {
                printf("%01d addr:%04x %03d %d %s \n", i, addr[i], defadr[i], tfflag[i], addr[i]);

        }
        scanf("%d\n", &j);
        for (i = 0; i < lastpt; i++) {
                printf("upadr: %03d , ope : %02d  , downadr : %03d \n", upadr[i], ope[i], downadr[i]);
        }
        scanf("%d\n", &j);
}

ques1()

{
        printf("  %s ならば必ず,%s なのですか?\n", text2, text3);
}

ques2()

{
        printf("  %s ならば必ず,%s なのですか?\n", text3, text2);
}

ques3()

{
        printf("  %s と,%s とは同じことなのですか?\n", text2, text3);
}

ques4()

{
        printf("  %s は,%s の時もあるのですね?\n", text2, text3);
}

ques5()

{
        printf("  もし %s ならば,絶対 %s ではないのですか?\n", text2, text3);
}

ques6()

{
        printf("  もし %s でなければ,絶対 %s ですか?\n",text2, text3);
}



kotae()

{
        printf("> ");
        scanf("%s", text1);
        printf("\n");
        if (strcmp(text1, "はい.") == 0 || strcmp(text1, "はい") == 0) {
                ans = 1;
                return(1);
        }
        if (strcmp(text1, "いいえ.") == 0 || strcmp(text1, "いいえ") == 0) {
                ans = 0;
                return(0);
        }
        if (strcmp(text1, "まちがえました.") == 0 || strcmp(text1, "まちがえました") == 0) {
                ans = 3;
                return(0);
        }

        return(-1);
}

wakatta()

{

        printf("  はい,わかりました.\n");
        printf("opecode = %d\n", opecode);

}

wakannai()

{
        printf("  ええと・・・わかんなくなちゃった.\n");
        printf("opecode = %d\n", opecode);
}

nnakoto()

{
        printf("  んなこといわれたって,わかんないよ・・・あたし.\n");
}

kotae1(flag1, flag2, flag3)

int     flag1, flag2, flag3;

{
        if (flag1 == 1) {
                printf("  はい,");
        }
        else {
                printf("  いいえ,");
        }
        printf(" %s ", text2);
        if (flag2 == 1) {
                printf("は,");
        }
        else {
                printf("でなければ,");
        }
        printf(" %s ", text3);
        if (flag3 == 1) {
                printf("です.\n");
        }
        if (flag3 == 2) {
                printf("かもしれません.\n");
        }
        if (flag3 == 3) {
                printf("ではありません.\n");
        }
        if (flag3 == 4) {
        printf("でないかもしれません.\n");
        }

}

kotae2(i, j, k, l)

char    *i, *k;

int     j, l;

{
        printf(" %s ", i);

        if (j == 1) {
                printf("ならば,");
        }
        if (j == 2) {
                printf("かもしれなければ,");
        }
        if (j == 3) {
                printf("でなければ,");
        }
        if (j == 4) {
                printf("でないかもしれないなら,");
        }
        printf(" %s ", k);
        if (l == 1) {
                printf("です.\n");
        }
        if (l == 2) {
                printf("かもしれません.\n");
        }
        if (l == 3) {
                printf("ではありません.\n");
        }
        if (l == 4) {
                printf("でないかもしれません.\n");
        }

}

suiron(ai, aj, flag)

int     *ai, *aj;
int     flag;

{
        int i, j, k, l, henka;
        si = *ai;
        sj = *aj;

        for (i = 0; i < 100; i++) {
                tfflag[i] = FUTEI;
                defadr[i] = 0;
        }
        tfflag[*ai] = flag;
looptop:
        for (i = 0; i < lastpt; i++) {
                j = tfflag[upadr[i]];
                k = tfflag[downadr[i]];
                if (j == TR || j == FS) {
                        l = op1[ope[i]][j];
                        if (k == MBTR && k == MBFS) {
                                if (l == TR || l == FS) {
                                        tfflag[downadr[i]] = l;
                                        defadr[downadr[i]] = upadr[i];
                                        henka += 1;
                                }
                        }
                        if (k == FUTEI) {
                                tfflag[downadr[i]] = l;
                                defadr[downadr[i]] = upadr[i];
                                henka += 1;
                        }
                }
                if (j == MBTR || j == MBFS) {
                        if (k == FUTEI) {
                                l = op1[ope[i]][j];
                                tfflag[downadr[i]] = l;
                                defadr[downadr[i]] = upadr[i];
                                henka += 1;
                        }
                }

                if (k == TR || k == FS) {
                        l = op2[ope[i]][k];
                        if (j == MBTR && j == MBFS) {
                                if (l == TR || l == FS) {
                                        tfflag[upadr[i]] = l;
                                                defadr[upadr[i]] = downadr[i];
                                        henka += 1;
                                }
                        }
                        if (j == FUTEI) {
                                tfflag[upadr[i]] = l;
                                defadr[upadr[i]] = downadr[i];
                                henka += 1;
                        }
                }
                if (k == MBTR || k == MBFS) {
                        if (j == FUTEI) {
                                l = op2[ope[i]][k];
                                tfflag[upadr[i]] = l;
                                defadr[upadr[i]] = downadr[i];
                                henka += 1;
                        }
                }

        }
        if (*aj == TR || *aj == FS) {
                return(tfflag[*aj]);
        }
        if (henka == 0) {
                return(tfflag[*aj]);
        }
        henka = 0;
        goto looptop;

}

doushite()

{
        int i, j, k;

        if (sj == FUTEI) {
                nnakoto();
                return(ERROR);
        }
        for (i = 0; i< 100; i++) {
                siadr[i] = 0;
                sitff[i] = 0;
                sjadr[i] = 0;
                sjtff[i] = 0;
        }
        for (i = 0, j = sj;; i++) {
                sjadr[i] = j;
                sjtff[i] = tfflag[j];
                siadr[i] = defadr[j];
                sitff[i] = tfflag[defadr[j]];
                j = defadr[j];
                if (siadr[i] == si) break;
        }
        for (; i >= 0; i--) {
                kotae2(addr[siadr[i]], sitff[i], addr[sjadr[i]], sjtff[i]);
        }
        printf(" ですから,");
        kotae2(addr[si], tfflag[si], addr[sj], tfflag[sj]);

}

flagclr()

{
        int i;
        for (i = 0; i < 100; i++) {
                tfflag[i] = 0;
        }

}

opeinit()

{

        int     i, j, k;

        /* A => */
        op1[1][1] = TR; op1[1][2] = MBTR; op1[1][3] = FS; op1[1][4] = MBFS;

        /* A <= */
        op1[2][1] = MBTR; op1[2][2] = MBTR; op1[2][3] = FS; op1[2][4] = MBFS;

        /* E => */
        op1[3][1] = MBTR; op1[3][2] = MBTR; op1[3][3] = MBFS; op1[3][4] = MBFS;

        /* E <= */
        op1[4][1] = MBTR; op1[4][2] = FUTEI; op1[4][3] = MBFS; op1[4][4] = FUTEI;

        /* A <=> */
        op1[5][1] = TR; op1[5][2] = MBTR; op1[5][3] = FS; op1[5][4] = MBFS;

        /* A = */
        op1[6][1] = TR; op1[6][2] = MBTR; op1[6][3] = FS; op1[6][4] = MBFS;

        /* E <> */
        op1[7][1] = MBFS; op1[7][2] = MBFS; op1[7][3] = MBTR; op1[7][4] = MBTR;

        /* E <> */
        op1[8][1] = MBFS; op1[8][2] = MBFS; op1[8][3] = MBTR; op1[8][4] = MBTR;

        /* A <> */
        op1[9][1] = FS; op1[9][2] = MBFS; op1[9][3] = MBTR; op1[9][4] = MBTR;

        /* A <> */
        op1[10][1] = FS; op1[10][2] = MBFS; op1[10][3] = MBTR; op1[10][4] = MBTR;

        /* .FF. */
        op1[11][1] = FS; op1[11][2] = MBFS; op1[11][3] = TR; op1[11][4] = MBTR;

        /* .FF. */
        op1[12][1] = FS; op1[12][2] = MBFS; op1[12][3] = TR; op1[12][4] = MBTR;

        for (i = 1; i <= 6; i++) {
                k = i * 2;
                for (j = 1; j <= 4; j++) {
                        op2[k - 1][j] = op1[k][j];
                }
                for (j = 1; j <= 4; j++) {
                        op2[k][j] = op1[k - 1][j];
                }
        }
}