aznishboy

Life

Dec 2nd, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.83 KB | None | 0 0
  1. import chn.util.*;
  2. import apcslib.*;
  3. import java.util.*;
  4.  
  5. public class Life {
  6.     public void printTable (int[][] pTable){
  7.         int totalAlive = 0;
  8.         System.out.print("  ");
  9.         for (int i = 1; i <= 2; i++){
  10.             for (int k = 1; k <= 9; k++){
  11.                 System.out.print(k);
  12.             }
  13.             System.out.print("0");
  14.         }
  15.         System.out.println();
  16.         for (int row = 1; row < pTable.length - 1; row++){
  17.             System.out.print(Format.right(row, 2));
  18.             for (int col = 1; col < (pTable[row].length); col++){
  19.                 if(pTable[row][col] == 1){
  20.                     System.out.print("*");
  21.                     totalAlive += 1;
  22.                 }
  23.                 else System.out.print(" ");
  24.             }
  25.             System.out.println();
  26.         }
  27.         System.out.println("Number in Row 10 ---> " +checkRow(pTable, 10));
  28.         System.out.println("Number in Column 10 ---> " + checkCol(pTable, 10));
  29.         System.out.println("Number of living organisms ---> " + totalAlive);
  30.        
  31.     }
  32.     public static int[][] zeroGeneration(FileInput inFile, int[][] currentTable){
  33.         int number = inFile.readInt();
  34.         int row, col;
  35.         while(inFile.hasMoreTokens()){
  36.             row = inFile.readInt();
  37.             col = inFile.readInt();
  38.             currentTable[row][col] = 1;
  39.         }
  40.         return currentTable;
  41.     }
  42.     public static int checkNeighbors(int[][] currentTable, int r, int c){
  43.         int neighbors = 0;
  44.         for (int row = -1; row <= 1; row++){
  45.             for (int col = -1; col <= 1; col++){
  46.                 if (currentTable[r + row][c + col] == 1 && (row != 0 || col != 0))
  47.                     neighbors++;
  48.             }
  49.         }
  50.         return neighbors;
  51.     }
  52.     public static int[][] nextGeneration(int[][]currentTable){
  53.         int[][] scratchTable = new int[22][22];
  54.         for(int row = 1; row <= 20; row++){
  55.             for (int col = 1; col <= 20; col++){
  56.                 if (currentTable[row][col] == 1){
  57.                     if (checkNeighbors(currentTable, row, col) < 2 || checkNeighbors(currentTable, row, col) > 3){
  58.                         scratchTable[row][col] = 0;
  59.                     }
  60.                     else scratchTable[row][col] = 1;
  61.                 }
  62.                 else if(checkNeighbors(currentTable, row, col) == 3){
  63.                     scratchTable[row][col] = 1;
  64.                 }
  65.             }
  66.         }
  67.         return scratchTable;
  68.     }
  69.     public static int checkRow(int[][] currentTable, int cell){
  70.         int rowAlive = 0;
  71.         for (int col = 0; col < currentTable[cell].length; col++){
  72.             if (currentTable[cell][col] == 1){
  73.                 rowAlive += 1;
  74.             }
  75.         }
  76.         return rowAlive;
  77.     }
  78.     public static int checkCol(int[][] currentTable, int cell){
  79.         int colAlive = 0;
  80.         for (int row = 0; row < currentTable.length; row++){
  81.             if (currentTable[row][cell] == 1){
  82.                 colAlive += 1;
  83.             }
  84.         }
  85.         return colAlive;
  86.     }
  87.     public static void main(String[] args) {
  88.         FileInput inFile = new FileInput("life100.txt");
  89.         Life test = new Life();
  90.         int[][] firstTable = new int[22][22];
  91.         firstTable = zeroGeneration(inFile, firstTable);
  92.         int[][] nextTable = new int[22][22];
  93.         nextTable = nextGeneration(firstTable);
  94.         for(int i = 1; i < 5; i++){
  95.             nextTable = nextGeneration(nextTable);
  96.         }
  97.         test.printTable(nextTable);
  98.     }
  99. }
  100. `
  101.  
Advertisement
Add Comment
Please, Sign In to add comment