diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-05 21:28:46 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-05 21:28:46 +0100 |
commit | 900bda5f4c60898c07e81ff25be57ffc835b68d8 (patch) | |
tree | 89bdc84b235c551f235aab6dd1a56d0cfb36eae8 /tictactoe-2/apps/ttt_server/src/game.erl | |
parent | c34d7363b61a9e00c986b79793bf7cdc03e9ea99 (diff) | |
download | erlang-workshop-900bda5f4c60898c07e81ff25be57ffc835b68d8.tar.gz erlang-workshop-900bda5f4c60898c07e81ff25be57ffc835b68d8.tar.bz2 erlang-workshop-900bda5f4c60898c07e81ff25be57ffc835b68d8.tar.xz erlang-workshop-900bda5f4c60898c07e81ff25be57ffc835b68d8.zip |
wip
Diffstat (limited to 'tictactoe-2/apps/ttt_server/src/game.erl')
-rw-r--r-- | tictactoe-2/apps/ttt_server/src/game.erl | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tictactoe-2/apps/ttt_server/src/game.erl b/tictactoe-2/apps/ttt_server/src/game.erl new file mode 100644 index 0000000..8c6550a --- /dev/null +++ b/tictactoe-2/apps/ttt_server/src/game.erl @@ -0,0 +1,36 @@ +-module(game). +-author("trygvis"). + +-export([ + game_loop/2]). + +-import(ttt, [empty_board/0]). + +-record(state, { + id, + board = ttt:empty_board(), + player1, + player2}). + +game_loop(Id, Player1) -> loop(#state{ + id = Id, + player1 = Player1}). + +loop(State) -> + Id = State#state.id, + receive + {From, join} when not is_pid(State#state.player2) -> + io:format("game ~p: player joined: ~p~n", [Id, From]), + From ! {joined, State#state.id}, + loop(#state{player2 = From}); + {From, join} -> + io:format("game ~p: busy: ~p~n", [Id, From]), + From ! {busy}, + loop(State); + {From, show} -> + Str = ttt:format(State#state.board), + io:format("game ~p: current state:~n~p~n", [Id, Str]), + From ! Str, + loop(State) + end, + State. |