Advertisement
tolem

game of life version 2 repaired

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