Adobe AIR Game - Hamurabi

Today I’m happy to release a little project I’ve been working on for the past few weeks - a port of the old basic game of Hamurabi. This is a game I used to play on my Apple II a long, long, long time ago. The premis of the game is simple. Given a kingdom with acres of land, bushels of grain, and a population, you decide how much land to buy or sell, how much food to give your people, and how much should be planted. Of course, what those figures mean for your kingdom is notoriously hard to figure out. The game is hard (although I made it a tiny bit easier) and intentionally vague. You will fail. I failed pretty badly too into I completely ported over the old BASIC code and saw the exact numbers used.

To make things a bit more interesting, I added a small social aspect to the game. As you play, you will see notices from other players as they play. This makes use of a demo by Mike Fleming (Adobe AIR Notification Demo). I modified his code to make his notifications dynamic. The idea was that as you played, you would see how others did and either be encouraged to beat them, or use it as an opportunity to trash talk. And yes - this makes use of the amazing BlazeDS talking to a simple ColdFusion Event Gateway.

Another change I made was to prompt you for a title and a name - this leads right into the messaging aspect so the notifications are more personal.

The actual game screen is... well... better than 1980s BASIC text input. But not much more so...

To install, simply click the fancy button below:

Ok, so let me talk about a few details - problems I had - things I learned - etc. Consider this an open list of stuff for folks to talk about or tell me how I bad I screwed up. ;)

BlazeDS and the Flex/Ajax Bridge
Working with BlazeDS and HTMl/AIR apps is - for the most part, simple. I've already talked about Stephen Moretti's excellent blog entries on it. I strongly recommend taking a look at that. The biggest issue I ran into was getting my application to send messages. I spent an hour because I simply didn't have a fault handler for my producer. Dumb. Never, ever, ever skip using a fault handler. The second I did that and saw that I wasn't specifying a destination for my messages, it didn't take long to fix. Yeah, may be obvious I guess - but it was a painful lesson.

And speaking of the bug - I wasn't able to find one lick of HTML code that showed setting up the message the correct way in an Flex Ajax Bridge application. The important line was the setHeaders below:

function send(msg) { var message = new AsyncMessage(); var data = {}; data.owner = myid; data.message = msg; message.setBody(data); message.setHeaders({"gatewayid":gatewayid}); producer.send(message); air.trace('done sending msg'); }

UI in an HTML/AIR Application
Man - I never really appreciated the simplicity of Flex's States feature until I needed in HTML. Handling the various states of my application was tricky. I made use of the Dialog control from jQuery UI, but all in all, I'm not terribly pleased with my solution. I really think there should be a more elegant way of handling this.

jQuery demos are easy. Applications are non-trivial.
So no surprise here - but as I find myself building "applications" as opposed to just simple demos, I'm finding out that I've got a lot to learn about JavaScript still. I'm reminded of my ColdFusion code back during my 2-4th years of development. I got stuff done. Big stuff. But did I code it well? Hell no. Recognizing that I need to work on my organization is at least a good sign I'd say.

Handling sending a message to everyone and ignoring it yourself.
Here is something that was a bit interesting. When I broadcast a message from the application, I want to ensure that I don't display it when it gets echoed back to all the applications. I know there is a unique ID, an originator ID, when I send messages, but I wasn't sure how I could access it on the client side. So for that, I made use of a JavaScript function that "fakes" UUIDs. I assigned one to myself and send it along with the message. When I get a message I check that before displaying it. I bet there is a better way of doing this.

Anyway, take a spin with the application and use the Download link if you want to see the complete source behind the application.

Download attached file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments