/*** * GameOfLife.java *As shown in lecture 6/3/2003, with some fixes for background and centering, and comments added. This version does NOT let you click on the cells (see GameOfLife2). */ public class GameOfLife { final private int cellsAcross = 11; final private int cellsDown = 10; /** Codes for alive and dead. */ final static int STATUS_ALIVE = 1; final static int STATUS_DEAD = 0; private int lifeThreashhold = 3; private int deathThreashhold = 7; private int[][] board; /** A GUI for displaying the board. */ private GOLDisplay viewer; /** Creates a new instance of GameOfLife */ public GameOfLife() { this.board = new int[cellsDown][cellsAcross]; for(int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { board[r][c] = STATUS_DEAD; } } viewer = new GOLDisplay(cellsAcross, cellsDown, this); } public void updateBoard() { int[][] newBoard; newBoard = new int[cellsDown][cellsAcross]; for(int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { newBoard[r][c] = board[r][c]; //determine new status by counting neighbors of one cell int nCount = countLiveNeighbors(r, c); if (nCount <= lifeThreashhold) { newBoard[r][c] = STATUS_ALIVE; } if (nCount >= deathThreashhold) { newBoard[r][c] = STATUS_DEAD; } } } board = newBoard; viewer.repaint(); //This is how the GUI knows to redraw the board! } private int countLiveNeighbors(final int r, final int c) { int count = 0; for (int i = r-1; i <= r + 1; i++) { for (int j = c-1; j <= c + 1; j++) { if (i >= 0 && i < cellsDown && j >=0 && j < cellsAcross) { if (board[i][j] == STATUS_ALIVE) { count++; } } } } //System.out.print(" " + count); return count; } /** ASCII art display of the board. */ public void printBoard() { System.out.println("Board looks like this:"); for(int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { if (cellStatus(r,c) == STATUS_ALIVE) { System.out.print("A"); } else { System.out.print("."); } } System.out.println(); } } public int cellStatus(int r, int c) { return this.board[r][c]; } public void runGame() { while (1 == 1) { updateBoard(); printBoard(); } } public static void main (String[] args) { System.out.println("Game of Life"); GameOfLife aGame = new GameOfLife(); int p = 10; while (p > 0) { aGame.printBoard(); aGame.updateBoard(); try { Thread.sleep(500); } catch (Exception e) { } p--; } System.out.println("End game"); } //end class GameOfLife } class GOLDisplay extends javax.swing.JPanel { private int cellWidth = 35; private int cellHeight = 40; private int cellsAcross; private int cellsDown; private GameOfLife game; public GOLDisplay(int across, int down, GameOfLife aGame) { super(); this.cellsAcross = across; this.cellsDown = down; this.game = aGame; javax.swing.JFrame frame = new javax.swing.JFrame("Game of Life"); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); int totalWidth = cellWidth*cellsAcross; int totalHeight = cellHeight*cellsDown; frame.setSize(totalWidth + 60, totalHeight + 90); this.setBackground(java.awt.Color.orange); this.setPreferredSize(new java.awt.Dimension(totalWidth+20, totalHeight+20)); java.awt.Container container = frame.getContentPane(); container.setBackground(java.awt.Color.red); container.setLayout(new java.awt.FlowLayout()); container.add(this); frame.show(); } /** Override the default paintComponent to draw the game board. */ public void paintComponent(java.awt.Graphics origG) { super.paintComponent(origG); java.awt.Graphics2D g = (java.awt.Graphics2D) origG; final int margin = 4; for (int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { java.awt.Shape cellOutline = new java.awt.Rectangle( c*cellWidth+margin, r*cellHeight+margin, cellWidth, cellHeight); g.setColor(java.awt.Color.black); g.draw(cellOutline); java.awt.Shape cellShape = new java.awt.Rectangle( c*cellWidth+margin+1, r*cellHeight+margin+1, cellWidth-1, cellHeight-1); if (game.cellStatus(r,c) == GameOfLife.STATUS_ALIVE) { g.setColor(java.awt.Color.green); } else { g.setColor(java.awt.Color.yellow); } g.fill(cellShape); } } } }