/* * GameOfLife.java * * Created on June 2, 2003, 7:02 AM */ /** * * @author dickey */ public class GameOfLife2 { final private int cellsAcross = 12; final private int cellsDown = 15; /** Codes for alive and dead. NOT private! */ final static int STATUS_ALIVE = 1; final static int STATUS_DEAD = 2; private int lifeThreashhold = 4; private int deathThreashhold = 5; private int[][] board; private GOLDisplay2 viewer; /** Creates a new instance of GameOfLife */ public GameOfLife2() { this.board = new int[cellsDown][cellsAcross]; for(int r = 0; r < cellsDown; r++) { for (int c = 0; c < cellsAcross; c++) { if ((c+r)%3 ==0) { board[r][c] = STATUS_DEAD; } else { board[r][c] = STATUS_ALIVE; } } } viewer = new GOLDisplay2(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(); } 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; } 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 (board[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 toggleCell(int r, int c) { if (board[r][c] == STATUS_ALIVE) { board[r][c] = STATUS_DEAD; } else { board[r][c] = STATUS_ALIVE; } } public void runGame() { while (1 == 1) { updateBoard(); try { Thread.sleep(300); } catch (Exception e) { } //printBoard(); } } public static void main (String[] args) { System.out.println("Game of Life"); GameOfLife2 aGame = new GameOfLife2(); int p = 0; try { Thread.sleep(9000); } catch (Exception e) { } while (p >= 0) { //aGame.printBoard(); aGame.updateBoard(); try { Thread.sleep(300); } catch (Exception e) { } p++; } System.out.println("End game"); } //end class GameOfLife } class GOLDisplay2 extends javax.swing.JPanel { private int cellWidth = 32; private int cellHeight = 28; private int cellsAcross; private int cellsDown; private GameOfLife2 game; public GOLDisplay2(int across, int down, GameOfLife2 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 + 70); this.setPreferredSize(new java.awt.Dimension(totalWidth+15, totalHeight+15)); this.setBackground(java.awt.Color.cyan); java.awt.Container container = frame.getContentPane(); container.setBackground(java.awt.Color.red); container.setLayout(new java.awt.FlowLayout()); container.add(this); frame.show(); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { int col = e.getX()/cellWidth; int row = e.getY()/cellHeight; game.toggleCell(row, col); repaint(); } public void mouseClicked(java.awt.event.MouseEvent e) { } }); } private final java.awt.Color ALIVE_COLOR = java.awt.Color.green; private final java.awt.Color DEAD_COLOR = java.awt.Color.yellow; 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(ALIVE_COLOR); } else { g.setColor(DEAD_COLOR); } g.fill(cellShape); } } } }