Java Game ColorPuzzle

| tagged as

Today I like to show you a little java game I have written roughly a year ago. Back in that time I was looking at the Java-2D-API and Swing/Canvas and thought that the best way to learn this stuff is to write a little game. Thinking of an good example game idea was not that easy so I decided to recreate an existing JavaScript game that I had played some time ago. The problem is that I can't remember what the original name was and where I found this game. :-(

screenshot

The gameplay is easy: You have a grid of squares like a chessboard but not in black and white. Instead the squares are randomly filled with one of 7 colors. At the beginning the player has control over the square in the top left corner. With the buttons on the right (one for every color) you can change the color of the square(s) you control. When there is a square with the same color in the direct neighbourhood of one of your controlled squares, this square is now also controlled by you. When you now click on one of the color-buttons, all squares controlled by you are changing the color again. The aim of the game is to control all squares with as little clicks as possible.

Like I said, the main reason to create the game was not to build a great new super game but to learn and exercise the programming language and framework. The game was working more or less but there was an exception with the painting of the component in rare cases. I wasn't able to find the real reason for this exception and so I haven't released the game.

Since then I haven't worked on the game but some weeks ago I remembered the game and wanted to finish it. While looking at the code I found some code smells and bad design. When I would build a game like this from scratch today I would do many things differently.

As the first task I was looking at the unit-tests: I replaced EasyMock with Mockito and changed all asserts to used FEST-Assert. After this I refactored many classes and splitted the code into smaller pieces. While I didn't know the concept of Dependency Injection back in the time I created the game, nowadays DI for me is one of the most important design patterns and so I modified the application to use CDI/WELD-SE. I changed the whole event-mechanism to also use the features of CDI-Events. With this modifications I was able to reduce the coupling between the components a lot.

The last modification was to change the way the canvas is repainted. The old version was using a game-loop with a repaint-mechanism that was more suitable for games that need a high framerate. As this is not the case for this game I changed it to used the default repaint mechanism of the swing components and removed the game loop. The exception that irrirtated me at first was now gone and so I thought it would be the right time to "release" the game now.

Another reason to create the game was the idea to create something like an "artificial intelligence" (of cause it's not realy intelligent or clever) that can solve the game. I liked to test many strategies to build AIs and let them play against each other. I created a "Bogo" algorithm that chooses a color at random (referring to BogoSort) and a "Bruteforce" algorithm that tries every available color. At the moment this artificial intelligence feature is disabled but probably in the future I will create a UI for this.

Have fun with the game. Before I forget, of course the Code is free software under the GPL and can be seen on github. Feel free to look into it and tell me what you think.