我正在用 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.
运行此代码时有两个主要问题:
- 当我执行 add_player() 然后 remove_player() 时,第二个函数崩溃并出现异常
- 如果我在一个终端窗口上启动程序,然后在第二个终端窗口上执行 add_player() ,我会收到错误消息。我应该怎么做才能在多个终端窗口上运行它?
任何帮助将不胜感激。