SameGame with F# and Fable: Functional programming meets JavaScript

Fable is a compiler for F# to JavaScript.

It brings all the good parts of functional-first programming in F# to JavaScript development.

Even without a lot of knowledge of Node.js and the JavaScript ecosystem, it is fairly easy to get started with and use functional-first programming for client-side browser applications. Fable can also be used for client-server, Node, mobile or desktop applications.

After reading the very nice introductory tutorial Getting started with Fable and Webpack I was ready to make an implementation of SameGame.

It is integrated in this post and you can play it right here in the browser.

This sample was also published on Fable’s official sample site where you can find more information on the implementation details.

Play SameGame

width

height

colors

select game

0 point(s).

Rules

SameGame is a single player game. It is played on a two-dimensional board filled with stones of different colors.

The player may remove groups of stones from the board.

A group of stones is defined by two or more orthogonally connected identical-colored stones.

After a group is removed, all the stones above will fall down.

If a column is cleared completely, the columns to the right will slide to the left to close the gap.

The game ends when the board is either empty or the remaining stones cannot be removed.

Scoring

Removing a group of n stones will result in a score of ~(n-2)^2 points.

If all stones are removed from the board, the player will receive a bonus of 1000 points.

If the game ends without clearing the board, the player will receive a penalty. The penalty is computed according to ~(n-2)^2 where n is the number of stones left on the board.

Source Code

The complete code is available on GitHub.