On games and PHP objects

You're reading Mikko Saari's blog Life and opinions. This entry was written 01/10/2005, at 10:12.

If you want read more of my entries in the same topic, this entry belongs to the category of Geek stuff.

Previous entry: Dawn of the Dead
Next entry: Fluff and time travel

I've been reading Jonathan Schaeffer's book on his Checkers computer Chinook and it inspired me to do some game programming. The book (One Jump Ahead : Challenging Human Supremacy in Checkers, Springer Verlag, ISBN 0-387-94930-5) is excellent, by the way, and I will cover it later in better detail (probably in Gameblog) after I finish it.

Anyway, I wanted to do some game AI programming. To do a game AI, I need a game for the AI to play. As it goes these days, it means a web implementation of a game or another. I chose Geschenkt - it's hardly a challenge AI-wise, but I wanted to do something simple anyway. Besides, I'd like a web version of Geschenkt.

I've always thought that object-oriented programming would be a good basis for a game program. After all, games consist of different objects that interact with each other. I've toyed with the idea before, but now I actually started writing code. My game consists of two classes of objects: there's Game that contains information about the game state and Players, who do what the Game says. Game asks the players for move, telling the current situation of the game as parameters. Players use that information, formulate a move and return that to the Game.

Simple, and it actually works pretty well. It has other benefits, too. Saving the game state couldn't be easier. Instead of a complicated SQL table, my game state table has exactly two columns: id and data, which holds serialized game objects. That's it.

However, it also has problems. I've done something with objects in PHP before, but most of my object-oriented experience comes from Java, where objects are both natural and mandatory. Handling objects is more complicated in PHP and I really had to struggle with references to get everything work well. The problems were of annoying nature: I'd set a value to an object attribute somewhere, but it wouldn't carry on so I could use it elsewhere. I got it all sorted out in the end, however.

While the object-based structure was even elegant, it didn't work too well with the fact that the application would be run over web. The result is the single most bizarre piece of web code I've written. There's the main script, which initiates the games and passes variables, but most of the action happens in a class method, somewhere in include files. That's not good and kind of undermines my object method, but I can understand it and it works. That's enough.

I got the game running pretty smoothly now. What needs to be done is loads of interface work, boring basic website tasks like user management and finally, the main reason for this project, AI coding! Then I'll have a nice web version of Geschenkt I can show to other people. Temptation to keep it mine alone is strong, because that would mean I could forget nice user interfaces and user management.

In the end I will have a object structure I can probably alter to create other similar games. That should be fairly easy (but in reality it won't be, of course).

Trackback Pings

TrackBack URL for this entry:


You *do* realise that if I play it, I will send you lots of annoying emails insisting on user interface improvements?


Posted by: Iain Author Profile Page at January 10, 2005 11:36 AM

That was one of the reasons I didn't include a link. I do need some interface ideas, but their time isn't yet.

Posted by: Mikko Saari Author Profile Page at January 10, 2005 12:02 PM

Post a comment

Remember Me?