Advertisement
tolem

game of life version 2

Mar 25th, 2022
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 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.  
  25. public void updateToNextGeneration() {
  26. int[][] nextGeneration = new int[Main.HEIGHT][Main.WIDTH];
  27. for (int row = 0; row < this.mx; row++) {
  28. for (int column = 0; column < this.my; column++) {
  29. switch (countNeighbourCells(row, column)) {
  30. case 3:
  31. nextGeneration[row][column] = 1;
  32. break;
  33. case 2:
  34. nextGeneration[row][column] = this.space[row][column];
  35. break;
  36. default:
  37. nextGeneration[row][column] = 0;
  38. }
  39. }
  40. }
  41. this.space = nextGeneration;
  42. }
  43.  
  44.  
  45. private int countNeighbourCells(int row, int column) {
  46. int cellNeighbourCounter = 0;
  47. for (int relativeRow = -1; relativeRow <= 1; relativeRow++) {
  48. for (int relativeColumn = -1; relativeColumn <= 1; relativeColumn++) {
  49. if (isCellPositionValid(row, column, relativeRow, relativeColumn)) {
  50. cellNeighbourCounter += this.space[row + relativeRow][column + relativeColumn];
  51. }
  52. }
  53. }
  54. return cellNeighbourCounter;
  55. }
  56.  
  57. public boolean isAlive(int row, int column) {
  58.  
  59. return this.space[row][column] == 1;
  60. }
  61.  
  62. public boolean isCellPositionValid(int actualRow, int actualColumn, int relativeRow, int relativeColumn) {
  63.  
  64.  
  65. return relativeRow + actualRow < this.mx || relativeRow + actualRow > 0
  66. || relativeColumn + actualColumn > 0 || relativeRow + actualRow < this.my;
  67. }
  68.  
  69. public void generateInitialBoard() {
  70. SpaceData.initSpace = new int[this.mx + 2][this.my + 2];
  71. Random random = new Random();
  72. for (int row = 0; row < this.mx; row++) {
  73. for (int column = 0; column < this.my; column++) {
  74. SpaceData.initSpace[row][column] = random.nextBoolean() ? 1 : 0;
  75. }
  76. }
  77.  
  78. space = SpaceData.initSpace;
  79. }
  80.  
  81. public int getMx() {
  82. return mx;
  83. }
  84.  
  85. public int getMy() {
  86. return my;
  87. }
  88.  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement