/* friss.c - Spiel fr EPAC68008 */

#include <dos.h>

#define auf 72
#define ab 80
#define rechts 77
#define links 75
#define futteranfang 5
#define wartezeit 20000

long zufall;
int futter,gefressen;
char bild[2000];

#define Cconis() bdos(11,0,0)
#define Cnecin() bdos(8,0,0)
#define Cconout(char) bdos(2,(unsigned int)char,0)

void stringaus(char *string) {
  while (*string!=0)
    Cconout(*string++); }

void zahlaus(int zahl) {
  if (zahl>9)
    zahlaus(zahl/10);
  Cconout(0x30+zahl%10); }

void setzecursor(int y, int x) {
  Cconout('\x1b'); Cconout('[');
  zahlaus(y+1); Cconout(';');
  zahlaus(x+1); Cconout('H'); }

int rnd(int modulo) {
  zufall=(109*zufall+853)%4096;
  return(zufall%modulo); }

void zeichnebild() {
  int i,x,y;
  for (i=0; i<2000; i++)
    bild[i]=' ';
  for (i=0; i<80; i++) {
    bild[i]='X'; bild[23*80+i]='X'; }
  for (i=0; i<24; i++) {
    bild[i*80]='X'; bild[i*80+79]='X'; }
  bild[81]='O';
  for (i=0; i<futter; i++) {
    x=rnd(78)+1; y=rnd(22)+1;
    while (bild[y*80+x] != ' ')
      x=rnd(78)+1;
    bild[y*80+x]='X'; }
  for (i=0; i<futter; i++) {
    x=rnd(78)+1; y=rnd(22)+1;
    while (bild[y*80+x] != ' ')
      x=rnd(78)+1;
    bild[y*80+x]='.'; }
  bild[24*80]='\0';
  setzecursor(24,0); stringaus(bild);
  setzecursor(24,8); stringaus("friss!");
  setzecursor(24,40); stringaus("futter =   , gefressen =   ");
  setzecursor(24,49); zahlaus(futter);
  setzecursor(24,65); zahlaus(gefressen); }

void main() {
  int x,y,dx,dy,taste;
  long warte;
neuanfang:
  stringaus("beliebige Taste druecken! ");
  zufall=Cnecin();
  futter=futteranfang;
  while (1) {
    gefressen=0;
    zeichnebild();
    x=1; y=1;
    do {
      taste=Cnecin()&0xff;
    } while (taste==0);
    switch (taste) {
      case auf:
	dx=0; dy=-1; break;
      case ab:
	dx=0; dy=1; break;
      case rechts:
	dx=1; dy=0; break;
      case links:
	dx=-1; dy=0; }
    while (gefressen!=futter) {
      if (Cconis() & 0xff) {
	do {
	  taste=Cnecin()&0xff;
	} while (taste==0);
	switch (taste) {
	  case auf:
	    dx=0; dy=-1; break;
	  case ab:
	    dx=0; dy=1; break;
	  case rechts:
	    dx=1; dy=0; break;
	  case links:
	    dx=-1; dy=0; } }
      x=x+dx; y=y+dy;
      switch (bild[y*80+x]) {
	case ' ':
	  bild[y*80+x]='O'; setzecursor(y,x); Cconout('O'); break;
	case '.':
	  bild[y*80+x]='*'; setzecursor(y,x); Cconout('*');
	  gefressen=gefressen+1;
	  setzecursor(24,65); zahlaus(gefressen); break;
	case 'O':
	case '*':
	case 'X':
	  setzecursor(12,28);
	  stringaus("Kollision, Spiel beendet");
	  setzecursor(14,28);
	  goto neuanfang; }
      if (dy!=0)
	for (warte=0; warte<wartezeit; warte++);
      for (warte=0; warte<wartezeit; warte++); }
    futter=futter+1; } }

