Home > English, Erlang > A card game in erlang…continue!

A card game in erlang…continue!

Here I am with a new post!

A couple of days ago, I had some time to fix my erlang environment in my new Ubuntu Karmic Koala, so I decided to continue with the development of my cards game in erlang.

If you remember from my previous posts, I did a process for handling the table, it was a gen_server providing a basic interface for a set of actions users may want to perform, as “play a card on the table”, “take a single card/a set of cards from table”, “give me 10 cards” and so on…

This process was also responsible for checking whether the action user wants to do is allowed (e.g. is the card user wants to take in the table), so I thought that next step would have been the creation of another gen_server.

In my mind this server should have listened to a specific port waiting for users; since “scopa” is a game played by 4 users, the server should have waited until 4 users joined the game, than it would have started a new table process for these users.

Now I’m going to write some code, and discuss it with you.

Let’s start with some handle_calls for our gen_server:

handle_call({add, Socket}, _From, State) ->
    NewSockets = [Socket | State#state.sockets],
    Reply = ok,
    NewState = #state{sockets=NewSockets},
    {reply, Reply, NewState};

The previous handle_call adds a new socket to the list of sockets stored in the state of the gen_server; I use the sockets to distinguish each player.

handle_call(get_players_number, _From, State) ->
    PlayersNum = length(State#state.sockets),
    Reply = PlayersNum,
    {reply, Reply, State};

The previous handle_call is used to retrieve the number of users who joined already the game.

handle_call(get_players_sockets, _From, State) ->
    PlayersSockets = State#state.sockets,
    Reply = PlayersSockets,
    {reply, Reply, State};

The previous handle_call is used to retrieve the list of sockets in the game (we can use this to understand whether the socket sending commands is a valid user)

handle_call(get_player_turn, _From, State) ->
    [PlayerSocket | _Others] = State#state.sockets,
    Reply = PlayerSocket,
    {reply, Reply, State};

The previous handle_call is used to retrieve wich user/socket is the next to perform an action (so we can manage turns)

handle_call(update_turn, _From, State) ->
    [PlayerSocket | Others] = State#state.sockets,
    NewSockets = Others ++ [PlayerSocket],
    NewState = #state{sockets=NewSockets},
    Reply = ok,
    {reply, Reply, NewState}

The last handle_call is used to update the turn list after a player submit an action.

Let’s say that a player sends a command to our server…before forwarding this command to the table we must check whether the user who sent the command is a player of the match and whether he is the player we expect to play (turn); we may want to use two functions like:

%% Func: is_player(Socket) -> true | false
%% Description: Given a socket checks whether it represents a user in
%%              current match
is_player(Socket) ->
    lists:member(Socket, gen_server:call(?MODULE, get_players_sockets)).


%% Func: is_player_turn(Socket) -> true | false
%% Description: Given a socket checks whether the user it represents
%%              is the one expected to play
is_player_turn(Socket) ->
    Socket == gen_server:call(?MODULE, get_player_turn).

Next time new stuff! Stay tuned!

Categories: English, Erlang Tags: , , , ,
  1. Werner Thie
    March 5, 2010 at 6:18 pm


    interesting post, interesting because I’m writing browser (solely JScript) based card games for a living. If you want to have a look see


    feel free to comment. I’m currently revamping the chat component to use XMPP based services, a field you also seem to be fluent with.

    Thxs for your posts, Werner

  2. pdincau
    March 8, 2010 at 7:40 am


    actually my idea for the system was pretty similar to the one you wrote in the post…the backend system will be only in erlang (I hope)…the connection to it will be done through AJAX…and I think that your idea about a chat in xmpp is really interesting (and simple to do!).

    unfortunately during these months I have had really really few time..but I hope that my schedule will let me some free hours to code more!


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: