diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-07 22:14:10 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-07 22:14:10 +0100 |
commit | 911547d0e4e3838fa71cc3f71e5f472114e01431 (patch) | |
tree | b62cee7709fe159a6765378748c7ef28514178fb | |
parent | d6b2edd8cef2a3ab1bb9488fe7e82920a1333af1 (diff) | |
download | erlang-workshop-911547d0e4e3838fa71cc3f71e5f472114e01431.tar.gz erlang-workshop-911547d0e4e3838fa71cc3f71e5f472114e01431.tar.bz2 erlang-workshop-911547d0e4e3838fa71cc3f71e5f472114e01431.tar.xz erlang-workshop-911547d0e4e3838fa71cc3f71e5f472114e01431.zip |
wip
-rw-r--r-- | tictactoe-2/apps/ttt/src/ttt.erl | 3 | ||||
-rw-r--r-- | tictactoe-2/apps/ttt_server/src/game.erl | 2 | ||||
-rw-r--r-- | tictactoe-2/apps/ttt_server/src/ttt_server.erl | 18 | ||||
-rw-r--r-- | tictactoe/ttt/src/ttt.erl | 3 |
4 files changed, 19 insertions, 7 deletions
diff --git a/tictactoe-2/apps/ttt/src/ttt.erl b/tictactoe-2/apps/ttt/src/ttt.erl index a9fc4d1..f030f03 100644 --- a/tictactoe-2/apps/ttt/src/ttt.erl +++ b/tictactoe-2/apps/ttt/src/ttt.erl @@ -45,7 +45,8 @@ move(Board, Move, Row, Col) -> Updated = lists:sublist(Board, I) ++ [Move] ++ lists:nthtail(I + 1, Board), {ok, Updated}. +-spec format(board()) -> io_lib:chars(). format(Board) when length(Board) == 9 -> B = lists:map(fun(C) -> case C of 'E' -> ' '; _ -> C end end, Board), - io:format("+---+~n|~s~s~s|~n|~s~s~s|~n|~s~s~s|~n+---+~n", B); + io_lib:format("+---+~n|~s~s~s|~n|~s~s~s|~n|~s~s~s|~n+---+~n", B); format(_) -> "bad board". diff --git a/tictactoe-2/apps/ttt_server/src/game.erl b/tictactoe-2/apps/ttt_server/src/game.erl index 1b8aff2..608b5a8 100644 --- a/tictactoe-2/apps/ttt_server/src/game.erl +++ b/tictactoe-2/apps/ttt_server/src/game.erl @@ -30,7 +30,7 @@ loop(State) -> loop(State); {From, show} -> Str = ttt:format(State#state.board), - io:format("game ~p: current state:~n~p~n", [Id, Str]), + io:format("game ~p: current state:~n~s~n", [Id, Str]), From ! Str, loop(State); code_changed -> diff --git a/tictactoe-2/apps/ttt_server/src/ttt_server.erl b/tictactoe-2/apps/ttt_server/src/ttt_server.erl index a0de947..52a7957 100644 --- a/tictactoe-2/apps/ttt_server/src/ttt_server.erl +++ b/tictactoe-2/apps/ttt_server/src/ttt_server.erl @@ -36,8 +36,8 @@ loop(State) -> loop(#ttt_state{ seq = Id + 1, games = orddict:append(Id, Game, State#ttt_state.games)}); - {Player, show, Ref} -> - case orddict:find(Ref, State#ttt_state.games) of + {Player, show, Id} -> + case orddict:find(Id, State#ttt_state.games) of error -> Player ! no_such_game; {ok, [Game]} -> @@ -45,6 +45,14 @@ loop(State) -> Game#ttt_game.pid ! {Player, show} end, loop(State); + {Player, join, Id} -> + case orddict:find(Id, State#ttt_state.games) of + error -> + Player ! no_such_game; + {ok, [Game]} -> + Game#ttt_game.pid ! {Player, join} + end, + loop(State); dump -> io:format("Server state:~n~p~n", [State]), loop(State); @@ -78,7 +86,7 @@ start_game(Server) -> X after 100 -> io:format("timeout~n", []), - {timeout} + timeout end. dump(Server) -> @@ -103,6 +111,8 @@ stop(Server, Pid) -> stop() -> case global:whereis_name(?GLOBAL_NAME) of - undefined -> io:format("Server not running~n"); + undefined -> + io:format("Server not running~n"), + not_running; Pid -> Pid ! {stop, self()} end. diff --git a/tictactoe/ttt/src/ttt.erl b/tictactoe/ttt/src/ttt.erl index a9fc4d1..f1de18c 100644 --- a/tictactoe/ttt/src/ttt.erl +++ b/tictactoe/ttt/src/ttt.erl @@ -45,7 +45,8 @@ move(Board, Move, Row, Col) -> Updated = lists:sublist(Board, I) ++ [Move] ++ lists:nthtail(I + 1, Board), {ok, Updated}. +-spec format(board()) -> chars(). format(Board) when length(Board) == 9 -> B = lists:map(fun(C) -> case C of 'E' -> ' '; _ -> C end end, Board), - io:format("+---+~n|~s~s~s|~n|~s~s~s|~n|~s~s~s|~n+---+~n", B); + io_lib:format("+---+~n|~s~s~s|~n|~s~s~s|~n|~s~s~s|~n+---+~n", B); format(_) -> "bad board". |