Advertisement
Guest User

Untitled

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