Advertisement
noahjk

Life Console Interface

Feb 13th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * The Console class is a console interface to the Life game.
  3.  * @author DH
  4.  * @date Sept. 2008
  5.  */
  6.  
  7. import java.util.Scanner;
  8.  
  9.  
  10. public class Console {
  11.  
  12.     /**
  13.      * @param args unused
  14.      */
  15.     public static void main(String[] args) {
  16.         Scanner in = new Scanner(System.in);
  17.         System.out.println("Please enter the size of the matrix(rows, columns) :");
  18.         int rows = in.nextInt();
  19.         int columns = in.nextInt();
  20.         System.out.println("Please enter random seed: ");
  21.         long seed = in.nextLong();
  22.         System.out.println("Please enter birth range (low, high) :");
  23.         int birthLow = in.nextInt();
  24.         int birthHigh = in.nextInt();
  25.         System.out.println("Please enter live range (low, high): ");
  26.         int liveLow = in.nextInt();
  27.         int liveHigh = in.nextInt();
  28.         try {
  29.             Life game = new Life(seed, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
  30.             playLife(game);
  31.         } catch (IllegalArgumentException e) {
  32.             System.out.println("Inappropriate values: " + e.getMessage());
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Print a boolean matrix
  38.      * @param world is a boolean matrix to be printed with # for true and - for false.
  39.      */
  40.     public static void printWorld(boolean[][] matrix) {
  41.         for (int r=0; r<matrix.length; r++) {
  42.             for (int c=0; c<matrix[0].length; c++) {
  43.                 System.out.print(matrix[r][c] ? " # " : " - ");
  44.             }
  45.             System.out.println();
  46.         }
  47.         System.out.println();
  48.  
  49.     }
  50.  
  51.     /**
  52.      * Play the game of Life starting with a given state
  53.      * @param game is the Life object that provides the current state of Life
  54.      */
  55.     public static void playLife(Life game) {
  56.         printWorld(game.world());
  57.         for (int i=0; i<10; i++) {
  58.             game.update();
  59.             printWorld(game.world());
  60.         }
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement