views:

618

answers:

2

I'm writing a Mahjong Game in C# (the Chinese traditional game, not the solitaire kind). While writing the code for the bot player's AI, I'm wondering if a functional language like F# would be a more suitable language than what I currently use which is C# with a lot of Linq. I don't know much about F# which is why I ask here.

To illustrate what I try to solve, here's a quick summary of Mahjong:

Mahjong plays a bit like Gin Rummy. You have 13 tiles in your hand, and each turn, you draw a tile and discard another one, trying to improve your hand towards a winning Mahjong hand, which consists or 4 sets and a pair. Sets can be a 3 of a kind (pungs), 4 of a kind (kongs) or a sequence of 3 consecutive tiles (chows). You can also steal another player's discard, if it can complete one of your sets.

The code I had to write to detect if the bot can declare 3 consecutive tiles set (chow) is pretty tedious. I have to find all the unique tiles in the hand, and then start checking if there's a sequence of 3 tiles that contain that one in the hand. Detecting if the bot can go Mahjong is even more complicated since it's a combination of detecting if there's 4 sets and a pair in his hand. And that's just a standard Mahjong hand. There's also numerous "special" hands that break those rules but are still a Mahjong hand. For example, "13 unique wonders" consists of 13 specific tiles, "Jade Empire" consists of only tiles colored green, etc.

In a perfect world, I'd love to be able to just state the 'rules' of Mahjong, and have the language be able to match a set of 13 tiles against those rules to retrieve which rules it fulfills, for example, checking if it's a Mahjong hand or if it includes a 4 of a kind. Is this something F#'s pattern matching feature can help solve?

A: 

There's nothing you can't make yourself that appears in another language.

I've tried to make AI using java before, based off what I'd done in Prolog. I thought it would be a bitch to code. However, I just had a couple of methods that did a lot of the grunt work, taking it out of the main methods, and it worked wonderfully.

You may need to reinvent the wheel, but there shouldn't be much you can't do in C# that you can in F#.

note: I've never heard of F# before, but it can't be that bad. I may/may not be blowing out of my own arse.

glasnt
+7  A: 

If you're familiar with functional languages, they're a great way to write game AIs -- and if you aren't, the challenge of learning one will help you grow, and leave you a better programmer than you were. (I could truthfully say the same for declarative Prolog-like languages, and dynamic scripting/OO/multi-paradigm languages such as Ruby or Python!-).

Your task as you describe it should be easy in any of these groups of languages -- so pick one and go for it! We'll collectively be happy to help with any questions that should spring from these attempts (I'm personally unfamiliar with F# or Scala, but would be happy to help with Haskell, any ML-family language, Scheme, or Erlang -- and similarly for the other groups;-).

Seriously: full command of at least one language in each broad category (procedural, functional, declarative/clause unification, relational, dynamic/multi-paradigm, etc) makes you a seriously better programmer -- mahjong apart (and it's a classically popular game in the Romagna region of Italy, close to my hometown Bologna;-), any task that can add to your roster in this respect is well worth undertaking!!!

Alex Martelli