HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

GameOfLife on GDX

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gdxgameoflifestackoverflow

Problem

I would like to know if I did everything right and if there a way to do it more simple and\or better?

GameOfLife - main

```
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

// http://www.conwaylife.com/wiki/Rulestring#Rules
public class GameOfLife extends ApplicationAdapter {
ShapeRenderer shapeRenderer;
Board board;
int boardSize = 25;//rectangular
final int CELL_SIZE = 10;//rectangular
final int CELL_SCALE = CELL_SIZE;
List bornRule = new ArrayList();
List survivalRule = new ArrayList();
OrthographicCamera camera;
String preSet[] = {"Glider", "R-pentomino", "Diehard"};
String rule = "3/12345";
private int delay = 100;

public GameOfLife() throws URISyntaxException {
}

@Override
public void create () {
camera = new OrthographicCamera();
camera.setToOrtho(true);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(camera.combined);

parseRules();

try {
board = new Board(boardSize, bornRule, survivalRule, Paths.get(ClassLoader.getSystemResource(preSet[2]).toURI()));
} catch (Exception e) {
e.printStackTrace();
}
}

private void parseRules() {
String[] split = rule.split("/");
String[] born = split[0].split("");
String[] survive = split[1].split("");
initList(born, bornRule);
initList(survive, survivalRule);
}

private void initList(String[] splitedString, List list) {
for (String s : splitedString) {
list.add(Integer.parseInt(s));
}
}

@Override
public void render () {

Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

shapeRenderer.be

Solution

Thanks for sharing the code.

here are a few things I found:

Naming

Finding good names is the hardest part in programming, so always take your time to think about the names of your identifiers.

On the bright side you follow the Java Naming Conventions.

Do not abbreviate

At some places you use "short names" for variables like v and k in readFile(). There is no penalty for using long identifier names.

don't surprise your readers

This special case of v and k is even worse because you use unusual abbreviations for field coordinates.

Separation of Concerns

Some of the functionality is not in the place I would expect it.

reading input file

IMHO this should be in a separate class. This would enable you to change the source of the initial setup to a database or a webservice without any change in the Borad class.

Switching the actual state

You have the Cell class deciding what state is next. This means, the Cell class nut know the available states and how they follow each other.

This looks obvious for the basic "Game of Life" but there are variants like "Forrest fire" where we have 3 states...

calculating neighbor coordinate

You calculate the neighbor coordinates in the Board class. I'd do this in the Direction class. This way you would also avoid the public access to the fields in the Direction (which violates information hiding principle).

General coding

Don't do work in constructors

Especially do not call methods in other classes. Call methods in own class only if they are private or final.
Also do not invoke the new operator in a constructor.

The reason is that doing the above makes your code inflexible. It violates the open - closed principle.

use "real" constants

A Constant in Java is not only final, it also must be static.

Context

StackExchange Code Review Q#162788, answer score: 3

Revisions (0)

No revisions yet.