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

I'm Simon, and this is what I say

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

Problem

Play my game!

Having the name that I do (Simon), of course I have to participate in this challenge! (Even though I am a bit "late", but Simon says "have forgiveness!")

This is an implementation of the Simon Says challenge using libGDX. I started using this framework a couple of months ago, and I love the fact that it can create games for Desktop, Android, iOS and HTML5, at once.

As it is libGDX, and Android support is needed, I'm limited to Java 6. As I'm also using GWT, there's some restriction there as well.

Class Summary

I have not included the several different launchers here, as I am just using the defaults that comes with a LibGDX project there.

  • SimonGame: Main LibGDX entry point



  • MenuScreen: Shows the different implemented playing "difficulties"



  • GameScreen: The main game screen, with the buttons you are supposed to remember the sequence of and click at



  • SequenceGenerator: Interface for generating a sequence



  • AppendingGenerator: Adds one new random button each time, but keeping the old sequence



  • SimpleGenerator: Completely generates a new sequence each time



Dependencies

  • LibGDX



  • GWT



Code

This code can also be downloaded from GitHub

AppendingGenerator.java:

public class AppendingGenerator implements SequenceGenerator {
    private final Random random = new Random();
    private int count;
    private List recent;

    public AppendingGenerator(int count) {
        this.count = count;
        this.recent = new ArrayList();
    }

    @Override
    public void generate(int numButtons, List result) {
        while (recent.size() < count) {
            recent.add(random.nextInt(numButtons));
        }
        result.addAll(recent);
        this.count++;
    }
}


GameScreen.java:

```
public class GameScreen implements Screen {
private final SimonGame game;
private final Button[] images;
private final LinkedList sequence = new LinkedList(); // GWT does not support 'Deque' interface
private final Table table = new T

Solution

The only note I can add to this mostly A+ code is that you should generally avoid using Collections for boxed primitives (e.g. LinkedList etc.) - that's a rule of thumb; although it doesn't matter much in such a small application, with bigger code and 1k+ elements per collection - and especially on Dalvik - you'll run into trouble with it, sooner or later. If a simple array is not an option (e.g. you want the collection to be dynamic or use it as deque or whatever) there are some classes, both libGDX-based and e.g. Apache Commons or even Guava ones that allow using Collection-type abstraction on primitives with native speed.

I'd suggest taking a look at https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx/utils - e.g. IntArray and IntSet might be good ideas in many cases. OTOH, those libGDX classes lack Java Collections-compliant interfaces (mostly because it's basically incompatible with primitive-based classes), see https://github.com/libgdx/libgdx/issues/2359 and, for a possible solution, here: https://github.com/libgdx/libgdx/commit/9ff7125482cb822034acdb1e3ebfe5af624d9433 (view wrappers allowing both direct (unboxed) and collection-like (boxed) access) - note it's still waiting to get into trunk, sigh.

Context

StackExchange Code Review Q#73853, answer score: 11

Revisions (0)

No revisions yet.