Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
17.04.2013
Размер:
10.47 Кб
Скачать
#include "mouse.h"
#include <graphics.h>
#include <bios.h>
#include <dos.h>

#define ESC 27
#define ENTER 13
#define PROBEL 32

 struct cell
	      {
	      int x;           //X-coordinate of the cell
	      int y;           //Y-coordinate of the cell
	      int init;        //dead or alive
	      }  mas[60][45],mas1[60][45];

int generation;
int N,              //length of the field
    M;              //width of the field

void init_cell();
void init_screen();
void redact_cell();
void xor_cell(int ex,int ey);
int  begin_life();
void copy_mas(struct cell m1[60][45],struct cell m2[60][45]);


//procedure of the initialization of the cells
void init_cell()
{
int i=0,j=0,k,m;
setcolor(YELLOW);
randomize();
//life of the cells inserted at random
for(m=0;m<M;m++,j+=10)
  {
	for(k=0;k<N;k++,i+=10)
	{
		   mas[k][m].x=55+i;
		   mas[k][m].y=55+j;
		   mas[k][m].init=rand()%2;
	}
   i=0;
  }

//show alive sells
for( m=0;m<M;m++)
for( k=0;k<N;k++)
	if (mas[k][m].init==1)
	ellipse(mas[k][m].x,mas[k][m].y,0,360,4,4);
}

void init_screen()
{
//size of the field
printf("Enter size of the game field \n");
printf("lengthwise (up to 55):  ");
scanf("%d",&N);
printf("in width (up tu 40): ");
scanf("%d",&M);
clrscr();
///////////////////initialization of the graphic regime///////////////
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "d:\\progra~1\\bc\\bgi");

/* read result of initialization */
errorcode = graphresult();

if (errorcode != grOk)  /* an error occurred */
{
   printf("Graphics error: %s\n", grapherrormsg(errorcode));
   printf("Press any key to halt:");
   getch();
   exit(1);             /* return with error code */
}


//draw upper line
setfillstyle(SOLID_FILL,BLUE);
bar(1,20,679,35);
outtextxy(5,25,"    Generation: ");
outtextxy(250,25,"ESC - exit");
outtextxy(390,25,"ENTER - look at the evolution");

//draw field limits
setfillstyle(EMPTY_FILL,0);
rectangle(50,50,50+(N*10),50+(M*10));

//initialization of the begining state of the cells
init_cell();

//initialization of the mouse
//it can't go out of the field
initmouse();
showmouse();
mouselimit(51,51,(N*10)+49,(M*10)+49);
}

void xor_cell(int ex,int ey)
{
  int k=0,m=0;
  for(int i=50;i<(50+N*10);i+=10,k++) if(ex<i) break;
  for(int j=50;j<(50+M*10);j+=10,m++) if(ey<j) break;
  k--;m--;

  if (mas[k][m].init==0)
	{
	  setcolor(YELLOW);
	  hidemouse();
	  ellipse(mas[k][m].x,mas[k][m].y,0,360,4,4);
	  showmouse();
	  mas[k][m].init=1;
	}
   else
	{
	  setwritemode(XOR_PUT);
	  setcolor(0);
	  hidemouse();
	  ellipse(mas[k][m].x,mas[k][m].y,0,360,4,4);
	  showmouse();
	  mas[k][m].init=0;
	  setwritemode(COPY_PUT);
	  hidemouse();
	  ellipse(mas[k][m].x,mas[k][m].y,0,360,4,4);
	  showmouse();
	  setcolor(YELLOW);
	}
}

void copy_mas(struct cell m1[60][45],struct cell m2[60][45])
{
  for(int i=0;i<N;i++)
	for(int j=0;j<M;j++)
	{
		   m2[i][j].x=m1[i][j].x;
		   m2[i][j].y=m1[i][j].y;
		   m2[i][j].init=m1[i][j].init;
	}
}

