Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Random;
- import java.lang.*;
- import java.util.ArrayList;
- /**
- * This program plays Conway's Game of Life - a cellular automaton.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Life
- {
- private final int GRID_WIDTH = 50; // The number of cells across the board
- private final int GRID_HEIGHT = 50; // The number of cells dwn the board
- private final int CELL_WIDTH = 10; // How wide one cell should be (inpixels)
- private final int CELL_HEIGHT = 10; // How high oe cell should be (in pixels)
- /**
- * The main method to run the game of Life. Given the number of
- * generations, loop that many times, creating succeeding generations
- * and printing them out.
- *
- * YOU MUST NOT CHANGE THIS METHOD IN ANY WAY AT ALL.
- * DOING SO WILL RESULT IN LOSS OF MARKS
- *
- * @param nGenerations the number of iterations in the main loop
- */
- public Life(int nGenerations)
- {
- if (nGenerations >0)
- {
- Grid grid = new Grid(GRID_WIDTH, GRID_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
- boolean[][] board = newBoard(GRID_WIDTH, GRID_HEIGHT);
- grid.drawBoard(board);
- for (int i = 0; i < nGenerations; i++)
- {
- board = nextGeneration(board);
- grid.drawBoard(board);
- }
- }
- }
- /**
- * Create an initial board by using a random number generator to set about
- * 10% of the cells to be alive.
- *
- * @return the new board
- */
- public boolean[][] newBoard(int height, int width)
- {
- boolean [] [] board = new boolean [height] [width];
- Random generator = new Random();
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- board [i] [j] = (generator.nextFloat() < 0.10);
- }
- }
- return board;// Wrte your solution here
- }
- /**
- * Create the next generation from the previous one.
- * This takes the current board as input and creates a new board by
- * applying the rules of reproduction.
- *
- * @param board1 the current board, which is a 2-dimensional boolean array
- * @return the new board after applying the rules of reproduction
- */
- public boolean[][] nextGeneration(boolean[][] board1)
- {
- boolean [] [] newArray = new boolean[board1.length][board1.length];
- for (int i = 0; i < board1.length; i++)
- {
- newArray[i] = new boolean[board1[i].length];
- for (int j = 0; j<board1[i].length;j++)
- {
- newArray[i][j] = board1[i][j];
- }// Wrte your solution here
- }
- for (int i = 0; i < newArray.length; i++)
- {
- for (int j = 0; j< newArray[i].length; j++)
- {
- }
- }
- return newArray;
- }
- /**
- * Counts the number of live neighbours of cell <i,j>.
- * Don't try to count before 0 or after the end of the array.
- * Don't count <i,j> itself.
- *
- * @param board the board
- * @param i the row position
- * @param j the column position
- * @return the number of live neighbours of cell <i,j>
- */
- private static int countNeighbours(boolean[][] board, int row, int col)
- {
- int count = 0;
- for(int i = row-1; i <=row + 1;i++)
- {
- if (i >= 0 && i < board.length)
- for(int j = col - 1; j <= col+1;j++)
- if(j >= 0 && j < board[i].length)
- if(i!= row || j!= col)
- if (board[i][j] == true)
- count++;
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement