Why I wrote simplyxiangqi.com

A long time ago, my Dad got me the game ‘Battlechess 2’. Much time my surprise, it was some strange variant called ‘Chinese Chess’, and I enjoyed it (even though I’m not much good). It doesn’t have the whole ‘pawn-shuffling’ start or drawn out end games, so it appealed.

A lot of time passed, and I became a developer. One day I was looking at an online chess site and I just thought ‘I could build this’. I had recently started learning about JSP and Servlets to work towards Java Web Developer certification. So, I sat down, worked out the logic, learnt about ‘bit boards’, and build the engine for working out valid moves, given a position. And I wrote it all in Java.

After that, all I had to do was build the website to support that engine, and at this point I stalled. Building websites out of Java was excessively hard – security filters for login, carefully planned data structures, lots of JDBC to connect to the database. To be honest, the hard problem cracked, I lost interest.

Time passed. I started to read about this new thing ‘Ruby on Rails’, and so I thought I should take a look. I went through a couple of tutorials and I was impressed. In minutes I could make a (very basic) site that dealt with security, database interaction, etc., pretty much for me. I just thought “Now this is a simple way of building that site”. So, I did.

I ported my code from Java into Ruby in about 4 hours – most of which was getting to grips with the Ruby language itself. I built a database schema, and based the site around that structure.

And that’s pretty much where simplyxiangqi came from.

Why I wrote simplyxiangqi.com

Progess on Ruby Chinese Chess site

So, I’ve been a bit busy with things, so I don’t have any of my usual interesting links to post. I’ve been getting stuff done with the Rails Xiangqi site that I’m building (Chinese chess to the rest of us). Nothing radical, just plugging away.

My biggest complaint with Rails so far is a lack of books and documentation. I mean, the API docs are pretty good, but I’d like to see more example, more tutorials, and a structured thing like a book would be wonderful.

So, what have I learnt? Well, I’ve used routes.rb to set up the default page for the site. You can also ditch that index.html file from public in your application too. I think I’ve figured out how change passwords in the security thing, which is a bit involved, so sometime I’ll post the code.

Partials – annoyingly – don’t have a ‘general’ folder. You can create one, but you have to specify it each time you call a partial. That sucks a bit.

I built a lot of the user admin stuff – login, signup, etc.. It was made very easy by the Login Generator. Updating users and stuff – well, that was only a slight change to the normal scaffold. The big change was updating the password – but even that wasn’t too bad.

All in all, still impressed by how much you can get done in a short time with Rails.

One thing I’d like to figure out is how to do a persistent Login cookie for Rails. I don’t mind having my session deleted, but having authenticated that user, I’d like to not have to do it again the next time they visit the site.

Progess on Ruby Chinese Chess site

Porting Java to Ruby

Long time ago I wrote a Java engine for figuring out valid Chinese Chess (Xiangqi) moves. I was going to build it into a website, something like Red Hot Pawn. Well, it seems that Ruby might be much quicker to develop the database and presentation parts of a website, so I decided to port my Java code across to Ruby. It was very easy, both being very object oriented. It works just fine. Now, all I have to do is the site itself – which should be quick in Ruby.

Porting Java to Ruby

Chinese Chess Engine Again

Work continues…

Gaze of Death – Kings may not face each other directly without an intervening piece

and I have this thing through to the point that it will read a FEN (a notation describing a board state, like
4KAr2/4A4/3Hr4/7h1/9/9/9/9/4aP1R1/3ak3C w---1
), create a board recording that, work out the valid moves for each piece (that is, the squares that that piece can move to), then work out what moves are ‘allowed’ – that is, the move doesn’t result in check, or causing the Gaze of Death.

I’ve used bit boards for most of it, but I haven’t precomputed them, as has been the case for some chess engines. The reason, mainly, was being unsure how to cope with ‘Blocking’ rules – that is, some pieces can be blocked by a piece up close to them. For example, Knights can’t jump – they move 1 square straight, and another diagonally. If there is a piece on the straight square, they can’t move that way.

I couldn’t figure out how to precompute this, though now I wonder if a second bit board with ‘blocked’ squares on it would have sufficed. Still, the precomputation might have been a bit much – 90 squares, times 8 for the number of ways it could be blocked is 720 combinations, plus 90 for the blocked squares masks.

Actually, it might be worth a look. Anyway, speed is not a huge issue for this right now – it’s only point is to determine valid moves, and I intend to cache the result in a database, so that valid moves can be referred to by FEN…

Chinese Chess Engine Again

Chinese Chess Engine

Okay, so few people in the West have heard of Chinese Chess, or Xiangqi, but trust me, it’s pretty cool. It doesn’t have as many Pawns, and there is a piece called the Canon that can only take if it jumps over another piece on the way.

I’m wondering about making a chinese chess website, to play online. It’s a bit trickier – chinese chess has a 9×10 board (not 8×8), so bit boards will be more complex.

But I might give it a go, building an engine. I think I have the guts of it worked out, although I’m concerned about efficiency…

Chinese Chess Engine