Advertisement
apexsquirt

[C++ / GrAPic] Conway's Game of Life

Feb 21st, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <Grapic.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. using namespace grapic;
  8. const int DIMW = 900;
  9.  
  10. int intval (float x) { return x; }
  11.  
  12. void init (bool World[DIMW][DIMW]) {
  13.     int quotient, reste, cons = DIMW/20;
  14.     for (int i = 0; i < DIMW*DIMW/100; i++) {
  15.         quotient = i*10/DIMW;
  16.         reste = i - (quotient * DIMW / 10);
  17.         World[quotient][reste] = rand() % 2;
  18.     }
  19. }
  20.  
  21. void update (bool World[DIMW][DIMW]) {
  22.     bool tmp[DIMW][DIMW];
  23.     int quotient, reste, somme;
  24.     for (int i = 0; i < DIMW*DIMW/100; i++) {
  25.         quotient = i*10/DIMW;
  26.         reste = i - (quotient * DIMW / 10);
  27.         somme = 0;
  28.         if ( quotient > 0        and reste > 0       )  somme  = World[quotient-1][reste-1];
  29.         if ( quotient > 0                            )  somme += World[quotient-1][reste];
  30.         if ( quotient > 0        and reste < DIMW/10 )  somme += World[quotient-1][reste+1];
  31.         if (                         reste > 0       )  somme += World[quotient][reste-1];
  32.         if (                         reste < DIMW/10 )  somme += World[quotient][reste+1];
  33.         if ( quotient < DIMW/10  and reste > 0       )  somme += World[quotient+1][reste-1];
  34.         if ( quotient < DIMW/10                      )  somme += World[quotient+1][reste];
  35.         if ( quotient < DIMW/10  and reste < DIMW/10 )  somme += World[quotient+1][reste+1];
  36.         tmp[quotient][reste] = (abs(somme-2.5) <= 0.75)*World[quotient][reste] + (somme == 3)*(!World[quotient][reste]);
  37.     }
  38.     for (int i = 0; i < DIMW*DIMW/100; i++) {
  39.         quotient = i*10/DIMW;
  40.         reste = i - (quotient * DIMW / 10);
  41.         World[quotient][reste] = tmp[quotient][reste];
  42.     }
  43. }
  44.  
  45. void draw (bool World[DIMW][DIMW]) {
  46.     int quotient, reste, somme;
  47.     for (int i = 0; i < DIMW*DIMW/100; i++) {
  48.         quotient = i*10/DIMW;
  49.         reste = i - (quotient * DIMW / 10);
  50.         color(255,255,255,255*World[quotient][reste]);
  51.         rectangleFill(quotient*10,reste*10,quotient*10+10,reste*10+10);
  52.     }
  53. }
  54.  
  55. int main(int, char**) {
  56.  
  57.     bool stop = false;
  58.     int del = 50;
  59.     srand(time(NULL));
  60.  
  61.     winInit("Jeu de la vie",DIMW,DIMW);
  62.     backgroundColor(0,0,0);
  63.  
  64.     bool World[DIMW][DIMW];
  65.  
  66.     initialisation:
  67.     init(World);
  68.  
  69.     while (!stop) {
  70.         winClear();
  71.         update(World);
  72.         draw(World);
  73.         stop = winDisplay();
  74.         delay(del);
  75.         if (isKeyPressed(SDLK_SPACE))
  76.             goto initialisation;
  77.         if (isKeyPressed(SDLK_DOWN))
  78.             del += 5;
  79.         if (isKeyPressed(SDLK_UP) and del >= 5)
  80.             del -= 5;
  81.         color(255,255,255);
  82.     }
  83.  
  84.     winQuit();
  85.     return 0;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement