Skip to content

Instantly share code, notes, and snippets.

@ferd
Created July 13, 2014 21:43
Show Gist options
  • Save ferd/35a4e9af633339150411 to your computer and use it in GitHub Desktop.
Save ferd/35a4e9af633339150411 to your computer and use it in GitHub Desktop.
-module(demo).
% ...
check(Board) ->
case winner(Board) of
{victory, Player} -> % Player wins;
undefined ->
case is_full(Board) of
true -> draw;
false -> keep_playing
end
end.
winner(Board) of
case Board of
{x, x, x,
_, _, _,
_, _, _} -> {victory, x};
{x, _, _,
_, x, _,
_, _, x} -> {victory, x};
{x, _, _,
x, _, _,
x, _, _} -> {victory, x};
%% [snip]
_ -> undefined
end.
is_full(Board) ->
lists:all(fun(X) -> X =/= undefined end, tuple_to_list(Board)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment