Sunday, 8 May 2011

Programming: Java text board game

Gor, it must be getting on for a year, since I uploaded that game what I wrote in Perl to SourceForge, you can download it from here if you're into that sort of thing. Its a bit like the programs I used to type in from Amstrad Action or Amstrad Computer User.
Anyhoo, I've now ported it across in Java, and tidied it up a bit, making the code a bit more concise. Its taken me a while because I didn't actually know how to program in Java, luckily I managed to borrow this book from my girlfriend's sister.

Its all right, not too confusing, I'm not even halfway though but I've got enough of it in me to port perl to java.

The basic premise of the game is kind of like checkers, apart from you add one new piece to the board every turn and the stacks explode. Actually, its only like checkers in that its played on a 8x8 board and you can stack pieces, other than that, its like in games like Doom or Halo where you can blow up oil drums and they set of nearby oil drums in a chain reaction.

It must have been back in 1993 when a friend showed me a version of the game on his Acorn Archimedes, and then a few months later I was going through all my old ACU magazines typing in all listings and ended up with the same game. I hadn't see it since so I figured I could create my own version on modern computers.

For a player versus player game, it game works like this basically

The main difference I found between perl and Java was that in the latter you have to is statically typed whilst the former is dynamically typed. I think that means you have to define what sort of data all the variables are, whether they're integers or strings and so on. Also, with things like arrays you have to state how long they are before you use them, rather than just adding stuff into an array whenever you want. And lastly getting user input from the keyboard is a little bit more different in java than in perl. It took me about a week to figure it out.

Anyhoo, once I'd nailed all that it was easy. The gameboard is a great three dimensional integer array, 10x10, but it ignores the border squares and uses one layer for each player. You play a turn, it checks whether there's any 'explosions', explodes them and then checks again until its exhausted its explosives, then the next player takes a turn. This repeats until one player or the another has been annihilated or someone quits.

Then for playing against the computer it does the same thing but substitutes in a routine for chosing where to play. It builds itself a little list of what grid squares do the most damage, which are most aggressive, or which are safest, and then randomly selects the most appropriate.

Games take around twenty minutes to play, or maybe 200 turns. I've managed to win in 140 turns, which I guess is the closest thing to a high-score at this stage.

If you want to play, or just peer at my well leet coding, you can download the source code from this page.

ChainOfWar is the java version
plainofwec is the perl version

You'll need to compile the code to get it to work. If you don't know what that means, then you ought to have a long hard think about what you want to get out of life. Do you want to be a creator or a consumer? Do you personally want to leave the world a better place than how you found it. Do you want to grow as a person, or just wallow until dementia hits.

Anyhoo, I have a plan. Whilst this it just a command-line/text based version of the game, I'm going to use java's gridbag class to do a windows version that ought to be a bit more fun to play, then I'd going to do some kind of web applet version so you can play it online. Then, I know, crazy, I reckon its just the same learning curve to do an android and iPhone version that I can sell and become one of these millionaires.

No comments:

Post a Comment