Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Grapic.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace grapic;
- const int DIMW = 900;
- int intval (float x) { return x; }
- void init (bool World[DIMW][DIMW]) {
- int quotient, reste, cons = DIMW/20;
- for (int i = 0; i < DIMW*DIMW/100; i++) {
- quotient = i*10/DIMW;
- reste = i - (quotient * DIMW / 10);
- World[quotient][reste] = rand() % 2;
- }
- }
- void update (bool World[DIMW][DIMW]) {
- bool tmp[DIMW][DIMW];
- int quotient, reste, somme;
- for (int i = 0; i < DIMW*DIMW/100; i++) {
- quotient = i*10/DIMW;
- reste = i - (quotient * DIMW / 10);
- somme = 0;
- if ( quotient > 0 and reste > 0 ) somme = World[quotient-1][reste-1];
- if ( quotient > 0 ) somme += World[quotient-1][reste];
- if ( quotient > 0 and reste < DIMW/10 ) somme += World[quotient-1][reste+1];
- if ( reste > 0 ) somme += World[quotient][reste-1];
- if ( reste < DIMW/10 ) somme += World[quotient][reste+1];
- if ( quotient < DIMW/10 and reste > 0 ) somme += World[quotient+1][reste-1];
- if ( quotient < DIMW/10 ) somme += World[quotient+1][reste];
- if ( quotient < DIMW/10 and reste < DIMW/10 ) somme += World[quotient+1][reste+1];
- tmp[quotient][reste] = (abs(somme-2.5) <= 0.75)*World[quotient][reste] + (somme == 3)*(!World[quotient][reste]);
- }
- for (int i = 0; i < DIMW*DIMW/100; i++) {
- quotient = i*10/DIMW;
- reste = i - (quotient * DIMW / 10);
- World[quotient][reste] = tmp[quotient][reste];
- }
- }
- void draw (bool World[DIMW][DIMW]) {
- int quotient, reste, somme;
- for (int i = 0; i < DIMW*DIMW/100; i++) {
- quotient = i*10/DIMW;
- reste = i - (quotient * DIMW / 10);
- color(255,255,255,255*World[quotient][reste]);
- rectangleFill(quotient*10,reste*10,quotient*10+10,reste*10+10);
- }
- }
- int main(int, char**) {
- bool stop = false;
- int del = 50;
- srand(time(NULL));
- winInit("Jeu de la vie",DIMW,DIMW);
- backgroundColor(0,0,0);
- bool World[DIMW][DIMW];
- initialisation:
- init(World);
- while (!stop) {
- winClear();
- update(World);
- draw(World);
- stop = winDisplay();
- delay(del);
- if (isKeyPressed(SDLK_SPACE))
- goto initialisation;
- if (isKeyPressed(SDLK_DOWN))
- del += 5;
- if (isKeyPressed(SDLK_UP) and del >= 5)
- del -= 5;
- color(255,255,255);
- }
- winQuit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement