#define TTHREE
#define global
#include "iteach1.h"



/* The main program contains the error messages, which correspond to those in the Mac program. Factor sets the tick size for timing. A file is read in if specified, and the appropriate headings and copyright messages are printed. The teach subroutine is then entered */

void main(short argc,char **argv)

{
#ifdef MAC
	argc = ccommand(&argv);
#else
	textmode(3);
#endif
	factor = CLK_TCK;
	init(); /* Set up variables and time */
	if (argc == 2) { /* If a file was specified, open it */
		openDo(argc,argv);
		}
	clrscr();
	gotoxy(16,8);

/* prog indicates whether MT2 (prog=1) or MT3(prog=2 or 4) is executing*/

	if (prog == '2' || prog == '4')	
		puts("MacTeach 3, Version 2.1: Learning Loglan Affixes");
	else puts ("MacTeach 2, Version 2.1: Learning Loglan Words");
	gotoxy(11,10);
	puts ("Copyright(C) 1986-94 by the Loglan Institute, Inc.");
	gotoxy(30,11);
	puts("All rights reserved.");
	gotoxy(19,13);
	puts ("Programmed in  C by Robert A. McIvor");
	gotoxy(27,15);
	puts ("Based on a FORTH program by");
	gotoxy(24,16);
	puts ("James Cooke Brown & Glen B. Haydon");
	gotoxy(26,18);
	puts ("Press any key to continue.");
	getche();
	if(inpfile) display();
	doTeach(argc,argv); /* Begin the teaching session */
	}

void doSeek(void)

/* This routine is used to look up a word or word fragment. All primitives,
	affixes or English equivalents containing the string entered will be 
	displayed.  Unlike the Mac program, no selection of prim, affix, or 
	translation is required */

{	short i;
	char istring[10];
	if (!testIn()) return;
	puts("Enter search string");
	gets(istring);
	for (i=0; i<cardcount; ++i) 
		if(strindex(flasharray[i].prim,istring) != -1
		|| strindex(flasharray[i].affix,istring) != -1
		|| strindex(flasharray[i].trans,istring) != -1) {
		if (showSel(i)==STOPF) break;
		}
	}


/* On a correct response, advance the rung and count of right responses. 
	If in error box, check for return to the ladder */

void promote(short n)

/* On a correct response, advance the rung and count of right responses. 
	If in error box, check for return to the ladder */

{	gotoxy(15,18);
	printf("Correct!\n");
	right += 1;
	if (flasharray[n].flag[curflag] != '4') 
		flasharray[n].flag[curflag] += 1;
	if (flasharray[n].flag[curflag] == '5' + drillflg)
		flasharray[n].flag[curflag] = '1';
	}

void show4(short n)

/* On entry n is the card to be displayed. Display the primitive in an appropriate screen location */

{	puts("\n");
	gotoxy(10,10);
	printf("%s\n",flasharray[n].prim);
	}

void show5(short n)

/* On entry n is the card to be displayed. Display the affix in an appropriate screen location */

{	gotoxy(30,10);
	printf("%s\n",flasharray[n].affix);
	}

void show611(short n)

/* On entry n is the card to be displayed. Display the English translation and English alternates in appropriate screen locations */

{	char *rest;
	gotoxy(10,12);
	strcpy(instring,flasharray[n].trans);
	rest = instring;
	while (*rest++) {
		if (*(rest-1) == '-') *(rest-1) = ' ';
		if (*(rest-1) == '_') *(rest-1) = '-';
		}
	rest = instring;
	while ( *rest++ != '/' && *rest);
	if (*rest) *(rest-1) = NUL;
	printf("%s\n",instring);
	if (*rest && strlen(rest)) {
		gotoxy(10,14);
		printf("Or: %s\n",rest);
		}
	}

void show7(short n)

/* On entry n is the card to be displayed. Display the English extension in an appropriate screen location */

{	gotoxy(30,12);
	printf("(%s)\n",flasharray[n].extn);
	}


short showSel(short n)

/* On entry n is the card to be displayed for selecting or lookup. Display the various card fields in appropriate screen locations, and instructions and rung count at the bottom of the screen. A return character indicates whether pass, stop, or select was selected. */

{	clrscr();
	show4(n);
	show5(n);
	show611(n);
	show7(n);
	gotoxy(1,19);
	if (cmd == SELECT) {
		puts("Type Y or Return to select a card,");
		puts("     X or Q to return to Ladder Page,");
		puts("     Any other key to pass over a card.");
		gotoxy(5,1);
		printf("%d Selected.\n",selection);
		++selection;
		}
	else if (cmd==LOOKUP) 
		puts("Type Return to continue searching, X or Q to end.");
	else {
		puts("Type Return to continue.");
		gotoxy(5,1);
		printf("%d Left.\n",left);
		}
	return screenchr();
	}

short showTest(short n)

/* Display  partial card n for test or review purposes.o Measure response time and flag timeouts. Verify correctness of response.*/

{	clrscr();
	gotoxy(5,1);
	if (reviewflag) { /*Rung count does not change*/
		printf("%d Left.",left);
		--left;
		}
	else {
		countflag(); /*Rung count changes during test so must be calc'd*/
		printf("%d Left.",r[rungflag-'0']);
		}
	switch(curflag) { /*Show appropriate fields for nature of test*/
	case 0:
		show4(n);
		show5(n);
		break;
	case 3:
		show4(n);
		break;
	case 1:
		show611(n);
		show7(n);
		break;
	case 2:
		show5(n);
		break;
	default: break;
		}
	gotoxy(1,20);
	puts("Press RETURN to see answer.                       ");
	if (!reviewflag) { /*Ask for a response*/
	puts("Type C to cancel error-boxing from an accidental typing error.");
		gotoxy(10,16);
		printf("-->  ");
		if (doTime() < 0) { /*Measure time to first keystroke*/
			SysBeep(PAR); /*If threshold exceeded, equivalent to error*/
			++tmouts;
			demote(n);
			statdo(ticks);
			goto show; /*Display answer*/
			}
		gets(instring); /* Get the response */
		}
	else gets(instring); /* Get the response from review (Next,Halt)*/
	if (!strlen(instring)) { /*No answer is an error*/
		demote(n);
		goto show;
		}
	if (reviewflag) goto show; /*Show answer for review*/
	statdo(ticks); /* update the statistics */
	switch(curflag) { /*Check the answer for correctness*/
	case 0: 
		strcpy(istring,flasharray[n].trans);
		mycomp(istring,instring) ? promote(n) : demote(n);
		break;
	case 1:
	case 2: 
		strcmp(instring,flasharray[n].prim)? demote(n):promote(n);
		break;
	case 3: 
		strcmp(instring,flasharray[n].affix)? demote(n):promote(n);
		break;
		}
show:
	switch(curflag) { /*Show the answer fields*/
	case 1:
		show4(n);
		show5(n);
		break;
	case 3:
	case 0:
	case 2:
		show4(n);
		show5(n);
		show611(n);
		show7(n);
		break;
		}
	gotoxy(20,1);
	printf("Press RETURN to continue, X or Q to end %s\n",
		reviewflag ? "review." : "test.  ");
	return screenchr(); /*Get the return response*/
	}
