//paul koop m.a. mogeln2.cpp
/******************************************************************/
/* Die Simulation wurde ursprunglich entwickelt,                  */
/* um die Verwendbarkeit von Zellularautomaten                    */
/* fuer die Algorithmisch Rekursive Sequanzanalyse                */
/* zu ueberpruefen								*/
/* Modellcharakter hat allein der Quelltext. Eine Compilierung    */
/* dient nur als Falsifikationsversuch                            */
/******************************************************************/
//includedateien
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//datenstruktur

//prototypen
class   inhalt;
struct	zell;
typedef inhalt* zahl;
typedef zell zelle[80];
void    aufbau();
void    abbaux(zahl x);
void    abbauy(zahl y);
void    text_anfang();
void    text_ende();


//const
const int true  = 1;
const int false = 0;
const unsigned char l = 'k';
const unsigned char t = 'm';
const int kooperieren = true;
const int mogeln =false;

//typedef

//classen
class   inhalt
	      {//BEGIN inhalt
	       public:
	       int i;
	       zahl v;
	       zahl n;
	      };//END inhalt
struct  zell
	      {//BEGIN zelle
	       int einstellung;
	       int punkte;
	      };//END zelle

zelle a[80],b[80];
void zufall(zelle *von);
void editor(zelle *von);
void korrektor(zelle *von);
void neupunkte(zelle *r, zahl x, zahl y);
void neunachkomme(zelle *r, zahl x, zahl y);

//variablendefinition


zahl n,x,y,xa,ya;

//prozeduren und funktionen

void aufbau()
{//BEGIN aufbau

 int z = 0;
 n = new(inhalt);
 xa   = n;
 x    = n;
 x->i = z;
 do
 {//BEGIN do
  z=z+1;
  n=new(inhalt);
  x->n=n;
  n->v=x;
  x=n;
  x->i=z;
 }//END do
 while(z<79);
 x->n=xa;
 xa->v=x;

 z=0;
 n=new(inhalt);
 ya   = n;
 y    = n;
 y->i = z;
 do
 {//BEGIN do
  z=z+1;
  n=new(inhalt);
  y->n = n;
  n->v = y;
  y    = n;
  y->i = z;
 }//END do
 while (z<23);
 y->n  = ya;
 ya->v = y;
}//END aufbau

void abbaux(zahl x)
{//BEGIN abbaux
 //cout << "abbaux "<<x->i<<endl;
 if (x->n != xa) {abbaux(x->n);}
 delete(x);
}//END abbaux

void abbauy(zahl y)
{//BEGIN abbauy
 //cout << "abbauy "<<y->i<<endl;
 if (y->n != ya) {abbauy(y->n);}
 delete(y);
}//END abbauy

void neupunkte (zelle *r, zahl x, zahl y)
{//BEGIN neu

  if (r[x->i][y->i].einstellung==kooperieren)
  {
  if (r[x->v->i][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->v->i][y->v->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->v->i][y->v->i].punkte+=4;}
  if (r[x->i   ][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->i   ][y->v->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->i   ][y->v->i].punkte+=4;}
  if (r[x->n->i][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->n->i][y->v->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->n->i][y->v->i].punkte+=4;}
  if (r[x->v->i][y->i   ].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->v->i][y->i   ].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->v->i][y->i   ].punkte+=4;}
  if (r[x->n->i][y->i   ].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->n->i][y->i   ].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->n->i][y->i   ].punkte+=4;}
  if (r[x->v->i][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->v->i][y->n->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->v->i][y->n->i].punkte+=4;}
  if (r[x->i   ][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->i   ][y->n->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->i   ][y->n->i].punkte+=4;}
  if (r[x->n->i][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=2;r[x->n->i][y->n->i].punkte+=2;}else{r[x->i][y->i].punkte+=-1;r[x->n->i][y->n->i].punkte+=4;}
  }
  else
  {
  if (r[x->v->i][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->v->i][y->v->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->v->i][y->v->i].punkte+=0;}
  if (r[x->i   ][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->i   ][y->v->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->i   ][y->v->i].punkte+=0;}
  if (r[x->n->i][y->v->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->n->i][y->v->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->n->i][y->v->i].punkte+=0;}
  if (r[x->v->i][y->i   ].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->v->i][y->i   ].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->v->i][y->i   ].punkte+=0;}
  if (r[x->n->i][y->i   ].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->n->i][y->i   ].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->n->i][y->i   ].punkte+=0;}
  if (r[x->v->i][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->v->i][y->n->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->v->i][y->n->i].punkte+=0;}
  if (r[x->i   ][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->i   ][y->n->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->i   ][y->n->i].punkte+=0;}
  if (r[x->n->i][y->n->i].einstellung==kooperieren) {r[x->i][y->i].punkte+=-1;r[x->n->i][y->n->i].punkte+=4;}else{r[x->i][y->i].punkte+=0;r[x->n->i][y->n->i].punkte+=0;}

  }

}//END neu

