Question

I'm building a simple console game in Erlang. I've been writing a few Erlang modules and tests for the past 2 days and everything is working correctly (in the individual modules). I'm confused as to how to run the game and get the modules to work together.

I've been reading the "Greater Good" book and have read quite a bit about OTP, but it feels overwhelming. Is there a simple tutorial that I can follow somewhere? Are the OPT examples in the book overkill for what I am trying to do?

UPDATE: Thank you for the feedback. Here is a more specific question.

I have two modules.....

Console_IO Module

-module(console_io).
-export([display/1, display_welcome_message/0, gets/1]).

display(Message) ->
  io:format(Message).

gets(Message) ->
  {ok, [X]} = io:fread(Message, "~s").
  io:format("You entered '~s'.", [X]);

display_welcome_message() ->
  display("Welcome to the game!").

Secret Code Module

-module(secret_code).
-export([generate/0, symbols/0]).

generate() ->
  random:seed(now()),
  [lists:nth(random:uniform(6), symbols()) || _ <- lists:seq(1, 4)].

symbols() ->
  ["b","g","p","r","y","o"].

These two modules are in different files. I want these modules to talk to each other so that the user can enter a guess, attempting to guess the secret code and the game return a "Yes" or "No".

I want to add two lines to my "gets" statement to generate the secret code and compare it to the guess:

-module(console_io).
-export([display/1, display_welcome_message/0, gets/1]).

display(Message) ->
  io:format(Message).

gets(Message) ->
  {ok, [X]} = io:fread(Message, "~s"),
  Code = secret_code:generate,
  Code -- [X].

display_welcome_message() ->
  display("Welcome to the game!").

I know I'm not on the right track. Is there a simple way to do this?

Was it helpful?

Solution

Code = secret_code:generate, should be called as a function call Code = secret_code:generate(),. Compare can be done using pattern matching. Code as follows

-module(console_io).
-export([display/1, display_welcome_message/0, gets/1]).

display(Message) ->
  io:format(Message).

gets(Message) ->
  {ok, [X]} = io:fread(Message, "~s"),
  Code = secret_code:generate(),
  check_code(X, Code).

check_code(Code, Code) ->
    yes;
check_code(_, _) ->
    no.

display_welcome_message() ->
  display("Welcome to the game!").

Note the difference in secret_code to get it in string instead of returning a list of strings

-module(secret_code).
-export([generate/0, symbols/0]).

generate() ->
  random:seed(now()),
  [lists:nth(random:uniform(6), symbols()) || _ <- lists:seq(1, 4)].

symbols() ->
  "bgpryo".

Example run:

1> console_io:gets(">").
>bgryo
yes
2> console_io:gets(">").
>bgryj
no
3>

OTHER TIPS

You are already calling:
format function of io module in your console_io module io:format(Message)
seed function of random module from your secret_code module random:seed(now()).
The same way you will call generate function of secret_code module which looks like: secret_code:generate().

You can read LearnYouSomeEralng tutorial online, one of the best tutorials for Erlang/OTP and complete in all respect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top