Advertisement
tolem

game of life

Mar 25th, 2022
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 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. public class GameOfLife {
  13.  
  14. private int mx, my;
  15. private int[][] space;
  16.  
  17. public GameOfLife(int maximumX, int maximumY) {
  18. this.mx = maximumX;
  19. this.my = maximumY;
  20. this.space = new int[maximumX][maximumY];
  21. }
  22. public void updateToNextGeneration() {
  23. int[][] nextGeneration = new int[mx][my];
  24. for (int row = 0; row < this.mx; row++) {
  25. for (int column = 0; column < this.my; column++) {
  26. if (isAlive(row, column)) {
  27. nextGeneration[row][column] = 1;
  28. } else {
  29. nextGeneration[row][column] = 0;
  30. }
  31. }
  32. }
  33. this.space = nextGeneration;
  34. }
  35.  
  36. public Boolean isAlive(int rowIndex, int columnIndex) {
  37. int cellNeighbourCounter = 0;
  38. for (int row = -1; row <= 1; row++) {
  39. for (int column = -1; column <= 1; column++) {
  40. if (!(row == 0 && column == 0) && isCellIndeValid(row, rowIndex, column, columnIndex))
  41. cellNeighbourCounter += this.space[rowIndex + row][columnIndex + column];
  42. }
  43. }
  44. return cellNeighbourCounter == 3 ||
  45. (cellNeighbourCounter == 2 && this.space[rowIndex][columnIndex] == 1);
  46. }
  47.  
  48. private boolean isCellIndeValid(int row, int rowIndex, int column, int columnIndex) {
  49.  
  50. return (rowIndex + row > 0 && rowIndex + row < this.mx - 1) &&
  51. (columnIndex + column > 0 && columnIndex + column < this.mx - 1);
  52. }
  53.  
  54. public void generateInitialBoard() {
  55. space = SpaceData.initSpace;
  56. }
  57.  
  58. public int getMx() {
  59. return mx;
  60. }
  61.  
  62. public int getMy() {
  63. return my;
  64. }
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement