другой выход в игре Conways жизни - PullRequest
0 голосов
/ 19 ноября 2018

Я должен создать Java-программу для Conway's Game Of Life в процедурной манере, и я только в одном шаге от финиша - все, что мне осталось сделать, - это выяснить, как изменить сам вывод (что-то вроде GIF). ). Вместо этого моя программа выводит все результаты один за другим. Я полагаю, что это должна быть какая-то петля, но я не уверен. Вот код:

import java.util.Random;                                                                   
import java.util.Scanner;                                                                  

class gameOfLife {                                                                         
public static void main(String[] args) {                                               

    //User input                                                                       

    Scanner in = new Scanner(System.in);                                               
    System.out.println("How many rows?");                                              
    int rows = in.nextInt();                                                           
    System.out.println("How many columns?");                                           
    int cols = in.nextInt();                                                           

    //Declaring variables and grids                                                    

    int[][] grid = new int[rows][cols];                                                
    int[][] nextGrid = new int[rows][cols];                                            
    int[][] temp = new int [rows][cols];                                               

    //Initializing first generation                                                    

    initiateGrid(grid);                                                                
    printGameBoard(grid);                                                              

    //Looping through 10 generations                                                   

    for (int x = 0; x < 20; x++) {                                                     
        applyTheRules(grid, rows, cols, nextGrid);                                     
        temp = nextGrid;                                                               
        nextGrid = grid;                                                               
        grid = temp;                                                                   
        printGameBoard(grid);                                                          
    }                                                                                  
}                                                                                      

//Initiating first generation grid randomly                                            

static void initiateGrid(int[][] grid) {                                               
    Random r = new Random();                                                           
    for (int i = 0; i < grid.length; i++) {                                            
        for (int j = 0; j < grid[i].length; j++) {                                     
            grid[i][j] = r.nextInt(2);                                                 
        }                                                                              
    }                                                                                  
}                                                                                      

//Printing out the game board                                                          

static void printGameBoard(int[][] grid) {                                             
    for (int i = 0; i < grid.length; i++) {                                            
        for (int j = 0; j < grid[i].length; j++) {                                     
            if (grid[i][j] == 0)                                                       
                System.out.print(" . ");                                               
            else                                                                       
                System.out.print(" ■ ");                                               
        }                                                                              
        System.out.println();                                                          
    }                                                                                  
    System.out.println();                                                              
}                                                                                      

//Applying the rules of the game                                                       

static void applyTheRules(int [][] grid, int rows, int cols, int [][] nextGrid) {      
    for (int i = 0; i < rows; i++) {                                                   
        for (int j = 0; j < cols; j++) {                                               
            int count = 0;                                                             
            if(i-1>=0 && i+1<grid.length && j-1>=0 && j+1<grid[i].length) {            
                for (int x = -1; x <= 1; x++) {                                        
                    for (int y = -1; y <= 1; y++) {                                    
                        count += grid[i + x][j + y];                                   
                    }                                                                  
                }                                                                      
                count -= grid[i][j];                                                   

            } else{                                                                    
                     for (int x = -1; x <= 1; x++) {                                   
                         for (int y = -1; y <= 1; y++) {                               
                             count += 0;                                               
                         }                                                             
                     }                                                                 
                }                                                                      

            //Alive cell becomes dead, if there are more than                          
            //3 or less than 2 neighbouring cells                                      

            if((grid[i][j]==1)&&(count<2)||(count>3))                                  
                nextGrid[i][j]=0;                                                      

            //Dead cell becomes alive if there are exactly 3 neighbouring cells        

            else if((grid[i][j]==0)&&(count==3))                                       
                nextGrid[i][j]=1;                                                      

            //State stays the same                                                     

            else                                                                       
                nextGrid[i][j]=grid[i][j];                                             
        }                                                                              
    }                                                                                  
}                                                                                                                                                 

}

...