Publié par : yoannr | 3 octobre 2013

First Steps with Fsharp – Modelisation of a diplomacy game


I am trying to get into functionnal programming. For the last year I have been reading and making a few aborted attempt at programing in fsharp.

My last attempt was to create a web application in fsharp. The problem is that I am missing some basic baby step before being able to do what I want. It is the same thing as learning a new language, you need to know how to say beer before even being able to get into some cool social gathering…

Well then, I am quite fond of a board game called diplomacy, mecanism are easy, or so they seems at first , let’s try to model this. First step, I am trying to create some basic board game. I need the seven countries :

type Country =
    | France
    | England
    | Germany
    | Italy
    | AustriaHungary
    | Russia
    | Turkey

Territories can be land, coast or sea and might or not have a center, this center can itself be neutral or belong to some nation.

Therefore I have modeled so far the control and the center by :

type controlledBy =
    | SomeCountry of Country
    | NoOne 

type Center =
    | SomeCenter of controlledBy
    | NoCenter

and the territories are then detailed as follows :

type territory =
    |Land of string*string*Center
    |Sea of string*string*Center
    |Coast of string*string*Center

This allows me to setup a board game as follows ( it is an example not the whole game is here defined ) :

let territories = [Land("PAR","Paris", SomeCenter( SomeCountry(France)) );
                 Coast("PIC","Picardie", NoCenter);
                 Land("BOU","Bourgogne", NoCenter);
                 Coast("BRE","Brest", SomeCenter( SomeCountry(France)));
                 Coast("GAS","Gascogne", NoCenter);
                 Coast("MAR","Marseille",SomeCenter( SomeCountry(France)));
                 Sea("MID","Ocean Atlantique",NoCenter);
                 Sea("LIO","Golfe du Lion",NoCenter);
                 Sea("MAN","la Manche",NoCenter);
                 Coast("LON", "London" , SomeCenter( SomeCountry(England)));
                 Coast("WAL", "Wales" , NoCenter);
                 Coast("YOR", "YorkShire" , NoCenter);
                 Coast("LIV", "Liverpool" , SomeCenter( SomeCountry(England)));
                 Coast("EDI", "Edinburgh" , SomeCenter( SomeCountry(England)));
                 Sea("ATL", "Atlantique Nord" , NoCenter);
                 ]

In order to get something out of this model, I have decided to write some short description out of each part of my model so far :

Countries :

let printCountry country =
    match country with 
    | France -> "France"
    | England -> "Angleterre"
    | Germany -> "Allemagne"
    | Italy -> "Italie"
    | AustriaHungary -> "Autriche - Hongrie"
    | Russia -> "Russie"
    | Turkey -> "Turquie"

Control of Center :

let printControlledBy control =
    match control with
    | SomeCountry(country)  -> printCountry country
    | NoOne -> "Personne"

Center as follows :

let printCenter center =
    match center with
    | SomeCenter(controlled) -> sprintf " a un centre qui appartient à %s" (printControlledBy(controlled))
    | NoCenter -> sprintf "n'a pas de centre"

and finally territories like this :

let printTerritory territory = 
    match territory with
    | Land(pre,name,center) -> sprintf "Le territoire %s (%s) est terrestre et %s." name pre (printCenter(center))
    | Sea(pre, name, center) ->  sprintf "Le territoire %s (%s) est marin et %s." name pre (printCenter(center))
    | Coast(pre, name, center) ->  sprintf "Le territoire %s (%s) est cotier et %s." name pre (printCenter(center))

I have then only to map my boardgame to the print function in fsi:

territories |> List.map printTerritory;;

These are just baby steps and , it allowed me to dive into the syntax of disciminated unions and option. I do hope I am on the way of going more functionnal than OO. The mindshift is not an easy one to take, it is somewhat hard to shift from some stateful way of thinking to something more on the move, but I ‘ll give it my best try.

 

Publicités

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

Catégories

%d blogueurs aiment cette page :