Advertisement
tolem

game of life version 2 refactored

Mar 25th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /*
  2. * Copyright © Progmasters (QTC Kft.), 2016-2018.
  3. * All rights reserved. No part or the whole of this Teaching Material (TM) may be reproduced, copied, distributed,
  4. * publicly performed, disseminated to the public, adapted or transmitted in any form or by any means, including
  5. * photocopying, recording, or other electronic or mechanical methods, without the prior written permission of QTC Kft.
  6. * This TM may only be used for the purposes of teaching exclusively by QTC Kft. and studying exclusively by QTC Kft.’s
  7. * students and for no other purposes by any parties other than QTC Kft.
  8. * This TM shall be kept confidential and shall not be made public or made available or disclosed to any unauthorized person.
  9. * Any dispute or claim arising out of the breach of these provisions shall be governed by and construed in accordance with the laws of Hungary.
  10. */
  11.  
  12. import java.util.Random;
  13.  
  14. public class GameOfLife {
  15.  
  16. private int mx, my;
  17. private int[][] space;
  18.  
  19. public GameOfLife(int maximumX, int maximumY) {
  20. this.mx = maximumX;
  21. this.my = maximumY;
  22. this.space = new int[maximumX][maximumY];
  23. }
  24. public void updateToNextGeneration() {
  25. int[][] nextGeneration = new int[Main.HEIGHT][Main.WIDTH];
  26. for (int row = 0; row < this.mx; row++) {
  27. for (int column = 0; column < this.my; column++) {
  28. nextGeneration[row][column] = addNewCellValue(nextGeneration, row, column);
  29. }
  30. }
  31. this.space = nextGeneration;
  32. }
  33. private int addNewCellValue(int[][] nextGeneration, int row, int column) {
  34. int numberOfNeighbourCells = countNeighbourCells(row, column);
  35. boolean isAlive = isAlive(row,column);
  36. return numberOfNeighbourCells == 3 || (numberOfNeighbourCells == 2 && isAlive)?1:0;
  37. }
  38. private int countNeighbourCells(int row, int column) {
  39.  
  40. int cellNeighbourCounter = 0;
  41. for (int relativeRow = -1; relativeRow <= 1; relativeRow++) {
  42. for (int relativeColumn = -1; relativeColumn <= 1; relativeColumn++) {
  43. if (isCellPositionValid(row, column, relativeRow, relativeColumn) &&
  44. !(relativeColumn == 0 && relativeRow == 0)) {
  45. cellNeighbourCounter += this.space[row + relativeRow][column + relativeColumn];
  46. }
  47. }
  48. }
  49. return cellNeighbourCounter;
  50. }
  51. public boolean isAlive(int row, int column) {
  52.  
  53. return this.space[row][column] == 1;
  54. }
  55. private boolean isCellPositionValid(int actualRow, int actualColumn, int relativeRow, int relativeColumn) {
  56.  
  57. return (relativeRow + actualRow < this.mx-1 && relativeRow + actualRow > 0)
  58. && (relativeColumn + actualColumn > 0 && relativeColumn + actualColumn < this.my-1);
  59. }
  60. public void generateInitialBoard() {
  61. SpaceData.initSpace = new int[Main.HEIGHT][Main.WIDTH];
  62. Random random = new Random();
  63. for (int row = 0; row < this.mx; row++) {
  64. for (int column = 0; column < this.my; column++) {
  65. SpaceData.initSpace[row][column] = random.nextBoolean() ? 1 : 0;
  66. }
  67. }
  68.  
  69. space = SpaceData.initSpace;
  70. }
  71. public int getMx() {
  72. return mx;
  73. }
  74. public int getMy() {
  75. return my;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement