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

Finite Automaton for a Typing Game

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

Problem

I'm creating a game that implements a finite automaton and I don't know if I'm doing it right. Any advice?

Game class

```
import java.awt.Canvas;
import java.awt.event.KeyListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener
{
public final String TITLE = "Game";
public final int WIDTH = 640;
public final int HEIGHT = 680;
public int LEVEL = 1;
public int FPS = 0;
public int SCORE = 0;
public int LIFE = 5;

private boolean running = false;
private Thread thread;

private BufferedImage textBackground = null;
private BufferedImage spriteSheet = null;
private BufferedImage background = null;

private int enemyCount = 3;
private int enemyKilled = 0;
private int enemyNotKilled = 0;
private boolean target = false;
private String targetText = "";

private Controller c;
private LinkedList e = new LinkedList();
private Enemy enemy;

public void init()
{
requestFocus();
try
{
spriteSheet = ImageIO.read(getClass().getResource("/spritesheet.png"));
background = ImageIO.read(getClass().getResource("/background.png"));
}
catch(IOException e)
{
e.printStackTrace();
}

textBackground = getSprite(1, 1, 96, 32);
c = new Controller(textBackground, this);
addKeyListener(this);
c.createEnemy(enemyCount);
e = c.getEnemy();
}

private void start()
{
if(running)
return;

running = true;
thread = new Thread(this);
thread.start();
}

private void stop()
{
if(!running)
return;

running = false;
try
{
thread.join();
}
catch(Exception e)
{
e.printStackTrace();
}

System.exit(1);
}

public void run()
{
init();
long lastTime = System.nanoTime

Solution

There are a number of things to comment on here.
Code-Style

Java coding style puts the { opening brace at the end of the line containing the condition/construct that starts the block. Code like:

public char getFirstLetter()
{
    return firstLetter;
}


should be:

public char getFirstLetter() {
    return firstLetter;
}


Additionally, there should be an empty line after the closing } and the start of the next method declaration.
Threads

You are using swing, yet I cannot see any places where you are accommodating work that is done on the Event Dispatch Thread (EDT) vs. your game thread.

This is likely to lead to inconsistent gameplay, irregular timing, and other problems.... especially during the manipulation of the Graphics objects.
Terminology

I am not seeing a Finite Automata here... this looks pretty open-ended, not finite.
Magic Numbers

These are everywhere. There must be a better way to centralize them.
Integer Arithmetic

This line:

double ns = 1000000000 / framesPerSecond;


is doing integer arithmetic, in your case, you would expect ns to be 33333333.33..... but it will be plain 33333333.0 because the integer division is done before the value is converted to a double. Consider using:

double ns = 1000000000.0 / framesPerSecond;


which converts the one value to a double, and thus the entire expresion is evaluated in the double-precision space.

in this case, the loss of accuracy will likely not significantly affect the game, but you should be aware.

Code Snippets

public char getFirstLetter()
{
    return firstLetter;
}
public char getFirstLetter() {
    return firstLetter;
}
double ns = 1000000000 / framesPerSecond;
double ns = 1000000000.0 / framesPerSecond;

Context

StackExchange Code Review Q#43544, answer score: 7

Revisions (0)

No revisions yet.