void neunachkomme (zelle *r, zahl x, zahl y)
{//BEGIN neu
  int punkte;
  punkte = r[x->i][y->i].punkte;
  if (r[x->v->i][y->v->i].punkte>punkte) {punkte=r[x->v->i][y->v->i].punkte;r[x->i][y->i].einstellung=r[x->v->i][y->v->i].einstellung;}
  if (r[x->i   ][y->v->i].punkte>punkte) {punkte=r[x->i   ][y->v->i].punkte;r[x->i][y->i].einstellung=r[x->i   ][y->v->i].einstellung;}
  if (r[x->n->i][y->v->i].punkte>punkte) {punkte=r[x->n->i][y->v->i].punkte;r[x->i][y->i].einstellung=r[x->n->i][y->v->i].einstellung;}
  if (r[x->v->i][y->i   ].punkte>punkte) {punkte=r[x->v->i][y->i   ].punkte;r[x->i][y->i].einstellung=r[x->v->i][y->i   ].einstellung;}
  if (r[x->n->i][y->i   ].punkte>punkte) {punkte=r[x->n->i][y->i   ].punkte;r[x->i][y->i].einstellung=r[x->n->i][y->i   ].einstellung;}
  if (r[x->v->i][y->n->i].punkte>punkte) {punkte=r[x->v->i][y->n->i].punkte;r[x->i][y->i].einstellung=r[x->v->i][y->n->i].einstellung;}
  if (r[x->i   ][y->n->i].punkte>punkte) {punkte=r[x->i   ][y->n->i].punkte;r[x->i][y->i].einstellung=r[x->i   ][y->n->i].einstellung;}
  if (r[x->n->i][y->n->i].punkte>punkte) {punkte=r[x->n->i][y->n->i].punkte;r[x->i][y->i].einstellung=r[x->n->i][y->n->i].einstellung;}


}//END neu

void text_anfang()
{// BEGIN text_anfang
  window(1,25,80,25);
  textbackground(RED);
  textcolor(WHITE);
  clrscr();
  cout<<"Koop Editor <F10>Zufall <Pfeil>Cursor  <Einfg>kooperieren <Entf>tot <Return>Start";
  gotoxy(1,1);
  textcolor(2);
  textbackground(BLUE);
  window(1,1,80,24);clrscr();window(1,1,80,25);gotoxy(1,1);
}// END text_anfang

void text_ende()
{// BEGIN text_ende
  gotoxy(1,25);
  textbackground(RED);
  textcolor(WHITE);
  cout<<"Koop Mogeln <F1>neu       <F2>Editieren                    <RETURN> Ende";
  gotoxy(1,1);
  textcolor(2);
  textbackground(BLUE);
  window(1,1,80,24);clrscr();window(1,1,80,25);gotoxy(1,1);
}// END text_ende

void zufall( zelle* von)
{//BEGIN zufall
 int z;
  randomize();gotoxy(1,1);
  for (int y=0;y<=23;y++)
   {//BEGIN for1
    for (int x=0;x<=79;x++)
     {//BEGIN for2
      z = random(2);
      //cout<<x<<y;
      if (z == 0)
      {//BEGIN
	(von[x][y].einstellung) = kooperieren;
	(von[x][y].punkte) = 0;
	cout<<von[x][y].einstellung;
      }//END
      else
      {//BEGIN
	(von[x][y].einstellung) = mogeln;
	(von[x][y].punkte) = 0;
	cout<<von[x][y].einstellung;
      }//END
     }//END for2
    }//END for1
    //cout<<"Test zufall";
}// END zufall

void editor (zelle* von)
{//BEGIN editor
    char ch1,ch2,defchar;
    int z;

  z =mogeln;
  randomize();
  text_anfang();
  gotoxy(1,1);
  textcolor(2);
  textbackground(BLUE);
  window(1,1,80,24);
  gotoxy(1,1);

  for(int yz=0;yz<24;yz++)
  {
   for (int xz=0;xz<80;xz++)
   {
    von[xz][yz].einstellung=mogeln;
    cout<<von[xz][yz].einstellung;
   }
  }

   x= xa;
   y= ya;
   do
   {
   ch1 = getch();
   if (ch1 ==0)
    {//BEGINif
     ch2 = getch();
     switch (ch2)
     {//switch
      case 68 : {
		 zufall(von);
		 break;
		}
      case 72 : {y = y->v;break;}
      case 80 : {y = y->n;break;}
      case 75 : {x = x->v;break;}
      case 77 : {x = x->n;break;}
      case 71 : {y = y->v;
		 x = x->v;
		break;}
      case 73 :  {y = y->v;
		  x = x->n;
		  break;}
      case 81 :  {y = y->n;
		  x = x->n;
		  break;};
      case 79 :  {y = y->n;
		  x = x->v;
		  break;}

      case 83 :  { z = !(kooperieren);break;}
      case 82 :  { z = kooperieren;break;}
      default :   { defchar=7;cout<<defchar;}
     }//switch
    gotoxy(x->i,y->i);
    if (z)
     {
       von[x->i][y->i].einstellung = kooperieren;
       cout<<l;
       gotoxy(x->i,y->i);
      }
      else
      {
       von[x->i][y->i].einstellung = !(kooperieren);
       cout<<t;
       gotoxy(x->i,y->i);
      }
    }//end if ch==0;
    else {defchar=7;cout<<defchar;}
   }
   while (ch1 != 13);
   text_ende();
}//END editor

