我正在Erlang中开发一个简单的框架来处理2人回合制游戏。代码如下:
-module(game).
-export([start_server/0,generate_server/0,add_player/0,remove_player/0]).
generate_server() ->
Table_num = 0,
Player_num = 0,
io:format("Server generated...~n", []),
io:format("The current number of tables is ~w~n", [Table_num]),
io:format("The current number of players is ~w~n", [Player_num]),
receive
login ->
io:format("A new player has connected!~n", []),
New = Player_num + 1,
io:format("The current number of players is ~w~n", [New]);
logout ->
io:format("You have beeen succesfully disconnected~n", [])
end.
start_server() ->
io:format("Welcome player!~nInitializing game...~n", []),
io:format("Generating server...~n", []),
register(server,spawn(game, generate_server, [])).
add_player() ->
server ! login.
remove_player() ->
server ! logout.
运行此代码时存在两个主要问题:
任何帮助都将受到高度赞赏。
答案 0 :(得分:3)
1 / 您的服务器中没有循环。当你启动它时,在一些打印之后,它会等待接收语句。
当收到登录消息时,它执行操作,然后服务器代码完成;该过程死亡并且未注册。所有变量都会消失,VM会清理内存......
因此,稍后,任何向服务器发送消息的进程都将崩溃,因为您使用的是不再注册的名称。
要使其正常工作,您应该保留已连接的播放器列表(例如列表中)并使用此列表作为参数调用服务器循环。
generate_server(Tlist,Plist) ->
io:format("The current number of tables is ~w~n", [length(Tlist)]),
io:format("The current number of players is ~w~n", [length(Plist)]),
receive
{login,Name} ->
io:format("A new player ~p has connected!~n", [Name]),
generate_server(Tlist,[Name|Plist]);
{logout,Name} ->
io:format("You have beeen succesfully disconnected~n", []),
generate_server(Tlist,lists:delete(Name,Plist))
end.
,对generate_server的调用由
完成register(server,spawn(game, generate_server, [[],[]]))
2 / 为了在2个不同的节点之间使用erlang消息,您需要:
参见http://learnyousomeerlang.com/distribunomicon#alone-in-the-dark
的示例答案 1 :(得分:1)
您错过了服务器的loop
正文。您的程序崩溃,因为服务器只收到一条消息并退出。考虑以下服务器的另一个版本:
generate_server() -> Table_num = 0, Player_num = 0, io:format("Server generated...~n", []), io:format("The current number of tables is ~w~n", [Table_num]), io:format("The current number of players is ~w~n", [Player_num]), loop([]).那里;看起来好多了。
loop(Players)-> receive {From,{login,PlayerId}} -> io:format("A new player has connected!~n", []), io:format("The current number of players is ~w~n", [New]), NewPlayers = case lists:member(PlayerId,Players) of true -> From ! {login_failed,exists}, Players; false -> From ! {login_success,true}, [PlayerId|Players] end, loop(NewPlayers); {From,{logout,PlayerId}} -> NewPlayers = case lists:member(PlayerId,Players) of true -> From ! {logout,ok}, Players -- [PlayerId]; false -> From ! {logout,failed}, Players end,
loop(NewPlayers); _ -> loop(Players) end.