Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Random;
  3. import java.lang.*;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7. * This program plays Conway's Game of Life - a cellular automaton.
  8. *
  9. * @author (your name)
  10. * @version (a version number or a date)
  11. */
  12. public class Life
  13. {
  14. private final int GRID_WIDTH = 50; // The number of cells across the board
  15. private final int GRID_HEIGHT = 50; // The number of cells dwn the board
  16.  
  17. private final int CELL_WIDTH = 10; // How wide one cell should be (inpixels)
  18. private final int CELL_HEIGHT = 10; // How high oe cell should be (in pixels)
  19.  
  20. /**
  21. * The main method to run the game of Life. Given the number of
  22. * generations, loop that many times, creating succeeding generations
  23. * and printing them out.
  24. *
  25. * YOU MUST NOT CHANGE THIS METHOD IN ANY WAY AT ALL.
  26. * DOING SO WILL RESULT IN LOSS OF MARKS
  27. *
  28. * @param nGenerations the number of iterations in the main loop
  29. */
  30. public Life(int nGenerations)
  31. {
  32.  
  33. if (nGenerations >0)
  34. {
  35. Grid grid = new Grid(GRID_WIDTH, GRID_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
  36. boolean[][] board = newBoard(GRID_WIDTH, GRID_HEIGHT);
  37.  
  38. grid.drawBoard(board);
  39. for (int i = 0; i < nGenerations; i++)
  40. {
  41. board = nextGeneration(board);
  42. grid.drawBoard(board);
  43. }
  44. }
  45. }
  46.  
  47. /**
  48. * Create an initial board by using a random number generator to set about
  49. * 10% of the cells to be alive.
  50. *
  51. * @return the new board
  52. */
  53. public boolean[][] newBoard(int height, int width)
  54. {
  55. boolean [] [] board = new boolean [height] [width];
  56. Random generator = new Random();
  57.  
  58. for (int i = 0; i < height; i++)
  59. {
  60. for (int j = 0; j < width; j++)
  61. {
  62. board [i] [j] = (generator.nextFloat() < 0.10);
  63. }
  64. }
  65. return board;// Wrte your solution here
  66. }
  67.  
  68. /**
  69. * Create the next generation from the previous one.
  70. * This takes the current board as input and creates a new board by
  71. * applying the rules of reproduction.
  72. *
  73. * @param board1 the current board, which is a 2-dimensional boolean array
  74. * @return the new board after applying the rules of reproduction
  75. */
  76. public boolean[][] nextGeneration(boolean[][] board1)
  77. {
  78. boolean [] [] newArray = new boolean[board1.length][board1.length];
  79.  
  80. for (int i = 0; i < board1.length; i++)
  81. {
  82. newArray[i] = new boolean[board1[i].length];
  83.  
  84. for (int j = 0; j<board1[i].length;j++)
  85. {
  86. newArray[i][j] = board1[i][j];
  87.  
  88. }// Wrte your solution here
  89. }
  90.  
  91. for (int i = 0; i < newArray.length; i++)
  92. {
  93. for (int j = 0; j< newArray[i].length; j++)
  94. {
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101. return newArray;
  102. }
  103.  
  104. /**
  105. * Counts the number of live neighbours of cell <i,j>.
  106. * Don't try to count before 0 or after the end of the array.
  107. * Don't count <i,j> itself.
  108. *
  109. * @param board the board
  110. * @param i the row position
  111. * @param j the column position
  112. * @return the number of live neighbours of cell <i,j>
  113. */
  114. private static int countNeighbours(boolean[][] board, int row, int col)
  115. {
  116. int count = 0;
  117.  
  118. for(int i = row-1; i <=row + 1;i++)
  119. {
  120. if (i >= 0 && i < board.length)
  121. for(int j = col - 1; j <= col+1;j++)
  122. if(j >= 0 && j < board[i].length)
  123.  
  124. if(i!= row || j!= col)
  125. if (board[i][j] == true)
  126. count++;
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. }
  134. return count;
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement