Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright © Progmasters (QTC Kft.), 2016-2018.
- * All rights reserved. No part or the whole of this Teaching Material (TM) may be reproduced, copied, distributed,
- * publicly performed, disseminated to the public, adapted or transmitted in any form or by any means, including
- * photocopying, recording, or other electronic or mechanical methods, without the prior written permission of QTC Kft.
- * This TM may only be used for the purposes of teaching exclusively by QTC Kft. and studying exclusively by QTC Kft.’s
- * students and for no other purposes by any parties other than QTC Kft.
- * This TM shall be kept confidential and shall not be made public or made available or disclosed to any unauthorized person.
- * 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.
- */
- public class GameOfLife {
- private int mx, my;
- private int[][] space;
- public GameOfLife(int maximumX, int maximumY) {
- this.mx = maximumX;
- this.my = maximumY;
- this.space = new int[maximumX][maximumY];
- }
- public void updateToNextGeneration() {
- int[][] nextGeneration = new int[mx][my];
- for (int row = 0; row < this.mx; row++) {
- for (int column = 0; column < this.my; column++) {
- if (isAlive(row, column)) {
- nextGeneration[row][column] = 1;
- } else {
- nextGeneration[row][column] = 0;
- }
- }
- }
- this.space = nextGeneration;
- }
- public Boolean isAlive(int rowIndex, int columnIndex) {
- int cellNeighbourCounter = 0;
- for (int row = -1; row <= 1; row++) {
- for (int column = -1; column <= 1; column++) {
- if (!(row == 0 && column == 0) && isCellIndeValid(row, rowIndex, column, columnIndex))
- cellNeighbourCounter += this.space[rowIndex + row][columnIndex + column];
- }
- }
- return cellNeighbourCounter == 3 ||
- (cellNeighbourCounter == 2 && this.space[rowIndex][columnIndex] == 1);
- }
- private boolean isCellIndeValid(int row, int rowIndex, int column, int columnIndex) {
- return (rowIndex + row > 0 && rowIndex + row < this.mx - 1) &&
- (columnIndex + column > 0 && columnIndex + column < this.mx - 1);
- }
- public void generateInitialBoard() {
- space = SpaceData.initSpace;
- }
- public int getMx() {
- return mx;
- }
- public int getMy() {
- return my;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement