Writing something in C# and then rewriting it in F#, almost nobody does that. I did.

I used to write games in Silverlight. I didn't have any revenu, but I discovered that such projects are fun to do, and that you can pickup a lot of experience with all sorts of techniques, in a short time. Experience that your real day job may not provide. At some moment I quit Silverlight games. But I still appreciated the experience you can get from such projects.

To learn HTML5, I started a new game in 2012, not ever to be published, but with the background intention of being "experiment code". So the game was a real project goal - required to go beyond simple experiment. But background intention of the game, was to try-out all sorts of techniques, an ongoing experiment.

The game is "Reversi", but now for three players, under the name "Three is a Crowd". That name just popped in my mind, but if you play, then you'll notice it is indeed a bit crowded.

At some point I was writing the AI. The AI for the original Reversi is quite simple, the edges and corners of the board are strategic powerful. But the board for TIC can be very different. I created a level editor, and you can make all sorts of different boards. So all those AI rules needed to be rethought.

And at some point I was wondering, is there no easier way to write Artificial Intelligence? At that point I also remembered from my Computer Science education that functional languages are perfect for AI. And I remembered that F# was added to Visual Studio 2010. I bought a book and started learning this language.

And with this knowledge, I tried to rewrite the AI of the game in F#. And I failed, because it was illogical to have a ruleset in F#, while the context was still C#. And at that point, I rewrote the whole game in F#. Well, only the game logic which takes place at the server side, the visual parts are still non-F#.

Comparing languages

I am proud to say that I have a game, which really does stuff, in two languages, C# and F#. So how does that compare? It only compares in an unfair way.

Most comparisons which I've seen, and discussions on this topic, are along the lines of doing the same thing in both languages. But it does not work that way. The way you make a tile on the board aware of its neighbours, is done differently in C#, as opposed to F# (as explained here).

Fair comparison don't work very well, because fair means looking for something that can be compared. You could compare by adaption, where a task in language A is adapted to do technically the same as language B (which is in favour of B), or comparison by equality, where you can look for a common denominator and cutting away individual strengths. Or you can compare by competition, which may run an experienced C# against an experienced F# programmer. But it won't tell you how they perform at the office in a more relaxed environment.

When I wrote Three Is A Crowd in C#, I was (still am) an experienced C# programmer, even with C, C++ and Java background. I wasn't trying to get the best code quality, doing stuff lazy. The code was written at friday nights, perhaps with a beer. All the game logic details were still unknown and needed to be thought over and created.

When I wrote TIC in F#, I was quite an inexperienced F# programmer. I used Dictionary (in a functional way), because I didn't know Map. I already did know the details of the game logic however, so that part didn't need to be rediscovered. When I finished the game in F#, and ran it for the first time, it didn’t work. In total I solved about three bugs in the code, and then the game worked like a charm. This I did not expect. Okay, all the logic was already "discovered" via the C# project. But writing many lines of code and this little amount of bugs, still surprised me. If I had rewritten the game in Basic or something else which does not take copy/paste from the C# code, then I probably would not have seen something similar.

The F# code is about one third of the C# code in lines of code. And the F# project has much more code comments. And yes, both project don't deliver the exact same features, but still, this rough estimation stays, it won't get much worse.

Another F# feature which really appealed to me was the broad possibilities of generics. In C#, I tried to use the hole-in-the-middle pattern (without knowing its name). Nice and useful and all that. But I did not like the code. It appeared ugly to me, it does not tell you in one glance what the code does.

Applying generics in F# amazed me. I created one function to run over a line of tiles in a certain direction. It is multi purpose. One purpose is to check possible moves for a player. Another purpose is to check whether tiles should change colour when a player has made a choice.​

Three is a crowd

I finally came around in putting the code on GitHub. Don't forget to check the Readme about configurations. The game was developed using a Javascript library called "Kineticjs", which is currently retired. The game works (on my system) in the Edge browser. Chrome does not work (here) and I did not try others.

I did clean up the code, the project names and namespaces, to make it a bit more presentable, and the service refs to make it work again. I did not change the code itself, so all the mistakes I made are still in there. If I were to continue today, I would make different choices, in both worlds. But that's not what this story is about.