void korrektor (zelle* von)
{//BEGIN korrektor
    char ch1,ch2,defchar;
    int z;

  z =mogeln;
  randomize();
  text_anfang();
  textcolor(2);
  textbackground(BLUE);
  window(1,1,80,24);
  gotoxy(1,1);

  gotoxy(1,1);
  for(int yz=0;yz<24;yz++)
  {
   for (int xz=0;xz<80;xz++)
   {
    //if(von[xz][yz].einstellung==kooperieren){cout<<l;}else{cout<<t;}
    cout<<von[xz][yz].einstellung;
   }
  }

   x= xa;
   y= ya;
   do
   {
   ch1 = getch();
   if (ch1 ==0)
    {//BEGINif
     ch2 = getch();
     switch (ch2)
     {//switch
      case 68 : {
		 zufall(von);
		 break;
		}
      case 72 : {y = y->v;break;}
      case 80 : {y = y->n;break;}
      case 75 : {x = x->v;break;}
      case 77 : {x = x->n;break;}
      case 71 : {y = y->v;
		 x = x->v;
		break;}
      case 73 :  {y = y->v;
		  x = x->n;
		  break;}
      case 81 :  {y = y->n;
		  x = x->n;
		  break;};
      case 79 :  {y = y->n;
		  x = x->v;
		  break;}

      case 83 :  { z = !(kooperieren);break;}
      case 82 :  { z = kooperieren;break;}
      default :   { defchar=7;cout<<defchar;}
     }//switch
    gotoxy(x->i,y->i);
    if (z)
     {
       von[x->i][y->i].einstellung = kooperieren;
       cout<<l;
       gotoxy(x->i,y->i);
      }
      else
      {
       von[x->i][y->i].einstellung = !(kooperieren);
       cout<<t;
       gotoxy(x->i,y->i);
      }
    }//end if ch==0;
    else {defchar=7;cout<<defchar;}
   }
   while (ch1 != 13);
   text_ende();
}//END korrektor
void spielpunkte(zelle *von)
{// BEGIN spiel
  y =ya;
  x =xa;
  do{
   do{
    neupunkte(von,x,y);
    //cout<<von[x->i][y->i].punkte;
    x = x->n;
   } while (x!=xa);
   y = y->n;
  }while (y!=ya);
 }//END spiel

void spielnachkomme(zelle *nach)
{// BEGIN spiel
  y =ya;
  x =xa;
  do{
   do{
    neunachkomme(nach,x,y);
    //if (nach[x->i][y->i].einstellung) {cout<<l;}else{cout<<t;}
    cout<<nach[x->i][y->i].einstellung;
    x = x->n;
   } while (x!=xa);
   y = y->n;
  }while (y!=ya);
 }//END spiel

void spielpunktenull(zelle *von)
{// BEGIN spiel
  y =ya;
  x =xa;
  do{
   do{

    von[x->i][y->i].punkte=0;
    x = x->n;
   } while (x!=xa);
   y = y->n;
  }while (y!=ya);
 }//END spiel

void hauptprogramm()
{

    char ch1,ch2;
    int xz,yz;

   if (kbhit()){ do{ ch1 = getch();} while (kbhit());}
   do
   {//do
   if(kbhit())
     {
      ch1 = getch();
      if (ch1 == 0)
	{
	  ch2=getch();
	  switch (ch2)
	  {
	   case 59: {
		 x = xa;
		 abbaux(x);
		 y = ya;
		 abbauy(y);
		 aufbau();
		 editor(a);
		break;}
	   case 60: {korrektor(a);break;}
	  };
	 }
     }
    else
    {
     textcolor(2);
     textbackground(BLUE);
     window(1,1,80,24);
     gotoxy(1,1);
     spielpunktenull(a);
     spielpunkte(a);
     spielnachkomme(a);
     

    }
   }//do
   while (ch1 != 13);
 };


//hauptprogramm
void main()
{//BEGIN main
  aufbau();
  editor(a);
  hauptprogramm();
  x = xa;
  abbaux(x);
  y = ya;
  abbauy(y);
}//END main


