fork(2) download
  1. -module(prog).
  2. -export([main/0, play/1]).
  3.  
  4. -define(RULES, [{spock, [scissors, stone]},
  5. {lizard, [spock, paper]},
  6. {paper, [spock, stone]},
  7. {stone, [lizard, scissors]},
  8. {scissors, [lizard, paper]}]).
  9.  
  10.  
  11. main() ->
  12. play(spock),
  13. play(pizda_tvoei_mamki),
  14. play(scissors),
  15. play(scissors).
  16.  
  17.  
  18. play(Item) ->
  19. Members = proplists:get_keys(?RULES),
  20. case lists:member(Item, Members) of
  21. false ->
  22. io:format("Wrong item passed ~n");
  23. _ ->
  24. random:seed(now()),
  25. EnemyItem = lists:nth(random:uniform(length(Members)), Members),
  26. case fight(Item, EnemyItem) of
  27. draw ->
  28. io:format("Draw ~n");
  29. {win, Item} ->
  30. io:format("You win with ~p against ~p~n", [Item, EnemyItem]);
  31. _ ->
  32. io:format("You loose with ~p against ~p~n", [Item, EnemyItem])
  33. end
  34. end.
  35.  
  36.  
  37. fight(X, X) -> draw;
  38. fight(X, Y) ->
  39. case lists:member(Y, proplists:get_value(X, ?RULES)) of
  40. true ->
  41. {win, X};
  42. _ ->
  43. {win, Y}
  44. end.
Success #stdin #stdout 0.22s 8448KB
stdin
Standard input is empty
stdout
You loose with spock against paper
Wrong item passed 
You loose with scissors against stone
You loose with scissors against stone