All pastes #537176 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-06-04 17:32 UTC
rendered paste body
#include <allegro.h>
#include <stdlib.h>
#include <time.h>
#
//#include <iostream>
//using namespace std ;
BITMAP *bufor_ekranu = NULL ;


const char * the_dialog[] = { "Zycie", "Marsz", "nico" ,"Inwazja" }; ;

void rysuj(int tab[][250][250], int wys, int szer, int bufor, int flaga) ;

void zamaluj(int tab[][250][250], int wys, int szer, int x, int y, int bufor, int cos, int flaga) {

int xx = (int) (x-50) / (int)(500 / szer); //x i y jest na odwrot :P
int yy = (int) (y-50) / (int)(500 / wys);

//if(tab[bufor][yy][xx]==2) tab[bufor][yy][xx]=0 ;     
//else 

tab[bufor][yy][xx]=cos;
tab[bufor][yy-1][xx]=cos;
tab[bufor][yy+1][xx]=cos;
tab[bufor][yy][xx+1]=cos;
tab[bufor][yy][xx-1]=cos;
tab[bufor][yy+1][xx+1]=cos;
tab[bufor][yy+1][xx-1]=cos;
tab[bufor][yy-1][xx+1]=cos;
tab[bufor][yy-1][xx-1]=cos;
rysuj(tab,wys,szer,bufor,flaga);     
};     
/*
0 - martwa ;
1 - aktywna ;
2 - subaktywna ;

*/
void zeruj(int tab[][250][250], int wys, int szer) {
  
        for(int i=0;i<wys;i++)
        for(int j=0;j<szer;j++)  
             tab[0][i][j]=0;
             
             int y = wys / 2 ;
             int x = szer / 2 ;
         
             for(int i=y-2;i<=y+2;i++)
                for(int j=x-3;j<=x+3;j++)
                        tab[0][i][j]=1 ;
}
void generuj(int tab[][250][250], int wys, int szer, int flaga) {
     int cos = 2 ;
     if(flaga==4) cos = 3 ;
        for(int i=0;i<wys;i++)
        for(int j=0;j<szer;j++)  
             tab[0][i][j]=rand()% cos ;
         
}
void zycie(int tab[][250][250], int wys, int szer, int &bufor, int flaga) {
int nowy ;
if(bufor==1) nowy = 0 ;
else nowy = 1 ;
int liczniksub = 0 ;
int licznik =0;
int l,p,g,d ;

 for(int i=0;i<wys;i++)
   for(int j=0;j<szer;j++) {   
    
    if(j-1<0) l=j;
    else l=j-1;
    if(j+1==szer) p=j ;
    else p=j+1 ;
    if(i-1<0) g=i ;
    else g=i-1 ;
    if(i+1==wys) d=i ;
    else d=i+1 ;
    for(int k=g;k<=d;k++) 
      for(int t=l;t<=p;t++)
      {
      if(tab[bufor][k][t]==1) licznik++ ;
      if(flaga==4) {
      if(tab[bufor][k][t]==2) liczniksub++ ;
      }
      }
    


 if(flaga==1) {
    licznik-=tab[bufor][i][j];
  
if(licznik==3) tab[nowy][i][j]=1 ;
    else if(licznik==2 && tab[bufor][i][j]==1)  tab[nowy][i][j]=1 ;
    else tab[nowy][i][j]=0 ;
 
 


    
   }
 else if(flaga==2) {
    licznik-=tab[bufor][i][j];  
    if(licznik==2 && tab[bufor][i][j]==0) tab[nowy][i][j]=1 ;
    else if(licznik==3)  tab[nowy][i][j]=1 ;
    else tab[nowy][i][j]=0 ; //dziwne cos :P
    
      
      }
     
        
  else if(flaga==4) {
       if(tab[bufor][i][j]==2) liczniksub--;
       else if(tab[bufor][i][j]==1) licznik--;
    /*    if(tab[bufor][i][j]==1 &&(licznik>=3 || liczniksub>=3)) tab[nowy][i][j]=2 ; 
       else if(tab[bufor][i][j]==0 &&(licznik>=3 || liczniksub>=3)) tab[nowy][i][j]=1 ;
       else if(tab[bufor][i][j]==1 &&(licznik==1 || licznik==2 || liczniksub==1 || liczniksub==2)) tab[nowy][i][j]=1 ;
       else tab[nowy][i][j]=0 ;  */
       if(tab[bufor][i][j]==1 &&(licznik+liczniksub>=3)) tab[nowy][i][j]=2 ; 
       else if(tab[bufor][i][j]==0 &&(licznik+liczniksub>=3)) tab[nowy][i][j]=1 ;
       else if(tab[bufor][i][j]==1 &&(licznik+liczniksub==1 || licznik+liczniksub==2)) tab[nowy][i][j]=1 ;
       else tab[nowy][i][j]=0 ;       
      
       liczniksub=0 ; 
       }

      
licznik = 0 ;

}
bufor=nowy ;
return ;
}

