Tag Archives: game

I spent all my Sunday on design and implementation of basic match-3 game engine by Unity framework. See demo.


From time to time we need to translate some application from one platform/language to another. For example, we need to implement the same mobile/social game for web/flash, iOS and Android. Of course, we can use some multi-platform framework like Corona to build such system, but it’s not so power like native software development tools.

The idea is to design and implement the core (model) of the game in some universal module in Lua programming language. The model may be pretty complicated for the games like chess or poker. I implemented the game ‘Guess The Number’ in Lua as an example. Then I separated the model:

GuesGame = {}GuesGame.WIN = 1GuesGame.LOSE = 2GuesGame.GREATER = 3GuesGame.LESS = 4GuesGame.MIN = 1GuesGame.MAX = 10GuesGame.MAX_TRY = 3function GuesGame:new()        return setmetatable({                _counter = GuesGame.MAX_TRY        }, {                __index = GuesGame        })endfunction GuesGame:start()        self._x = math.random(GuesGame.MIN, GuesGame.MAX)        self._counter = 0endfunction GuesGame:play(a)        if self._counter >= GuesGame.MAX_TRY then                return GuesGame.LOSE        end        self._counter = self._counter + 1        if a == self._x then                return GuesGame.WIN        else                if self._counter >= GuesGame.MAX_TRY then                        return GuesGame.LOSE                elseif self._x > a then                        return GuesGame.GREATER                else                        return GuesGame.LESS                end        endend

As you can see, there is just classic ‘guess the number’ game. The program chooses integer number from 1 to 10 randomly. And the player is trying to guess it in 3 tries.

Next steps were implementation of the game as console applications in pure lua, Objective-C and Java. Lua engine is built as C library, so, there are not problems with Objective-C. In case of Java I used LuaJ. Keep in mind, I used exactly the same lua code of the game model for all implementations. Generally, it was not only that lua code. I need to add some support code for initialization and interface with UI side implemented in native language specified for the appropriate case.

E:ProjectsGuesGamepure_Lua>lua GuesGame.luaq - quit, s - start new game, 1-10 - guesTry to gues a number between 1 and 10?5Less3Win! :-)q

After that I implemented iOS version as iPhone application in the same way. There were no problems at all.


To do flash (action script) game I used lua via alchemy. It costs me some performance. Alchemy is going slow at line of calling it from action script side because of huge serialization. So, if you are going to touch you game model often, then you better avoid such scenario and port the model to action script.


We can’t use LuaJava on Android as for pure Java because of differences in JVM. So, I used Android NDK to compile lua engine as C module for Android game.


In game development performance is very important factor, especially in case of hi-load on-line multi player games in social networks. As we know one of useful tricks is to hard code as much as possible to avoid huge reading or initialization.

In the other hand, it’s hard to support such hard-coded solution (they even call it anti-pattern). For example, we have list of items in the game. It should be the same on servers side and on client side. If list is pretty long then it’s easy to make a fatal mistake, so, lists would be different. Better approach is to hold all the data in single place handy to modify/support and automation system to synchronize the data with all other places in the source code. There are a lot of benefits. As it’s single place then it’s easy to support and avoid difference mistakes. As it automated then we don’t need developer to spend his time to update it.

I created such system. I called it transistor. There are 3 parts:

  • The python script
  • Data composed into CSV
  • Source code with hard coded section and template to fill it

We can use one or more CSV tables. One table may have reference(s) as index(es) to another. CSV content should be available via HTTP or locally. I use Google Docs Spreadsheets for that. First line of each CSV table should have case-sensitive fields (columns) names. Other rows have the data. Later the script will parse those data into the list of dictionaries.

My first try was using of pure python string formatting as template. It means that the template and its logic were in python code on the script side. There were bad design and huge ugly implementation. Well, if you have a lot of code then it is almost always bad architecture. There should be very small amount of code. So, I declined that prototype. The second (and the best) design approach was to make that script universal and move all specific template data and logic to the template situated in the same source code file (in comments).

/*// gunTypes begin{% set gunTypes = tables.gunTypes -%}{%- for g in gunTypes -%}{%- if g.shoot_width -%}{%- if loop.index0 > 0 %},{% endif -%}new GunType({{}}, new Shape({{g.base_width}}, {{g.base_height}}, new byte[]{                        {{g.base_shape|replace('n', 'n                                                        ')}}        }), new Shape({{g.shoot_width}}, {{g.shoot_height}}, new byte[]{                        {{g.shoot_shape|replace('n', 'n                                                        ')}}        })){%- endif %}{%- endfor %}// gunTypes template end */        // ... here would be generated content ...// gunTypes end

Hard-coded section in the source code file has header, footer and template delimiter. There are the template between header and template delimiter and the generated content between delimiter and the footer. Usage example:

python --file=GunTypesDict.cs --section=gunTypes                --table=gunTypes                --url=""
or more complicated with 2 tables:
python --file=LocationsDict.cs --section=locations                --table=fields --table=locations                --url=""                --url=""
After such data publishing we have the same source code like:
/*// gunTypes begin{% set gunTypes = tables.gunTypes -%}{%- for g in gunTypes -%}{%- if g.shoot_width -%}{%- if loop.index0 > 0 %},{% endif -%}new GunType({{}}, new Shape({{g.base_width}}, {{g.base_height}}, new byte[]{                        {{g.base_shape|replace('n', 'n                                                        ')}}        }), new Shape({{g.shoot_width}}, {{g.shoot_height}}, new byte[]{                        {{g.shoot_shape|replace('n', 'n                                                        ')}}        })){%- endif %}{%- endfor %}// gunTypes template end */new GunType(0, new Shape(1, 2, new byte[]{                        1,1        }), new Shape(1, 1, new byte[]{                        5        })),new GunType(1, new Shape(2, 1, new byte[]{                        1,1        }), new Shape(2, 2, new byte[]{                        0,5,                        5,0        }))// gunTypes end

There is a multiplayer server for realtime strategy game.
There is special Process thread to process behaviour of the game world.
It wake up every N milliseconds, do some changes in our object model (game world)
and then sleep for next N milliseconds.
Also, there are asynchronous network actions runned in another Socket thread.
So, the question is about synchronization.

Usually, I use ‘lock (object) { … }’ way to sync. work with data in object model.
There are a lot of locks, so, it needs to keep in mind
this multithreading nature of the server during thinking on the business logic.
As better approach, this time I implemented queue-based solution:
The Process thread may be in 3 modes:
    1) Process the world;
    2) Process the queue of collected actions;
    3) Do nothing.
Depends on the this mode, incoming Socket actions is processing like:
    1) Collected to special queue
    2) Waiting for #2 finish
    3) Process the action directly

Here is pseudocode of both methods (threads), where lock() has C# meaning.

    lock (mode)
        if (isProcess)
            lock (queue)
        } else {
            lock (process)

    lock (mode)
        isProcess := true;
    lock (process)
        lock (mode)
            isProcess := false;
        lock (queu)

As simpler desing, we can only collect actions in Socket thread
and process them only in the Process thread.
But such solution looks ugly.
In deed, we don’t need to transfer the action via queue,
we need to syncronize the process only.

I just completed with tiny Java/Swing tool for matching words translation. This game loads vocabulary from file. The goal is matching all words translations correctly.
This implementation is standard MVC design. GUI notifies controller via delegate interface and the model notifies controller by observer pattern.