Borgar.net

— Go straight to page navigation

13. July 2009

Sōkoban

A few days without internet do strange things to a computer addict. In my case I start to rummage frenetically though old projects to see if there is anything I can finish, polish, or do something with that doesn't require internet access.

sokoban

So I found my old Sokoban game. Originally a Windows application, I though it could be fun to try to remake it in JavaScript. So that's what I did.

I wrote the bulk of it in about an evening, and spent a few more evenings applying spit and polish until I was satisfied that it worked the way I wanted. The whole thing is about 300 lines of mostly uncommented code running on top of jQuery — well, actually as a jQuery plugin.

The interresting bits

There are a few characteristics that make this implementation interesting. The first is that the game uses progressive enhancement methodology. The script runs through marked items on the webpage and makes valid Sokoban levels playable. If you don't have JavaScript you can still read the level codes, which are normally written in a standard like this:

####
# .#
#  ###
#*@  #
#  $ #
#  ###
####

The @ is the worker, # are walls, . are docks, $ are boxes, and so on...

The symbols are image-replaced with CSS when the level is live, but the whole thing works without CSS. You can disable the styles if you desire to play the levels in pure ASCII.

This also means that the game is printable. At any moment in gameplay you can grab a hardcopy of your status and frame it. I am hard pressed to think of other printable games. Copying the level onto the clipboard also works just as it would any other text. Not really useful features for a game, but interesting side-effects nonetheless.

Because parsing all the levels immediately as the game loads up — especially on level collections that have very many levels — is quite labour intensive, I am applying a recent jQuery plugin of mine, .processEach(), to prevent the browser from locking up. In short, it works like .each() but yields the process every iteration to allow the browser some time to do other things. It's perhaps a subject of another entry.

Have fun

Have fun with the game and the source (which are released under the GPL v3). I suspect that it might one day be exactly what a Sokoban level blog is looking for, but for now it mostly remains a curiosity rather than a spectacular reproduction of the game,

Published: 13. July 2009. Tagged: , , , .