Advertisement
brilliant_moves

GameOfLife.java

Sep 16th, 2012
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.68 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2.  
  3. public class GameOfLife {
  4.  
  5.     /**
  6.     *   Program: GameOfLife.java
  7.     *   Purpose: Implement Conway's Game of Life, using examples
  8.     *   Creator: Chris Clarke
  9.     *   Created: 02.10.2012
  10.     *   Modified: 19.10.2012
  11.     */
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scan = new Scanner(System.in);
  15.         int n=0;
  16.         System.out.print("Example 1, 2, 3 or 4? ");
  17.         try {
  18.             n = Integer.parseInt(scan.nextLine());
  19.         } catch (NumberFormatException e) {
  20.             System.out.println("Must be 1, 2, 3 or 4!");
  21.             System.exit(1);
  22.         }
  23.         clearBoard();
  24.         setUpExample(n);
  25.         printBoard();
  26.         while (true) {
  27.             System.out.print("Press Enter to continue (x to exit)...");
  28.             String s = scan.nextLine();
  29.             if (s.toLowerCase().startsWith("x")) break;
  30.             board = applyRules();
  31.             printBoard();
  32.         } // while
  33.     } // main
  34.  
  35.     public static void clearBoard() {
  36.         for (int i = 0; i < NUMBER_OF_ROWS; i++) {
  37.             for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
  38.                 board[i][j] = false;
  39.             } // for j
  40.         } // for i
  41.     } // clearBoard
  42.  
  43.     public static void example1() {
  44.         board[2][2] = true;
  45.         board[2][3] = true;
  46.         board[3][2] = true;
  47.         board[3][3] = true;
  48.         board[5][10] = true;
  49.         board[5][11] = true;
  50.         board[5][12] = true;
  51.         board[4][9] = true;
  52.         board[4][10] = true;
  53.         board[4][11] = true;
  54.         board[8][20] = true;
  55.         board[8][21] = true;
  56.         board[9][20] = true;
  57.         board[9][21] = true;
  58.         board[10][22] = true;
  59.         board[10][23] = true;
  60.         board[11][22] = true;
  61.         board[11][23] = true;
  62.     } // example1
  63.  
  64.     public static void example2() {
  65.         board[3][12] = true;
  66.         board[3][13] = true;
  67.         board[3][14] = true;
  68.         board[4][11] = true;
  69.         board[4][12] = true;
  70.         board[4][13] = true;
  71.         board[7][2] = true;
  72.         board[7][3] = true;
  73.         board[8][1] = true;
  74.         board[8][4] = true;
  75.         board[9][2] = true;
  76.         board[9][3] = true;
  77.         board[10][10] = true;
  78.         board[10][11] = true;
  79.         board[10][12] = true;
  80.     } // example2
  81.  
  82.     public static void example3() {
  83.         board[5][5] = true;
  84.         board[5][6] = true;
  85.         board[5][7] = true;
  86.         board[5][9] = true;
  87.         board[6][5] = true;
  88.         board[7][8] = true;
  89.         board[7][9] = true;
  90.         board[8][6] = true;
  91.         board[8][7] = true;
  92.         board[8][9] = true;
  93.         board[9][5] = true;
  94.         board[9][7] = true;
  95.         board[9][9] = true;
  96.     } // example3
  97.  
  98.     public static void example4() {
  99.         for (int i=2; i<=4; i++) {
  100.             board[8][i] = true;
  101.         }
  102.         board[9][4] = true;
  103.         board[10][2] = true;
  104.     } // example4
  105.  
  106.     public static void setUpExample(int x) {
  107.         switch (x) {
  108.             case 1  :   example1(); break;
  109.             case 2  :   example2(); break;
  110.             case 3  :   example3(); break;
  111.             case 4  :   example4(); break;
  112.             default :   System.out.println("Error");
  113.                     System.exit(1);
  114.         } // switch
  115.     } // setUpExample
  116.  
  117.     public static void printBoard() {
  118.         for (int i=0; i<20; i++) System.out.println();  // make space
  119.  
  120.         for (int i = 0; i < NUMBER_OF_ROWS; i++) {
  121.             for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
  122.                 if (board[i][j]) {
  123.                     System.out.print("*");
  124.                 } else {
  125.                     System.out.print(".");
  126.                 } // if
  127.             } // for j
  128.             System.out.println();
  129.         } // for i
  130.     } // printBoard
  131.  
  132.     public static int countLiveNeighbours(int i, int j) {
  133.         int n = 0;
  134.  
  135.         if (i>0) {              // up
  136.             if (board[i-1][j]) {
  137.                 n++;
  138.             }
  139.             if (j>0) {          // up & left
  140.                 if (board[i-1][j-1]) {
  141.                     n++;
  142.                 }
  143.             }
  144.             if (j < NUMBER_OF_COLUMNS-1) {  // up & right
  145.                 if (board[i-1][j+1]) {
  146.                     n++;
  147.                 }
  148.             }
  149.         }
  150.  
  151.         if (i < NUMBER_OF_ROWS-1) {     // down
  152.             if (board[i+1][j]) {
  153.                 n++;
  154.             }
  155.             if (j>0) {          // down & left
  156.                 if (board[i+1][j-1]) {
  157.                     n++;
  158.                 }
  159.             }
  160.             if (j < NUMBER_OF_COLUMNS-1) {  // down & right
  161.                 if (board[i+1][j+1]) {
  162.                     n++;
  163.                 }
  164.             }
  165.         }
  166.  
  167.         if (j>0) {              // left
  168.             if (board[i][j-1]) {
  169.                 n++;
  170.             }
  171.         }
  172.  
  173.         if (j < NUMBER_OF_COLUMNS-1) {      // right
  174.             if (board[i][j+1]) {
  175.                 n++;
  176.             }
  177.         }
  178.  
  179.         return n;
  180.     } // countLiveNeighbours
  181.  
  182.     public static boolean isAlive(boolean alive, int n) {
  183.         if (alive) {
  184.             if (n<2) {
  185.                 return false;
  186.             } else if (n<=3) {
  187.                 return true;
  188.             } else {
  189.                 return false;
  190.             }
  191.         } else {
  192.             if (n==3) {
  193.                 return true;
  194.             } else {
  195.                 return false;
  196.             }
  197.         } // if (alive)
  198.     } // isAlive
  199.  
  200.     public static boolean[][] applyRules() {
  201.         boolean[][] newBoard = new boolean[ NUMBER_OF_ROWS][ NUMBER_OF_COLUMNS];
  202.         for (int i = 0; i < NUMBER_OF_ROWS; i++) {
  203.             for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
  204.                 int n = countLiveNeighbours(i, j);
  205.                 newBoard[i][j] = isAlive(board[i][j], n);
  206.             } // for j
  207.         } // for i
  208.  
  209.         return newBoard;
  210.     } // applyRules
  211.  
  212.     static final int NUMBER_OF_ROWS = 22;
  213.     static final int NUMBER_OF_COLUMNS = 27;
  214.     static boolean[][] board = new boolean[ NUMBER_OF_ROWS][ NUMBER_OF_COLUMNS];
  215. } // class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement