Guest User

Untitled

a guest
Oct 15th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. /* Life.java
  2.  * Mark Schmele
  3.  * Creates a matrix of boolean values to represent life in an area of a grid
  4.  * 9/2/2012
  5.  */
  6.  
  7. import java.util.*;
  8. public class Life {
  9.    
  10.     public static void main(String args[]){
  11.         Scanner console = new Scanner(System.in);
  12.         int rows = console.nextInt();
  13.         int columns = console.nextInt();
  14.         long seed = console.nextLong();
  15.        
  16.         if(rows <= 0 || columns <= 0){
  17.             System.out.println("Please use positive numbers.");
  18.         }else{
  19.             makeMatrix(rows, columns, seed);
  20.         }
  21.                
  22.     }
  23.  
  24.    
  25.     // declares the matrix and fills it with booleans, then prints it
  26.     public static void makeMatrix(int rows, int columns, long seed){
  27.        
  28.         //border
  29.         boolean [][] liveGrid = new boolean[rows][columns];
  30.         for(int i = 0; i <= liveGrid.length - 1; i++){
  31.             for(int j = 0; j <= liveGrid[0].length - 1; j++){
  32.                 liveGrid[i][j] = false;
  33.             }
  34.         }
  35.         //insides
  36.         for(int ii = 1; ii <= liveGrid.length - 2; ii++){
  37.             for(int jj = 1; jj <= liveGrid[1].length - 2; jj++){
  38.                 liveGrid[ii][jj] = isLive();
  39.             }
  40.         }
  41.        
  42.         for(int i = 0; i <= liveGrid.length - 1; i++){
  43.             for(int j = 0; j<= liveGrid[0].length - 1; j++){
  44.                 if(liveGrid [i][j]){
  45.                     System.out.print("# ");
  46.                 }else{
  47.                     System.out.print("- ");
  48.                 }
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53.    
  54.     // returns a random boolean value
  55.     public static boolean isLive(){
  56.         Random rand = new Random();
  57.         return rand.nextBoolean();
  58.     }
  59. }
Add Comment
Please, Sign In to add comment