Advertisement
Guest User

Programme compte Jeu de la Vie

a guest
Dec 1st, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. public class JeuDeLaVieUnidim {
  2.     static int columns = 24;
  3.     static int[] board = new int[columns ];
  4.     static int[] next = new int[columns ];
  5.     static int countCata =0;
  6.     static int countStable = 0;
  7.  
  8.     public static int getRandomNumber(int max){
  9.         double x = (Math.random() * max );
  10.         return (int)Math.floor(x);
  11.     }
  12.  
  13.     public static double getRandomNumber(){
  14.         double x = Math.random() ;
  15.         return x;
  16.     }
  17.  
  18.     static void init(double p) {
  19.         for (int i = 0; i < columns; i++) {
  20.             // Filling the rest randomly
  21.             double r =getRandomNumber();
  22.             if (r < p)
  23.                 board[i] = 1;
  24.             else
  25.                 board[i] = 0;
  26.             next[i] = 0;
  27.         }
  28.     }
  29.  
  30.     static void initWithZeros() {
  31.         for (int i = 0; i < columns; i++) {
  32.             // Filling the rest randomly
  33.             board[i] = 0;
  34.             next[i] = 0;
  35.         }
  36.     }
  37.  
  38.     static void printTheArray(int arr[], int n)
  39.     {
  40.         for (int i = 0; i < n; i++)
  41.         {
  42.             System.out.print(arr[i]+" ");
  43.         }
  44.         System.out.println();
  45.     }
  46.  
  47.     // Function to generate all binary strings
  48.     static void generateAllBinaryStrings(int n, int arr[], int i)
  49.     {
  50.         if (i == n)
  51.         {
  52.             generate();
  53.             if (isCatastrophic()) {
  54.                countCata++;
  55.                printTheArray(arr, n);
  56.             }
  57.             if (isStable()) {
  58.                 countStable++;
  59.                 //printTheArray(arr, n);
  60.             }
  61.             return;
  62.         }
  63.  
  64.         // First assign "0" at ith position
  65.         // and try for all other permutations
  66.         // for remaining positions
  67.         arr[i] = 0;
  68.         generateAllBinaryStrings(n, arr, i + 1);
  69.  
  70.         // And then assign "1" at ith position
  71.         // and try for all other permutations
  72.         // for remaining positions
  73.         arr[i] = 1;
  74.         generateAllBinaryStrings(n, arr, i + 1);
  75.     }
  76.  
  77.  
  78.     static void generate() {
  79.  
  80.         // Loop through every spot in our 2D array and check spots neighbors
  81.         for (int x = 0; x < columns ; x++) {
  82.             // Add up all the states in a 3x3 surrounding grid
  83.             int neighbors = 0;
  84.             if(x==0){
  85.                 neighbors += board[1]+ board[0] + board[columns-1];
  86.             }else if (x==columns-1){
  87.                 neighbors += board[columns-2] + board[columns -1] + board[0];
  88.             }else{
  89.                 for (int i = -1; i <= 1; i++) {
  90.                     neighbors += board[x+i];
  91.                 }
  92.             }
  93.  
  94.             // A little trick to subtract the current cell's state since
  95.             // we added it in the above loop
  96.             neighbors -= board[x];
  97.             // Rules of Life
  98.             if      ((board[x] == 1) && (neighbors >=  1))
  99.                 next[x] = 0;           // Loneliness
  100.             else if ((board[x] == 0) && (neighbors == 1))
  101.                 next[x] = 1;           // Reproduction
  102.             else
  103.                 next[x] = board[x]; // Stasis
  104.  
  105.         }
  106.  
  107.     }
  108.  
  109.     static boolean isCatastrophic(){
  110.         for (int i=0; i<columns; i++){
  111.             if (next[i]==1){
  112.                 return false;
  113.             }
  114.         }
  115.         return true;
  116.     }
  117.  
  118.     static boolean isStable(){
  119.         for (int i =0;i<columns;i++){
  120.             if (board[i]!=next[i]){
  121.                 return false;
  122.             }
  123.         }
  124.         return true;
  125.     }
  126.  
  127.     public static void main(String[] args) {
  128. //      Pour compter le nombre de situations stable et catastrophique en 400 millions de génération aléatoires
  129. //        for (int i=0; i<400000000 ;i++){
  130. //            init(0.5);
  131. //            generate();
  132. //            if (isCatastrophic()) {
  133. //                countCata++;
  134. //            }
  135. //            if (isStable()) {
  136. //                countStable++;
  137. //            }
  138. //        }
  139.  
  140.  
  141.         generateAllBinaryStrings(columns, board,0);
  142.         System.out.println(countStable);
  143.         System.out.println(countCata);
  144.  
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement