Pig the dice game: Difference between revisions

m
Moved printouts from game to player.
(Added Erlang)
m (Moved printouts from game to player.)
Line 892:
 
=={{header|Erlang}}==
LotsSome of the code (ex: hold/2, quit/2, roll/2) is to make the next step (the Player) easysimpler.
<lang Erlang>
-module( pig_dice ).
Line 920:
Play = erlang:spawn( fun() -> play_loop( Game ) end ),
receive
{pig_resultpig, Result, Game} ->
erlang:exit( Play, kill ),
task_display( Result ),
Result
end.
Line 933 ⟶ 934:
end.
 
game_loop( [], _Goal, Report_pid ) -> Report_pid ! {pig_resultpig, game_over_all_quite. erlang:self()};
game_loop( [#player{name=Name}=Player | T]=Players, Goal, Report_pid ) ->
receive
Line 955 ⟶ 956:
 
game_loop_next_player( Total, [Player | T], Goal, Report_pid ) when Total >= Goal ->
Report_pid ! {pig_resultpig, [{X#player.name, X#player.total} || X <- [Player | T]]. erlang:self()};,
io:fwrite( "Winner is ~p with total of ~p~n", [Player#player.name, Total] ),
[];
io:fwrite( "Then follows: " ),
[io:fwrite("~p with ~p~n", [X#player.name, X#player.total]) || X <- lists:keysort( #player.total, T)],
Report_pid ! {pig_result, [{X#player.name, X#player.total} || X <- [Player | T]]};
game_loop_next_player( Total, [Player | T], _Goal, _Report_pid ) ->
T ++ [Player#player{score=0, total=Total}].
Line 982 ⟶ 981:
play_loop_next( 0, _Name, _Game ) -> io:fwrite( "~nScore 0, next player.~n" );
play_loop_next( _Score, Name, Game ) -> play_loop_command( io:fread("Roll again (y/n/q): ", "~s"), Name, Game ).
 
task_display( Results ) when is_list(Results) ->
[{Name, Total} | Rest] = lists:reverse( lists:keysort(2, Results) ),
io:fwrite( "Winner is ~p with total of ~p~n", [Player#player.nameName, Total] ),
io:fwrite( "Then follows: " ),
[io:fwrite("~p with ~p~n", [N, T]) || {N, T} <- Rest];
task_display( Result ) -> io:fwrite( "Result: ~p~n", [Result] ).
</lang>
{{out}}
Anonymous user