1313% % ~constructors
1414start_server ()->
1515 io :fwrite (" Server started~n " ),
16- register (game_server , spawn (ttt ,server_loop ,[[]])).
16+ global : register_name (game_server , spawn (ttt ,server_loop ,[[]])).
1717
1818new_gamer (Nickname )->
1919 io :format (" ~p~n " , [{hello , Nickname }]),
@@ -39,22 +39,21 @@ server_loop(Rooms)->
3939 BOOL = pid_exists (GPid ,Rooms ),
4040 if
4141 BOOL ->
42- io : format ( " ~p~n " , [{error , gamer_in_the_room }]),
42+ send ( GPid , " ~p~n " ,[{error , gamer_in_the_room }]),
4343 server_loop (Rooms );
4444 true ->
45- io : format ( " ~p~n " , [{ ok , room_created }]),
45+ send ( GPid , " ~p~n " ,[{ room_created }]),
4646 server_loop ([{spawn (ttt ,room_loop ,[create_board (),GPid ,[{GPid , Nickname , " .x." }]]), [GPid ]}|Rooms ])
4747 end ;
4848
4949 {find_game , GPid , Nickname } ->
5050 BOOL = pid_exists (GPid ,Rooms ),
5151 if
5252 BOOL ->
53- io : format ( " ~p~n " , [{error , gamer_in_the_room }]),
53+ send ( GPid , " ~p~n " , [{error , gamer_in_the_room }]),
5454 server_loop (Rooms );
5555 true ->
5656 find_game (GPid , Nickname , Rooms ),
57- io :format (" ~p~n " , [{ok }]),
5857 server_loop (Rooms )
5958 end ;
6059
@@ -79,29 +78,38 @@ server_loop(Rooms)->
7978 Pid = self (),
8079 receive
8180 {connect , GPid , Nickname } ->
82- draw (Pole ,keys ()),
83- room_loop (Pole ,CurrentPid ,[{GPid , Nickname , " .o." }|Gamers ]);
81+ Gs = [{GPid , Nickname , " .o." }|Gamers ],
82+ Pids = [P ||{P ,_ ,_ } <- Gs ],
83+ draw (Pids ,Pole ,keys ()),
84+ broadcast (Pids ," turn of ~p~n " , [Nickname ]),
85+ room_loop (Pole ,CurrentPid ,Gs );
8486 {leave , GPid , Nickname } ->
8587 [Nick ] = [N ||{P ,N ,_ } <- Gamers , P =/= GPid ],
86- io :format (" ~p won, because ~p left the game!~n Room closed!~n " , [Nick ,Nickname ]),
87- game_server ! {close_game , Pid };
88+ Pids = [P ||{P ,_ ,_ } <- Gamers ],
89+ broadcast (Pids ," ~p won, because ~p left the game!~n Room closed!~n " , [Nick ,Nickname ]),
90+ global :send (game_server , {close_game , Pid });
8891 {turn , GPid , Position } ->
8992 if
9093 GPid == CurrentPid ->
91- io : format ( " ~p~n " , [{error , not_your_turn }]),
94+ send ( GPid , " ~p~n " , [{error , not_your_turn }]),
9295 room_loop (Pole ,CurrentPid ,Gamers );
9396 true ->
9497 [Value ] = [V ||{P ,_ ,V } <- Gamers , P == GPid ],
95- Board = turn_execute (Pole ,Position ,Value ),
96- draw (Board ,keys ()),
97- [{_ ,N1 ,V1 },{_ ,N2 ,V2 }] = Gamers ,
98+ [Nickname ] = [N ||{P ,N ,_ } <- Gamers , P =/= GPid ],
99+ [{P1 ,N1 ,V1 },{P2 ,N2 ,V2 }] = Gamers ,
100+ Board = turn_execute (GPid ,Pole ,Position ,Value ),
101+ draw ([P1 ,P2 ], Board , keys ()),
102+ broadcast ([P1 ,P2 ]," turn of ~p~n " , [Nickname ]),
98103 V1BOOL = check_win (V1 ,Board ),
99104 V2BOOL = check_win (V2 ,Board ),
105+ V3BOOL = no_turns (Board ),
100106 if
101- V1BOOL -> io :format (" ~p won! Room closed!~n " , [N1 ]),
102- game_server ! {close_game , Pid };
103- V2BOOL -> io :format (" ~p won! Room closed!~n " , [N2 ]),
104- game_server ! {close_game , Pid };
107+ V1BOOL -> broadcast ([P1 ,P2 ]," ~p won! Room closed!~n " , [N1 ]),
108+ global :send (game_server , {close_game , Pid });
109+ V2BOOL -> broadcast ([P1 ,P2 ]," ~p won! Room closed!~n " , [N2 ]),
110+ global :send (game_server , {close_game , Pid });
111+ V3BOOL -> broadcast ([P1 ,P2 ]," Dead heat! Room closed!~n " , []),
112+ global :send (game_server , {close_game , Pid });
105113 true ->
106114 if
107115 Board == Pole ->
@@ -118,16 +126,19 @@ gamer_loop(Nickname)->
118126 Pid = self (),
119127 receive
120128 {create_game } ->
121- game_server ! {create_game , Pid , Nickname },
129+ global : send ( game_server , {create_game , Pid , Nickname }) ,
122130 gamer_loop (Nickname );
123131 {find_game } ->
124- game_server ! {find_game , Pid , Nickname },
132+ global : send ( game_server , {find_game , Pid , Nickname }) ,
125133 gamer_loop (Nickname );
126134 {turn , Position } ->
127- game_server ! {turn , Pid , Position },
135+ global : send ( game_server , {turn , Pid , Position }) ,
128136 gamer_loop (Nickname );
129- {leave } ->
130- game_server ! {leave , Pid , Nickname },
137+ {leave } ->
138+ global :send (game_server , {leave , Pid , Nickname }),
139+ gamer_loop (Nickname );
140+ {console ,String , List } ->
141+ io :format (String ,List ),
131142 gamer_loop (Nickname )
132143 end .
133144
@@ -137,17 +148,17 @@ pid_exists(Pid,[{_,[Pid,_]}|_])-> true;
137148pid_exists (Pid ,[{_ ,[_ ,Pid ]}|_ ])-> true ;
138149pid_exists (Pid ,[_ |T ])-> pid_exists (Pid ,T ).
139150
140- find_game (_ , _ , [])->
141- io : format ( " ~p~n " , [{error , free_room_not_found }]);
151+ find_game (GPid , _ , [])->
152+ send ( GPid , " ~p~n " , [{error , free_room_not_found }]);
142153find_game (GPid , Nickname , [{RPid , Pids }|T ]) ->
143154 case Pids of
144- [H |[]] -> game_server ! {change_pids , RPid , [H ,GPid ]},
155+ [H |[]] -> global : send ( game_server , {change_pids , RPid , [H ,GPid ]}) ,
145156 RPid ! {connect , GPid , Nickname };
146157 [_ |_ ] -> find_game (GPid ,Nickname ,T )
147158 end .
148159
149- find_game_and_execute_command (_ ,[],_ ) ->
150- io : format ( " ~p~n " , [{error , running_game_not_found }]);
160+ find_game_and_execute_command (GPid ,[],_ ) ->
161+ send ( GPid , " ~p~n " , [{error , running_game_not_found }]);
151162find_game_and_execute_command (GPid ,[H |T ], Command ) ->
152163 case H of
153164 {RPid ,[GPid ,_ ]} -> RPid ! Command ;
@@ -165,13 +176,13 @@ keys() ->
165176 getValue ([]) -> io :format (" ~p~n " , [{error , not_exists }]);
166177 getValue ([{_ ,V }|_ ])-> V .
167178
168- turn_execute (Board , Position , Value ) ->
179+ turn_execute (GPid , Board , Position , Value ) ->
169180 BOOL = can_pass_value (Board , Position ),
170181 if
171182 BOOL ->
172183 [{Position ,Value }|[{K ,V }||{K ,V } <- Board , K =/= Position ]];
173184 true ->
174- io : format ( " ~p~n " , [{error , not_correct_turn }]),
185+ send ( GPid , " ~p~n " , [{error , not_correct_turn }]),
175186 Board
176187 end .
177188
@@ -183,6 +194,9 @@ find_par(_,[]) -> false;
183194find_par ({K ,V },[{K ,V }|_ ]) -> true ;
184195find_par (Par ,[_ |T ])-> find_par (Par ,T ).
185196
197+ no_turns (Board ) ->
198+ [X ||{_ ,X } <- Board , X == " . ." ] == [].
199+
186200check_win (V ,Board )->
187201 BOOL1 = find_par ({11 ,V },Board ) and find_par ({12 ,V },Board ) and find_par ({13 ,V },Board ),
188202 BOOL2 = find_par ({21 ,V },Board ) and find_par ({22 ,V },Board ) and find_par ({23 ,V },Board ),
@@ -194,17 +208,25 @@ check_win(V,Board)->
194208 BOOL8 = find_par ({13 ,V },Board ) and find_par ({22 ,V },Board ) and find_par ({31 ,V },Board ),
195209 BOOL1 or BOOL2 or BOOL3 or BOOL4 or BOOL5 or BOOL6 or BOOL7 or BOOL8 .
196210
197- draw (Board , [H |T ]) ->
211+ draw (Pids , Board , [H |T ]) ->
198212 if
199213 H == s ->
200- io : fwrite ( " Board:~n " ),
201- draw (Board ,T );
214+ broadcast ( Pids , " Board:~n " ,[] ),
215+ draw (Pids , Board ,T );
202216 H == r ->
203- io : fwrite ( " ~n " ),
204- draw (Board ,T );
217+ broadcast ( Pids , " ~n " ,[] ),
218+ draw (Pids , Board ,T );
205219 H == e ->
206- io : fwrite ( " ~n " );
220+ broadcast ( Pids , " ~n " ,[] );
207221 true ->
208- io : format ( " ~p " ,[get (H ,Board )]),
209- draw (Board ,T )
222+ broadcast ( Pids , " ~p " ,[get (H ,Board )]),
223+ draw (Pids , Board ,T )
210224 end .
225+
226+ broadcast ([],_ ,_ ) -> {ok };
227+ broadcast ([H |T ],String ,List ) ->
228+ send (H ,String ,List ),
229+ broadcast (T ,String , List ).
230+
231+ send (Pid , String , List ) ->
232+ Pid ! {console , String , List }.
0 commit comments