Archive

Posts Tagged ‘game’

A card game in erlang…continue!

December 9, 2009 2 comments

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)).

and

%%--------------------------------------------------------------------
%% 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!

Advertisements
Categories: English, Erlang Tags: , , , ,

Cleaning the code

November 2, 2009 Leave a comment

L’altro giorno mi sono messo a fare il cerca bachi per il backend del gioco della scopa…prima di implementarci altre cose sopra vorrei avere un buon livello di qualità per quel codice…ne è uscita una cosa simile

Un'immagine tratta dal libro: "Code clean"

Un'immagine tratta dal libro: "Code clean"

Alla fine il lavoro non è stato di quelli estenuanti…però ci ho messo un pò a scovare tutti i bachi piu nascosti e insidiosi..o almeno un sacco di essi!

A presto posto qualche idea per il gestore dei players……per avere il codice finale dovrete aspettare che sia tutto pronto! 🙂

Categories: Erlang Tags: , , , ,