bridge update (or: programming is hard)
Mood: worried
Posted on 2010-10-27 14:00:00
Tags: bridge projects programming
Words: 477

After vacation and traveling and such, I've been working on the bridge program again. I'm at the point in the webOS client where I can now play a complete hand, bidding and all. It even looks relatively nice with cards sliding around, things fading into view, etc. Ask me to show it off if you're interested sometime!

So the next task is adding the AI for playing cards. I've decided to go with a rule-based system, where there are a bunch of rules that can decide if they apply. So I have "second hand low" and "third hand high" rules that go near the end (since they're fairly generic).

Last night I tried to sit down and really hammer out the rule that applies if you can determine exactly what will be played. If you're the last player on the trick, this is relatively easy - see if your partner is winning, if not win as cheaply as you can, if so (or if you can't win) throw some trash.

Even this is a little vague - what card should I throw away? Well, if there's only one suit I can play it's easy, but if not then it's hard. So I put this off with a TODO.

The tricky part is if you're the third player on the trick and the fourth player is the dummy. Then it's something like: look at all dummy's cards and the declarer's card that was played (if it's currently winning), then see if I can beat all these. If so, play the cheapest. If not....well, we are in the third hand and we'd like to force dummy's best card if we can. So find dummy's second best card and see if we can at least beat that. If not...well, at least we should try to beat the current high card played by declarer. If we can't do that, then just toss something.

--

This took a while to code up, and I need to test it thoroughly because I'm very much not confident in it. And it still leaves a lot of details out - what if dummy has AK6 and I have the 57 - under the algorithm I'd play the 5 which is pretty clearly wrong. It seems like to handle this case I should have been keeping track of which cards are winners in their suit, which is another layer of complexity!

And all this is for a (all things considered) pretty simple case where I can see all the cards. I'd also like to keep track of how many cards of each suit each person has (based on the bidding), which would help but would also add enormous complexity.

Maybe the AI should cheat and see all the cards? Is there some better thing to keep track of?

It could be a long time before the game seems to play intelligently...


2 comments

Comment from omega697:
2010-10-27T14:51:35+00:00

How about using someone else's bridge AI?

Comment from gregstoll:
2010-10-27T15:43:56+00:00

Not a bad idea! But
- I'd like to sell this app on the App Catalog, so it would have to be BSD-licensed or similar.
- I'd have to port it to Javascript, which is certainly not impossible
- I was hoping that writing my own would be enlightening. Which it has been, but I don't want it to take years.

So maybe I'll look around...know of any off the top of your head? :-)

This backup was done by LJBackup.