Guest User

Untitled

a guest
Feb 6th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Update {
  4.  
  5.     public final static int ITERATIONS = 5;
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         System.out.println("This program will simulate the game of Life.");
  10.  
  11.         Scanner console = new Scanner(System.in);
  12.  
  13.         System.out.println("Please input the size of your board.");
  14.  
  15.         System.out.println("Rows:");
  16.         final int rows = console.nextInt();
  17.  
  18.         System.out.println("Columns:");
  19.         final int columns = console.nextInt();
  20.  
  21.         System.out.println("Please enter a seed:");
  22.         final long seed = console.nextLong();
  23.  
  24.         int[] birthLive = new int[4];
  25.         boolean[][] board = new boolean[rows][columns];
  26.  
  27.         createMatrix(board, seed);
  28.  
  29.         birthAndLive(birthLive);
  30.  
  31.         for (int i = 0; i < ITERATIONS; i++) {
  32.             printMatrix(board);
  33.             updateMatrix(board, birthLive);
  34.         }
  35.  
  36.     }
  37.  
  38.     public static void createMatrix(boolean[][] board, long seed) {
  39.  
  40.         Random seedBool = new Random(seed);
  41.  
  42.         for (int i = 0; i < board.length; i++) {
  43.             for (int j = 0; j < board[i].length; j++) {
  44.                 board[i][j] = false;
  45.             }
  46.         }
  47.  
  48.         for (int i = 1; i < board.length - 1; i++) {
  49.             for (int j = 1; j < board[i].length - 1; j++) {
  50.                 board[i][j] = seedBool.nextBoolean();
  51.             }
  52.         }
  53.  
  54.     }
  55.  
  56.     public static void birthAndLive(int[] birthLive) {
  57.  
  58.         Scanner console = new Scanner(System.in);
  59.  
  60.         System.out.println("Please input the birth range and the live range:");
  61.  
  62.         System.out.println("Birth (Low):");
  63.         birthLive[0] = console.nextInt();
  64.  
  65.         System.out.println("Birth (High):");
  66.         birthLive[1] = console.nextInt();
  67.  
  68.         System.out.println("Live (Low):");
  69.         birthLive[2] = console.nextInt();
  70.  
  71.         System.out.println("Live (High):");
  72.         birthLive[3] = console.nextInt();
  73.  
  74.     }
  75.  
  76.     public static void printMatrix(boolean[][] board) {
  77.  
  78.         for (int i = 0; i < board.length; i++) {
  79.             for (int j = 0; j < board[i].length; j++) {
  80.                 if (board[i][j] == false)
  81.                     System.out.print(" - ");
  82.                 else
  83.                     System.out.print(" # ");
  84.             }
  85.             System.out.println();
  86.         }
  87.         System.out.println();
  88.     }
  89.  
  90.     public static void updateMatrix(boolean[][] board, int[] birthLive) {
  91.  
  92.         //clone the board so modified values don't affect the results of upcoming spaces
  93.         boolean[][] boardCopy = board.clone();
  94.         for (int i = 0; i < board.length; i++)
  95.             boardCopy[i] = board[i].clone();
  96.  
  97.         int count = 0;
  98.  
  99.         for (int i = 1; i < board.length - 1; i++) {
  100.             for (int j = 1; j < board[i].length - 1; j++) {
  101.  
  102.                 //different requirements for dead or living pieces' living status
  103.                 if (board[i][j] == false) {
  104.  
  105.                     //check the nine-by-nine square, starting at (-1, 1) and ending at (1, -1) where 0 is the affected location
  106.                     // * * *
  107.                     // * 0 *
  108.                     // * * *
  109.                     for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
  110.                         for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
  111.                             if (boardCopy[i][j] == true)
  112.                                 count++;
  113.                         }
  114.                     }
  115.  
  116.                     //check to see what high and low amt of required adjacent lifeforms is
  117.                     if (count >= birthLive[0] && count <= birthLive[1])
  118.                         board[i][j] = true;
  119.                     else
  120.                         board[i][j] = false;
  121.                     count = 0;
  122.  
  123.                 }
  124.  
  125.                 else {
  126.  
  127.                     for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
  128.                         for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
  129.                             if (boardCopy[i][j] == true)
  130.                                 count++;
  131.                         }
  132.                     }
  133.  
  134.                     count -= 1; //For board[i][j] is always true
  135.                     if (count >= birthLive[2] && count <= birthLive[3])
  136.                         board[i][j] = true;
  137.                     else
  138.                         board[i][j] = false;
  139.                     count = 0;
  140.  
  141.                 }
  142.  
  143.             }
  144.         }
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment