// This program simulates the game called Life import java.util.Random; import java.awt.*; import javax.swing.*; // The LifeIP class - simulates the game called life public class LifeIP extends JApplet { public void paint(Graphics g) { char[][] lifeBoard = new char[50][50]; int[][] age = new int[50][50]; Random rand; long seed; char c = ' '; // Get a seed value for the random number generator // If it is on the command line, get it from there. // Otherwise, ask for it. // if(args.length == 0) { // System.out.print("Enter a seed value: "); // seed = Console.in.readLong(); // } else { // seed = Long.parseLong(args[0]); // } // Initialize the random number generator // with the supplied seed value // rand = new Random(seed); rand = new Random(); // Initialize the board, filling it with // random live spots initializeBoard(rand, lifeBoard, age); // Print out the initial board drawBoard(g, lifeBoard, age); // Loop, updating the board and printing it out while(c != 'x') { live(lifeBoard, age); drawBoard(g, lifeBoard, age); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Interrupt Exception"); } } } /* * Print out the board */ public static void drawBoard(Graphics g, char[][] board, int[][] age) { for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { if(board[i][j] == ' ') { g.clearRect(i*10, j*10, 10, 10); } else { Color c = new Color(Math.min(age[i][j]*5,255), 0, Math.max(0,255-age[i][j]*5)); g.setColor( c ); g.fillOval(i*10, j*10, 10, 10); } } } } /* * Initialize the board with random live spots */ public static void initializeBoard(Random rand, char[][] board, int[][] age) { char c; // Nested for loops to loop over the arrays (rows), and loop // over the elements of the arrays (columns) for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { // Randomly determine if the spot is alive // or dead. We are using spaces for dead spots // and asterisks for alive spots c = rand.nextBoolean()?' ':'*'; // Set the appropriate location to that value board[i][j] = c; age[i][j] = 0; } } } /* * Update the board for one generation */ public static void live(char[][] board, int[][] age) { int neighbors; char[][] copyOfBoard = twoDArrayClone(board); // Then loop over each element of the 2D array. // If a spot is dead and has three neighbors, it // comes to life. If it was alive and has less than // 2 or more than 3 neighbors, it dies. Otherwise, it // remains the same. for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { // Count the neighbors neighbors = countNeighbors(i, j, copyOfBoard); // If it was not alive if(board[i][j] == ' ') { // If it has three neighbors, it comes to life if(neighbors == 3) { board[i][j] = '*'; age[i][j] = 1; } } else { // It was alive age[i][j]++; // If it has < 2 or > 3 neighbors, it dies if(neighbors != 2 && neighbors != 3) { board[i][j] = ' '; age[i][j] = 0; } } } } } /* * Count the number of neighbors around the specified * location. Care must be taken for edge values. This * code implements wrapping from side to side and top to * bottom. */ public static int countNeighbors(int i, int j, char[][] board) { int neighbors; int left; int right; int up; int down; // First deal with the edge effects if(i > 0) up = i-1; else up = board.length-1; if(i < (board.length-1)) down = i+1; else down = 0; if(j > 0) left = j-1; else left = board[i].length - 1; if(j < (board[i].length-1)) right = j+1; else right = 0; // Count the number of neighbors neighbors = 0; if(board[up][left] == '*') neighbors++; if(board[up][j] == '*') neighbors++; if(board[up][right] == '*') neighbors++; if(board[i][left] == '*') neighbors++; if(board[i][right] == '*') neighbors++; if(board[down][left] == '*') neighbors++; if(board[down][j] == '*') neighbors++; if(board[down][right] == '*') neighbors++; // Return the count of neighbors return neighbors; } public static char[][] twoDArrayClone(char[][] oldArray) { char[][] newArray = new char[oldArray.length][oldArray[0].length]; // First generate a copy of the board for use in the // neighbors calculation for(int i = 0; i < oldArray.length; i++) for(int j = 0; j < oldArray[i].length; j++) newArray[i][j] = oldArray[i][j]; return newArray; } }