int begin_life()
{
  int count=0;
  char  ss[10];
  hidemouse();
//draw upper line
setfillstyle(SOLID_FILL,BLUE);
bar(1,20,679,35);
setcolor(WHITE);
outtextxy(5,25,"    Generation: ");
outtextxy(250,25,"ESC - exit");
outtextxy(390,25,"SPASE - edit evolution");
  union inkey {
	       char ch[2];
	       int i;
			  } c;

//endless cycle,in which keyboard adn mouse are asked
for(;;)
  {
   //if a key pressed,then ESC check
   if (bioskey(1))
	      {
		  c.i=bioskey(0);
		  if (c.ch[0]==ESC)
		    { setfillstyle(SOLID_FILL,BLUE);
		      bar(190,200,410,260);
		      setfillstyle(SOLID_FILL,YELLOW);
		      bar(195,205,405,255);
		      setcolor(BLACK);
		      outtextxy(240,210,"Congralution!");
		      itoa(generation,ss,10);
		      outtextxy(210,225,"You cells have lived :");
		      outtextxy(240,240,ss);
		      outtextxy(265,240,"generations!");
		      getch();
		      exit(1);}
		  if (c.ch[0]==PROBEL)
			 {
			 setfillstyle(SOLID_FILL,BLUE);
			 bar(1,20,679,35);
			 setcolor(WHITE);
			 outtextxy(5,25,"    Generation: ");
			 outtextxy(250,25,"ESC - exit");
			 outtextxy(390,25,"ENTER - look at the evolution    ");
			 showmouse(); return 0;
			 }
	      }

//initialization of the subsidiary massif
  copy_mas(mas,mas1);
  int N1=N-1,M1=M-1;
//data about new generation are put in to the mas1
  for(int i=0;i<N;i++)
	for(int j=0;j<M;j++)
	{
	 count=0;
//////1) corner cells/////////////////////////////
	 if ((mas[i][j].init==1)  && (i==0) && (j==0))
	    {count=mas[i][j+1].init+mas[i+1][j+1].init+
	    +mas[i+1][j].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i==0) && (j==0))
	    {count=mas[i][j+1].init+mas[i+1][j+1].init+
	    +mas[i+1][j].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (i==N1) && (j==0))
	    {count=mas[i-1][j].init+mas[i-1][j+1].init+
	     mas[i-1][j].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i==N1) && (j==0))
	    {count=mas[i-1][j].init+mas[i-1][j+1].init+
	     mas[i-1][j].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (i==N1) && (j==M1))
	    {count=mas[i-1][j-1].init+mas[i-1][j].init+
	     mas[i][j-1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i==N1) && (j==M1))
	    {count=mas[i-1][j-1].init+mas[i-1][j].init+
	     mas[i][j-1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (i==0) && (j==M1))
	    {count=mas[i+1][j].init+mas[i][j-1].init+
	     mas[i+1][j-1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i==0) && (j==M1))
	    {count=mas[i+1][j].init+mas[i][j-1].init+
	     mas[i+1][j-1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
//////2) regional cells///////////////////////////
	if ((mas[i][j].init==1)  && (i>0) && (i<N1)  && (j==0))
	    {count=mas[i-1][j].init+mas[i-1][j+1].init+
	     mas[i][j+1].init+mas[i+1][j].init+
	     mas[i+1][j+1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i>0) && (i<N1)  && (j==0))
	    {count=mas[i-1][j].init+mas[i-1][j+1].init+
	     mas[i][j+1].init+mas[i+1][j].init+
	     mas[i+1][j+1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (j>0) && (i==N1)  && (j<M1))
	    {count=mas[i][j-1].init+mas[i][j+1].init+
	     mas[i-1][j+1].init+mas[i-1][j].init+
	     mas[i-1][j-1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (j>0) && (i==N1)  && (j<M1))
	    {count=mas[i][j-1].init+mas[i][j+1].init+
	     mas[i-1][j+1].init+mas[i-1][j].init+
	     mas[i-1][j-1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (i>0) && (i<N1)  && (j==M1))
	    {count=mas[i-1][j-1].init+mas[i][j-1].init+
	     mas[i+1][j-1].init+mas[i-1][j].init+
	     mas[i+1][j].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (i>0) && (i<N1)  && (j==M1))
	    {count=mas[i-1][j-1].init+mas[i][j-1].init+
	     mas[i+1][j-1].init+mas[i-1][j].init+
	     mas[i+1][j].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

	if ((mas[i][j].init==1)  && (j>0) && (i==0)  && (j<M1))
	    {count=mas[i][j-1].init+mas[i][j+1].init+
	     mas[i+1][j-1].init+mas[i+1][j].init+
	     mas[i+1][j+1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
	if ((mas[i][j].init==0)  && (j>0) && (i==0)  && (j<M1))
	    {count=mas[i][j-1].init+mas[i][j+1].init+
	     mas[i+1][j-1].init+mas[i+1][j].init+
	     mas[i+1][j+1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }

//////3) central cells///////////////////////////////
	if ((mas[i][j].init==1)  && (j>0) && (i>0)  && (j<M1) && (i<N1))
	    {count=mas[i-1][j-1].init+mas[i][j-1].init+
	     mas[i+1][j-1].init+mas[i-1][j].init+
	     mas[i+1][j].init+mas[i-1][j+1].init+
	     mas[i][j+1].init+mas[i+1][j+1].init;
	     if ((count==2) || (count==3)) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	     }
	if ((mas[i][j].init==0)  && (j>0) && (i>0)  && (j<M1) && (i<N1))
	    {count=mas[i-1][j-1].init+mas[i][j-1].init+
	     mas[i+1][j-1].init+mas[i-1][j].init+
	     mas[i+1][j].init+mas[i-1][j+1].init+
	     mas[i][j+1].init+mas[i+1][j+1].init;
	     if (count==3) mas1[i][j].init=1;
	     else mas1[i][j].init=0;
	    }
    }
copy_mas(mas1,mas);
for( int m=0;m<M;m++)
for( int k=0;k<N;k++)
	{
	if (mas[k][m].init==1) setcolor(YELLOW);
	else setcolor(0);
	ellipse(mas[k][m].x,mas[k][m].y,0,360,4,4);
	}
 generation++;
 setfillstyle(SOLID_FILL,BLUE);
 bar(130,20,160,35);
 setcolor(WHITE);
 itoa(generation,ss,10);
 outtextxy(140,25,ss);
 delay(300);
 }
}

void redact_cell()
{
  int x1,y1;
//subsidiary strusture,which helps to define code of the pressed key
  union inkey {
	       char ch[2];
	       int i;
			  } c;

//endless cycle,in which keyboard adn mouse are asked
for(;;)
  {
   //polling of the keyboard and the mouse
   if (bioskey(1))
	      {
		  c.i=bioskey(0);
		  if (c.ch[0]==ESC)
		   {setfillstyle(SOLID_FILL,BLUE);
		    bar(190,200,410,260);
		    setfillstyle(SOLID_FILL,YELLOW);
		    bar(195,205,405,255);
		    setcolor(BLACK);
		    outtextxy(250,215,"You lose!");
		    outtextxy(200,235,"You didn't breed any cell!");
		    getch();
		    exit(1);}
		  if (c.ch[0]==ENTER) begin_life();
	      }
   if(mousepressed()!=0)
	      {
		  x1=mousegetx();
		  y1=mousegety();
		  //draw or eraze electeed cell
		   while (mousepressed()!=0); xor_cell(x1,y1);
	      }
  }
}
Соседние файлы в папке LIFECELL