Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JeuDeLaVieUnidim {
- static int columns = 24;
- static int[] board = new int[columns ];
- static int[] next = new int[columns ];
- static int countCata =0;
- static int countStable = 0;
- public static int getRandomNumber(int max){
- double x = (Math.random() * max );
- return (int)Math.floor(x);
- }
- public static double getRandomNumber(){
- double x = Math.random() ;
- return x;
- }
- static void init(double p) {
- for (int i = 0; i < columns; i++) {
- // Filling the rest randomly
- double r =getRandomNumber();
- if (r < p)
- board[i] = 1;
- else
- board[i] = 0;
- next[i] = 0;
- }
- }
- static void initWithZeros() {
- for (int i = 0; i < columns; i++) {
- // Filling the rest randomly
- board[i] = 0;
- next[i] = 0;
- }
- }
- static void printTheArray(int arr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- System.out.print(arr[i]+" ");
- }
- System.out.println();
- }
- // Function to generate all binary strings
- static void generateAllBinaryStrings(int n, int arr[], int i)
- {
- if (i == n)
- {
- generate();
- if (isCatastrophic()) {
- countCata++;
- printTheArray(arr, n);
- }
- if (isStable()) {
- countStable++;
- //printTheArray(arr, n);
- }
- return;
- }
- // First assign "0" at ith position
- // and try for all other permutations
- // for remaining positions
- arr[i] = 0;
- generateAllBinaryStrings(n, arr, i + 1);
- // And then assign "1" at ith position
- // and try for all other permutations
- // for remaining positions
- arr[i] = 1;
- generateAllBinaryStrings(n, arr, i + 1);
- }
- static void generate() {
- // Loop through every spot in our 2D array and check spots neighbors
- for (int x = 0; x < columns ; x++) {
- // Add up all the states in a 3x3 surrounding grid
- int neighbors = 0;
- if(x==0){
- neighbors += board[1]+ board[0] + board[columns-1];
- }else if (x==columns-1){
- neighbors += board[columns-2] + board[columns -1] + board[0];
- }else{
- for (int i = -1; i <= 1; i++) {
- neighbors += board[x+i];
- }
- }
- // A little trick to subtract the current cell's state since
- // we added it in the above loop
- neighbors -= board[x];
- // Rules of Life
- if ((board[x] == 1) && (neighbors >= 1))
- next[x] = 0; // Loneliness
- else if ((board[x] == 0) && (neighbors == 1))
- next[x] = 1; // Reproduction
- else
- next[x] = board[x]; // Stasis
- }
- }
- static boolean isCatastrophic(){
- for (int i=0; i<columns; i++){
- if (next[i]==1){
- return false;
- }
- }
- return true;
- }
- static boolean isStable(){
- for (int i =0;i<columns;i++){
- if (board[i]!=next[i]){
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- // Pour compter le nombre de situations stable et catastrophique en 400 millions de génération aléatoires
- // for (int i=0; i<400000000 ;i++){
- // init(0.5);
- // generate();
- // if (isCatastrophic()) {
- // countCata++;
- // }
- // if (isStable()) {
- // countStable++;
- // }
- // }
- generateAllBinaryStrings(columns, board,0);
- System.out.println(countStable);
- System.out.println(countCata);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement