erlang设计的一款纸牌游戏
我想与您讨论 erlang 中纸牌游戏的可能实现。 我在网上找到的唯一完整示例是 OpenPoker。
我想自己创建一个,所以这是我想到的实现:
代表一副牌的 gen_server:启动时创建一副牌(洗牌)。并将其存储在其状态中。提供一个handle_call(draw_card)
一个gen_server来代表聊天室。在其状态中存储玩家进程的注册名称(例如,player1、player2、luke 等)。导出handle_cast以加入聊天(当有人成功加入游戏时默认执行)和一个通过调用代表玩家的gen_server上的handle_cast向所有用户广播聊天消息的handle_cast。
一个 gen_fsm 代表一个游戏实例。有两种状态(wait_join 和turn)。导出 join/1 加入游戏、play_card/2 和 send_msg/2。其中一个参数是玩家进程的pid。
代表玩家的gen_server。仅导出 start_link/1,其中参数是用于注册进程的名称(在 init 内我调用 gen_fsm 的 join 方法)。具有不同的handle_calls(例如get_hand、draw_card)和handle_casts(例如play_card、deliver_msg和send_msg)
一个gen_server来表示主进程。导出(join_game/1 调用player:start_link/1,send_msg/2 调用玩家的send_msg,play_card/3 调用玩家的play_card)。
你觉得这个架构怎么样?
提前致谢
I would like to discuss with you a possible implementation for a card game in erlang.
The only full example I found online is OpenPoker.
I would like to create one myself, so here is the implementation I have in mind:
A gen_server to represent a deck: when started creates a deck of cards (shuffled). And stores it in its state. provides an handle_call (draw_card)
A gen_server to represent the chat room. Stores in its state the registered name of a player process (e.g. player1, player2, luke etc etc). Exports handle_cast to join the chat (executed by default when somebody joins successfully the game) and one to broadcast a chat message to all users by calling an handle_cast on the gen_server representing a player.
a gen_fsm to represent a game instance. Has two states (wait_join, and turn). Exports join/1 to join the game, play_card/2 and send_msg/2. One parameter is the pid of the player process.
a gen_server to represent the player. Exports only start_link/1 where the parameter is the name to use to register the process (inside the init I call join method of gen_fsm). Has different handle_calls (e.g. get_hand, draw_card) and handle_casts (e.g. play_card, deliver_msg, and send_msg)
A gen_server to represent the main process. Exports (join_game/1 which calls player:start_link/1, send_msg/2 to call player's send_msg, play_card/3 to call player's play_card).
What do you think of this architecture?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论