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.
- */
- import java.util.Random;
- 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[Main.HEIGHT][Main.WIDTH];
- for (int row = 0; row < this.mx; row++) {
- for (int column = 0; column < this.my; column++) {
- switch (countNeighbourCells(row, column)) {
- case 3:
- nextGeneration[row][column] = 1;
- break;
- case 2:
- nextGeneration[row][column] = this.space[row][column];
- break;
- default:
- nextGeneration[row][column] = 0;
- }
- }
- }
- this.space = nextGeneration;
- }
- private int countNeighbourCells(int row, int column) {
- int cellNeighbourCounter = 0;
- for (int relativeRow = -1; relativeRow <= 1; relativeRow++) {
- for (int relativeColumn = -1; relativeColumn <= 1; relativeColumn++) {
- if (isCellPositionValid(row, column, relativeRow, relativeColumn) &&
- !(relativeColumn == 0 && relativeRow == 0)) {
- cellNeighbourCounter += this.space[row + relativeRow][column + relativeColumn];
- }
- }
- }
- return cellNeighbourCounter;
- }
- public boolean isAlive(int row, int column) {
- return this.space[row][column] == 1;
- }
- public boolean isCellPositionValid(int actualRow, int actualColumn, int relativeRow, int relativeColumn) {
- return (relativeRow + actualRow < this.mx-1 && relativeRow + actualRow > 0)
- && (relativeColumn + actualColumn > 0 && relativeColumn + actualColumn < this.my-1);
- }
- public void generateInitialBoard() {
- SpaceData.initSpace = new int[Main.HEIGHT][Main.WIDTH];
- Random random = new Random();
- for (int row = 0; row < this.mx; row++) {
- for (int column = 0; column < this.my; column++) {
- SpaceData.initSpace[row][column] = random.nextBoolean() ? 1 : 0;
- }
- }
- space = SpaceData.initSpace;
- }
- public int getMx() {
- return mx;
- }
- public int getMy() {
- return my;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement