sábado, 30 de noviembre de 2013

Game of Life C++ Program Example

#include <iostream>
#include <cstdlib>
using namespace std;

const int xRow=42+2;
const int xCol=77+2;
int Xrow=xRow;
int Xcol=xCol;
int Xnum;

void dspCont();
void dspMenu();
void dspGMenu();
void subMenu();
void runGame(int num[][xCol], int &xrow, int &xcol, char fld);
void scrPaint(int num[][xCol],char fld);
void clcNeigh(int num[][xCol],int &xrow, int &xcol, char fld);
void chkTest(char fld[]);

int main()
{
  char menu1=' ';

  while(menu1 != 'Q' || menu1 != 'q' )

    {
      dspMenu();

      cout << "Q. Quit" << endl;
      cout << ' ' << endl;
      cin >> menu1 ;

      switch(menu1)
{
case '1':

 dspMenu();
 subMenu();

 break;

case '2':
 int Erow, Ecol;

 dspMenu();

 cout << "** Row/Col Limits : " << xRow-2 << "/" <<
   xCol-2 << " **\n " << endl;
 cout << "Enter Row & Col " ;
 cin >> Erow >> Ecol;

 if (Erow > 0 && Erow < (xRow+1)-2 )
   if ( Ecol > 0 && Ecol < (xCol+1)-2 )
     {
Xcol = Ecol+2;
Xrow = Erow+2;
     }

 break;

case '3':
 cout << '\n' << endl;
 cout << " **The Game of Life** \n";
 cout << "The mathematician John Horton Conway invented the 'Game of Life'."
      << "Through not a 'game' in any traditional sense, it provides"
      << "interesting behavior that is specified with only a few rules";

 cout << "The program follows the rule of LIFE to show the continuing behavior of the configuration." << endl;
 cout << "Generations mark the passing of time."
      << "Each generation brings births and deaths to the LIFE continuity." << endl;
 cout << "The births and deaths follow the following set of rules:" << endl;

 cout << "     1. We define each cell to have 8 neighbord cells.\n";
 cout << "     2. If an occupied cell has zero or one neighbors, it dies of loneliness.\n";
 cout << "     3. If an empty cell has exactly 3 ocuppied neighbor cells, there is a birth"
      << " of a new cell to replace the empty cell.\n";
 cout << "     4. Births and deaths are instantaneous and occur at the changes of generations."
      << " A cell dying for whatever reason may help cause birth, but a"
      << " newborn cell cannot resurrect a cell that is dying, nor will a"
      << " cell's death prevent the death of another, say, by reducing the local population\n";

 dspCont();

case 'Q':
 break;
}

      if(menu1 == 'Q' || menu1 == 'q' )

break;
    }

  cout << "\n" << endl;
  cout << "ALL DONE ! \n \n " << endl;

  return 0;
}
void subMenu()
{
  char menu2=' ';
  int cnt[xRow][xCol];
  int pRow=1;
  int pCol=1;

  scrPaint(cnt,'C');
  bool mnuflg=false;

  while(menu2 != 'Q' || menu2 != 'q' )
    {
      if(menu2=='d' || menu2=='D')
{
 mnuflg=false;
}
      if(mnuflg)
{
 cout << "Position : ( " << pRow << "-" << pCol << " )" << endl;
 cout << "Press 1=Fill 2=Blank -D-one " << endl;
}
      else
{
 cout<< "" << endl;
 cout  << "Press -F-ill -C-lear -S-elect -R-un -Q-uit " << endl;
}
   
   
      cout  << "" << endl;
   
      cin >> menu2 ;

      switch(menu2)
{
case 'D': case 'd':
 scrPaint(cnt, 'P');
 break;

case 'S': case 's': case '1': case '2':
 mnuflg=true;
 runGame(cnt,pRow,pCol,menu2);
 scrPaint(cnt, 'P');
 break;

case 'R': case 'r':
 if(!mnuflg)
   {
     pRow=1,pCol=1;
     runGame(cnt,pRow,pCol,menu2);
     scrPaint(cnt, 'P');
   }
 break;

case 'C': case 'c':
 if(!mnuflg)
   {
     scrPaint(cnt, 'C');
     pRow=1,pCol=1;
   }
 break;

case 'F': case 'f':
 if(!mnuflg)
   {
     scrPaint(cnt, 'F');
     pRow=1,pCol=1;
   }
 break;

case 'Q':
 break;
}
      if(menu2 == 'Q' || menu2 == 'q' )
break;
    }

}

