Tuesday, September 30, 2014

Hangman Game Exercice

Hello, team

Another fun challenge for another job application. Some how this solution was not what they expected, maybe I misunderstood or maybe my coding standars where not at the expected level. Some day I will find out I guess. For now, I decided to make the code public so someone can resue all or some part and improve it.

The exercise was defined as follows:

 The challenge is to build a simple version of a 'hangman' game as a web-app (look & feel is not important).
The app should be built with 'ant' and produce a war file that can be deployed in a tomcat.
The app should use jquery and ajax queries for interaction.
The app should keep the current game state persistent across server and browser re-starts.
The app should have a 'management' page that shows a summary of the state of all games that are currently being played.
Add this line to the .htaccess of root server folder. 'AddType text/cache-manifest .appcache'


My solution was implemented using the following resources:

I used Netbeans 7.4 with grate integration for Tomcat and Ant. Also in Ubuntu it looks like is the only stable IDE for the moment. Eclipse and Intellij are crashing after short time. I implemented an Ant build.xml script with all the required tasks lke junit, war, and deploy.

The implementation of the game is a stateless back end application. At every ajax request from the client side, the state of the game is sent in a JSON object to the server. This game object is sent to the back end to be stored into the MapDB. The Map DB holds two linked maps which makes very easy to run a query and get all the games with the current state of each payer. Each new game gets an id to identify the game throughout all the requests.
     
The two maps are defined as:

        Map 1: Map_objects. <Long, Json String / Serialized>
        Map 2: Index state, calculated from the Json value.

The persistence layer its implemented as a Singleton in the object "ConcurrentPersistenceService.java" which implements de interface "PersistenceService.java" with the following methods:

        Save instance.
        Load instance.
        Store secondary map to index by status.
        Search all objects by state attribute.
     

The REST Web Services are implemented using Jersey RESTful Framework. I used a sample application tutorial as best practices. There is a simple resource to query the list of current games in "GamesResources" and another resource to interact with each game at "GameResoruce". The design on the REST API its inspired in these best practices.

The web UI is implemented using HTML5 and jQuery. The UI is based on another grate tutorial of HTML5 with Canvas hangman game web app implemented in Javascript and without back end interaction. It was a very good starting point were it was only necessary to refactor some code in the  "hangman.js" to make it stateless and work with Game WS Resources.

Finally the Json object that represents the games state is stored in the client side using one of the new HTML5 features, the local storage. The object is stored in a simple key value storage.

Please leave your comments below if you like so, any feed back will be of grate help.

Posted by Marc Andreu.

No comments: