4Blocks Source Code

Today I decided to try my hand at releasing a cabal package with the 4Blocks code. I finally managed: http://hackage.haskell.org/package/4Blocks-0.2. (I made a newbie mistake in 0.1 so please ignore that release).

Unfortunately the library can be compiled only with, as far as i know:

  • GHC 6.8.3
  • Gtk2hs 0.9.13

The reasons why are in the README file in the cabal package, copy/pasted here:

Some notes:
– The game currently works only with GHC 6.8.3 due to its use of Gtk2hs 0.9.13.
– I haven’t tested with anything later but it is likely to fail due the fact that later versions of Gtk2hs have a different system of handling events.
– To make this project compatible with later versions of GHC and Gtk2hs two changes are required:
– Remove the function “permutations” which was copied from a later GHC base library
– Alter key-event handling to the version used by later Gtk2hs: some functions (in CommandKeys.hs) were simply introduced in order to disallow some of the keys used in the game (namely rotation) to trigger continuously when a key is held pressed. I believe this kind of behaviour can be managed automatically with Gtk2hs’ new event handling mechanism, however I haven’t had time to recode accordingly myself.
– I hope to write a patch for this in the near future.

So yeah, fairly old now, but I thought, I’d share the code all the same as some people have requested it earlier. Hopefully I will release the code with my AI later on after my project is over and done with in summer and also a patch to make it work with newer versions of GHC and Gtk2hs.

I have to say I really enjoyed coding the game in Haskell and if you have any comments for me, regarding better ways to code stuff, silly things I did, or anything else please let me know by leaving a comment!

4Blocks with AI

I’ve been wanting to write another blog post on the series on DSELs I’ve been planning, but I’ve been really swamped with work lately. So once more I’ll post a video or two on something I’ve been working on. It’s the 4blocks game again, this time playing on its own with an AI I’ve written for it. Here are the videos:


On the whole these were two good runs. I’ve had much better and much worse runs of course.

The AI is by no means perfect but it uses the DSEL techniques in Haskell. I have two languages acting here:
1. A Tetris DSEL
2. Haskell acting as a meta-language over the Tetris DSEL.

What is happening is that for every new brick the game state is queried by Haskell for certain information such as occupied blocks or number of lines. Using this information, by means of Haskell’s case statement I simply trigger one of a possible number of strategies. These strategies acquire certain information from the game state and generate a DSEL script. This script is then carried out by a DSEL script interpreter.

There are numerous problems with the AI and its mostly because I haven’t had time to write more strategies or a more intelligent way to select what according to the selected strategy is the best final brick destination.

That’s about it for now. Let me know what you think.

4Blocks in Haskell!

This week, instead of a post on DSELs, I’m going to show what I’ve been working on. It’s my first, real foray into Haskell: a game implementation. Some day soon I hope to make my implementation available on the Haskell libraries repository (Hackage).

Thanks goes to the folk at the Haskell irc channel #haskell who helped me when I got stuck. Special mention goes to Saizan for his everlasting patience.

Edit:
Some more details:
The game is being played by me in single player mode. The other parts shown are WIP. The graphics library used if anyone is interested is Gtk2hs.

Edit 2:
In light of a comment by one of the visitors of the blog (which was also a nagging worry of mine), and some searching on my side I decided to alter the game’s name and certain minor features to prevent copyright problems. I have uploaded a new video which reflects these changes.

Edit 3:
4Blocks code released! Check here and here for more info! 🙂