void dspMenu()
{

  cout << ' ' << endl;
  cout << "Game of Life\n " << endl;
  cout << "1. Run Game ( calculate at -" << Xrow-2 << "- Rows and -" << Xcol-
    2 << "- Columns " << endl;
  cout << "2. Change the Game Dimensions of rows / cols " << endl;
  cout << "3. Read Me" << endl;
  cout << " " << endl;
}

void dspGMenu()
{

  cout << " ** Game of Life ** " << endl;
}

void dspCont()
{
  char chcc;
  cout << " \n Press C and then <RET> to Continue " ;
  cin >> chcc;
}

void scrPaint(int num[][xCol], char fld)
{
  dspGMenu();
  char line[80];

  for(int i=0;i<Xrow;i++)
    {
      for(int ii=0;ii<Xcol;ii++)
{
 if(fld=='F' || fld=='C')
   {
     if(i!= 0 && i!=Xrow-1 && ii!=0 && ii!=Xcol-1)
clcNeigh(num,i,ii,fld);
     else
num[i][ii]='+';}
 line[ii]=num[i][ii];
 cout << line[ii];
}
      cout << '\n';
    }
}

void runGame(int num[][xCol],int &xrow, int &xcol, char fld)
{
  bool flgx=false;

  if(xrow<Xrow && xcol<Xcol)
    {
      for(int i=0;i<Xrow;i++)
{
 for(int ii=0;ii<Xcol;ii++)
   {
     if(i!= 0 && i!=Xrow-1 && ii!=0 && ii!=Xcol-1)
{
 switch(fld)
   {
   case 'S': case 's': case '1': case '2':
     if(ii==xcol && i==xrow && !flgx)
{
 clcNeigh(num,xrow,xcol,fld);
 flgx=true;
}
     break;

   case 'R': case 'r':
     clcNeigh(num,i,ii,fld);
     break;
   }
}
   }
}
      switch(fld)
{
case 'S': case 's': case '1': case '2':
 xcol=xcol+1;
 if(xcol>Xcol-2)
   {
     xcol=1;
     if(xrow<Xrow-2)
{
 xrow=xrow+1;
}
     else
xrow=1;
   }
}
    }
}

void clcNeigh(int num[][xCol],int &xrow, int &xcol, char fld)
{
  switch(fld)
    {
    case 'C': case 'c': case '2':
      num[xrow][xcol]=' ';
      break;

    case 'F': case 'f': case '1':
      num[xrow][xcol]='*';
break;

    case 'R': case 'r':
      char srchArray[8];
      srchArray[0]=num[xrow-1][xcol-1];
      srchArray[1]=num[xrow][xcol-1];
      srchArray[2]=num[xrow+1][xcol-1];
      srchArray[3]=num[xrow-1][xcol];
      srchArray[4]=num[xrow+1][xcol];
      srchArray[5]=num[xrow-1][xcol+1];
      srchArray[6]=num[xrow][xcol+1];
      srchArray[7]=num[xrow+1][xcol+1];

      int xHit=0;

      for(int x=0;x<8;x++)
{
 if(srchArray[x]=='*')
   {
     xHit=xHit+1;
   }
}

      switch(xHit)
{
case 3:
 num[xrow][xcol]='*';
 break;

case 2:
 // Nothing
 break;

default:
 num[xrow][xcol]=' ';
 break;
}
      break;
    }
}

void chkTest(char fld[])
{
  char chkTest;
  cout << fld << endl;
  cin >> chkTest;
}

No hay comentarios.:

Publicar un comentario