void rysuj(int tab[][250][250], int wys, int szer, int bufor, int flaga) {

int x=50 ;
int y=50 ;
int rozmiar_x = 500 / szer ;
int rozmiar_y = 500 / wys ;
int color ;

for(int i=0;i<wys;i++)
   for(int j=0;j<szer;j++) {
           if(tab[bufor][i][j]==0) color=makecol(0,0,0);
           else if(tab[bufor][i][j]==1) color=makecol(35,75,255);
           else  color=makecol(255,170,0);
   
           rectfill(bufor_ekranu, x+rozmiar_x*j, y+i*rozmiar_y, x+rozmiar_x*j+rozmiar_x, y+i*rozmiar_y+rozmiar_y, color);
   }   
  

blit(bufor_ekranu,screen,0,0,0,0,600,600);

}
void init();
void deinit();

int main() {
   time_t t;
   srand((unsigned)time(&t));
	

int tab[2][250][250] ;

int wys;
int szer ;
int max_szer = 80;
int max_wys = 80 ;/*
cout << "\nPodaj wysokosc siatki (1 - " << max_wys << ") : " << endl ;
cin >> wys ;
cout << "\nPodaj szerokosc siatki (1 - " << max_szer << ") : " << endl ;
cin >> szer ;
if(wys > max_wys ) wys = max_wys ;
if(wys <= 0 ) wys = 1 ; 
if(szer > max_szer ) szer = max_szer ;
if(szer <= 0 ) szer = 1 ;*/

wys = 250 ;
szer = 250;
int bufor = 0 ;
int flaga = 1 ;
int colorek ;
generuj(tab,wys,szer,2);
init();






	while (!key[KEY_ESC]) {
        clear_bitmap(bufor_ekranu);
        
      //  do_dialog(the_dialog,-1);
       // if(bufor==1)  colorek = makecol(255,0,0) ;
         colorek = makecol(0,255,0);
     
      
        textprintf_ex(bufor_ekranu, font, 150, 30, colorek,-1,"mouse_x = %-5d", mouse_x);
        textprintf_ex(bufor_ekranu, font, 270, 30, colorek,-1,"mouse_y = %-5d", mouse_y);
		rysuj(tab,wys,szer,bufor, flaga);
		
              if(key[KEY_ESC]) return 1;
              
        if(key[KEY_B]) zycie(tab,wys,szer,bufor,flaga);
		if(key[KEY_A]) {zeruj(tab,wys,szer);bufor=0;}
//menu
	if(key[KEY_1]) {flaga = 1 ; zeruj(tab,wys,szer);bufor=0;rysuj(tab,wys,szer,bufor,flaga);}
    if(key[KEY_2]) {flaga = 2 ; zeruj(tab,wys,szer);bufor=0;rysuj(tab,wys,szer,bufor,flaga);}
    if(key[KEY_3]) {flaga = 4 ; zeruj(tab,wys,szer);bufor=0;rysuj(tab,wys,szer,bufor,flaga);}
   
    if(key[KEY_9]) {generuj(tab,wys,szer,flaga);bufor=0;rysuj(tab,wys,szer,bufor,flaga);}
    if(key[KEY_0]) {zeruj(tab,wys,szer);bufor=0;rysuj(tab,wys,szer,bufor,flaga);}
    
    if((mouse_x > 50 && mouse_x < 550) && (mouse_y > 50 && mouse_y < 550) && mouse_b & 1) {zamaluj(tab, wys, szer, mouse_x, mouse_y, bufor, 1, flaga);}
    if((mouse_x > 50 && mouse_x < 550) && (mouse_y > 50 && mouse_y < 550) && mouse_b & 2) {zamaluj(tab, wys, szer, mouse_x, mouse_y, bufor, 0, flaga);}
    if(key[KEY_O] && szer > 20) {szer-=10;wys-=10;rysuj(tab,wys,szer,bufor,flaga);}
    if(key[KEY_P] && szer < 220) {szer+=10;wys+=10;rysuj(tab,wys,szer,bufor,flaga);}
    if(key[KEY_K] && wys > 20) {wys-=10;}
    if(key[KEY_L] && wys < 220) {wys+=10;}
    }

	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 600, 600, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

if(!(bufor_ekranu = create_bitmap(600,600)))
{
set_gfx_mode(GFX_TEXT,0,0,0,0);
allegro_message("Zamalo pamieci potrzebnej do stworzenia bufora ekranu !\n");
allegro_exit();

}
	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
	 show_mouse(screen);
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}