我正在Erlang開發一個簡單的框架來處理2人玩家回合制遊戲。該代碼是下面的:在不同的終端窗口上運行Erlang程序時的例外情況
-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(),則會出現錯誤。我應該怎麼做才能在多個終端窗口上運行它?
任何幫助將不勝感激。
非常感謝您的幫助:)我會有更多的問題:P – user1301428