经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Erlang » 查看文章
简单Elixir游戏服设计-玩法simple_poker
来源:cnblogs  作者:damnit  时间:2018/9/25 20:32:22  对本文有异议

上回介绍了玩法,现在编写了玩法的简单建模。

做到现在感觉目前还没有使用umbrella的必要(也许以后会发现必要吧),model 应用完全可以合并到game_server。

代码还在https://github.com/rubyist1982/simple 上。

model 应用新增 simple_poker.ex , 代码不多,做了点简单注释,可以贴下

  1. defmodule SimplePoker do
  2. @cards for i <- 1..4, j<- 1..13, do: {i, j}
  3. @ten 10
  4. @ace 1
  5. @tian_gong [8,9]
  6. @ignore 0
  7. def init_cards, do: @cards
  8. # 洗牌
  9. def shuffle(cards), do: cards |> Enum.shuffle
  10. # 初始发牌
  11. def init_deal(cards, seat_num) do
  12. {cs, left} = cards |> Enum.split(seat_num * 2)
  13. {:ok, Enum.chunk_every(cs, 2), left}
  14. end
  15. # 补单张
  16. def deal([card| left]), do: {:ok, card, left}
  17. def single_point({_, p}) when p < @ten, do: p
  18. def single_point(_), do: @ten
  19. def normal_power(cards) do
  20. sum = cards |> Enum.map( &single_point(&1) ) |> Enum.sum
  21. rem(sum, @ten)
  22. end
  23. # 牌力计算, 需参考是否开牌
  24. def power([_a, _b] = cards, is_open) do
  25. p = normal_power(cards)
  26. cond do
  27. p in @tian_gong and is_open -> {:tian_gong, p}
  28. true ->{:normal, p}
  29. end
  30. end
  31. def power(cards, false) do
  32. cond do
  33. is_flush_straight?(cards) -> {:flush_straight, @ignore}
  34. is_three?(cards) -> {:three, @ignore}
  35. is_flush?(cards) -> {:flush, @ignore}
  36. is_straight?(cards) -> {:straight, @ignore}
  37. true -> {:normal, normal_power(cards)}
  38. end
  39. end
  40. # a 是否赢 b
  41. # 都是天公,比点数
  42. def win?({:tian_gong, p1}, {:tian_gong, p2}), do: p1 > p2
  43. # 天公比其他都大
  44. def win?({:tian_gong, _}, _), do: true
  45. def win?(_, {:tian_gong, _}), do: false
  46. # 非普通牌,通牌型一样大
  47. def win?({same, _}, {same, _}) when same != :normal, do: false
  48. # 同花顺比余下都大, 以下类推
  49. def win?({:flush_straight, _}, _), do: true
  50. def win?(_, {:flush_straight, _}), do: false
  51. def win?({:three, _}, _), do: true
  52. def win?(_, {:three, _}), do: false
  53. def win?({:flush, _}, _), do: true
  54. def win?(_, {:flush, _}), do: false
  55. def win?({:straight, _}, _), do: true
  56. def win?(_, {:straight, _}), do: false
  57. # 普通牌需要比较点数
  58. def win?({:normal, p1}, {:normal, p2}), do: p1 > p2
  59. # 赢多少倍
  60. def multiply({:tian_gong, _}), do: 1
  61. def multiply({:flush_straight, _}), do: 16
  62. def multiply({:three, _}), do: 8
  63. def multiply({:flush, _}), do: 4
  64. def multiply({:straight, _}), do: 2
  65. def multiply({:normal, _}), do: 1
  66.  
  67.  
  68. def is_flush?([{s, _}, {s, _}, {s, _}]), do: true
  69. def is_flush?(_), do: false
  70. def is_straight?([{_, p1}, {_, p2}, {_, p3}]) do
  71. [n1, n2, n3] = [p1, p2, p3] |> Enum.sort
  72. cond do
  73. n1 + 1 == n2 and n2 + 1 == n3 -> true
  74. n1 == @ace and n2 + 1 == n3 -> true
  75. n1 == @ace and n2 + 2 == n3 -> true
  76. true -> false
  77. end
  78. end
  79. def is_three?([{_, p}, {_, p}, {_, p}]), do: true
  80. def is_three?([{_, p1}, {_, p2}, {_, p3}]) do
  81. case [p1, p2, p3] |> Enum.sort do
  82. [@ace, @ace, _] -> true
  83. [@ace, n, n] -> true
  84. _other -> false
  85. end
  86. end
  87. def is_flush_straight?(cards), do: is_flush?(cards) and is_straight?(cards)
  88. end
  89. # SimplePoker.init_cards |> SimplePoker.shuffle |> IO.inspect
  90. # SimplePoker.init_cards |> SimplePoker.init_deal(2) |> IO.inspect
simple_poker.ex

测试代码 simple_poker_test.exs

  1. defmodule SimplePokerTest do
  2. use ExUnit.Case
  3. doctest SimplePoker
  4. setup do
  5. %{
  6. s_ace: {1,1}, # 黑桃A
  7. h_ace: {2, 1}, # 红桃A,
  8. c_ace: {3, 1}, # 梅花A
  9. s_two: {1, 2}, # 黑桃2
  10. h_two: {2, 2}, # 红桃2
  11. c_two: {3, 2}, # 梅花2
  12. s_three: {1, 3}, # 黑桃3
  13. h_three: {2, 3}, # 红桃3
  14. s_four: {1, 4}, # 黑桃4
  15. h_four: {2, 4}, # 红桃4
  16. s_five: {1, 5}, # 黑桃5
  17. s_eight: {1, 8}, # 黑桃8
  18. s_nine: {1, 9}, # 黑桃9
  19. s_ten: {1, 10}, # 黑桃10
  20. s_jack: {1, 11}
  21. }
  22. end
  23. test "同花: 黑桃A,黑桃2, 黑桃3 ", cards do
  24. flush_cards = [cards.s_ace, cards.s_two, cards.s_three]
  25. assert SimplePoker.is_flush?(flush_cards)
  26. end
  27. test "非同花: 黑桃A, 红桃A, 黑桃2 ", cards do
  28. not_flush_cards = [cards.s_ace, cards.h_ace, cards.s_two]
  29. refute SimplePoker.is_flush?(not_flush_cards)
  30. end
  31. test "三条: 普通", cards do
  32. normal_three = [cards.s_two, cards.h_two, cards.c_two]
  33. assert SimplePoker.is_three?(normal_three)
  34. end
  35. test "三条:1张A + 1对 ", cards do
  36. one_ace_and_one_pair = [cards.s_two, cards.h_two, cards.s_ace]
  37. assert SimplePoker.is_three?(one_ace_and_one_pair)
  38. end
  39. test "三条: 2张A + 1张2 ", cards do
  40. two_ace_and_one = [cards.s_ace, cards.h_ace, cards.s_two]
  41. assert SimplePoker.is_three?(two_ace_and_one)
  42. end
  43. test "非三条: A, 2, 3", cards do
  44. not_three = [cards.s_ace, cards.s_two, cards.s_three]
  45. refute SimplePoker.is_three?(not_three)
  46. end
  47. test "顺子: 普通 黑桃2, 黑桃3, 红桃4", cards do
  48. normal_straight = [cards.s_two, cards.s_three, cards.h_four]
  49. assert SimplePoker.is_straight?(normal_straight)
  50. end
  51. test "顺子: 普通 黑桃A, 黑桃2, 红桃3", cards do
  52. one_ace_normal_straight = [cards.s_ace, cards.s_two, cards.h_three]
  53. assert SimplePoker.is_straight?(one_ace_normal_straight)
  54. end
  55. test "顺子: 普通 黑桃A, 黑桃2, 红桃4", cards do
  56. one_ace_normal_straight = [cards.s_ace, cards.s_two, cards.h_four]
  57. assert SimplePoker.is_straight?(one_ace_normal_straight)
  58. end
  59. test "非顺子: 黑桃A, 黑桃2, 红桃2", cards do
  60. not_straight = [cards.s_ace, cards.s_two, cards.h_two]
  61. refute SimplePoker.is_straight?(not_straight)
  62. end
  63. test "同花顺: 普通", cards do
  64. normal_flush_straight = [cards.s_two, cards.s_three, cards.s_four]
  65. assert SimplePoker.is_flush_straight?(normal_flush_straight)
  66. end
  67. test "普通三张", cards do
  68. normal = [cards.s_two, cards.s_two, cards.h_three]
  69. assert {:normal, _} = SimplePoker.power(normal, false)
  70. end
  71. test "天公9点", cards do
  72. assert {:tian_gong, 9} = [cards.s_ace, cards.s_eight] |> SimplePoker.power(true)
  73. assert {:tian_gong, 9} = [cards.s_four, cards.s_five] |> SimplePoker.power(true)
  74. end
  75. test "普通9点", cards do
  76. assert {:normal, 9} = [cards.s_ace, cards.s_eight] |> SimplePoker.power(false)
  77. end
  78. test "single_point", cards do
  79. assert 1 == cards.s_ace |> SimplePoker.single_point
  80. assert 10 == cards.s_ten |> SimplePoker.single_point
  81. assert 10 == cards.s_jack |> SimplePoker.single_point
  82. end
  83. test "win?" do
  84. tian_gong_9 = {:tian_gong, 9}
  85. tian_gong_8 = {:tian_gong, 8}
  86. flush_straight = {:flush_straight, 0}
  87. three = {:three, 0}
  88. flush = {:flush, 0}
  89. straight = {:straight, 0}
  90. normal_9 = {:normal, 9}
  91. normal_8 = {:normal, 8}
  92. assert SimplePoker.win?(tian_gong_9, tian_gong_8)
  93. refute SimplePoker.win?(tian_gong_9, tian_gong_9)
  94. refute SimplePoker.win?(tian_gong_8, tian_gong_9)
  95. assert SimplePoker.win?(tian_gong_9, flush_straight)
  96. refute SimplePoker.win?(flush_straight, tian_gong_9)
  97. refute SimplePoker.win?(flush_straight, flush_straight)
  98. assert SimplePoker.win?(flush_straight, three)
  99. refute SimplePoker.win?(three, flush_straight)
  100. assert SimplePoker.win?(three, flush)
  101. refute SimplePoker.win?(flush, three)
  102. assert SimplePoker.win?(flush, straight)
  103. refute SimplePoker.win?(straight, flush)
  104. assert SimplePoker.win?(straight, normal_9)
  105. refute SimplePoker.win?(normal_9, straight)
  106. assert SimplePoker.win?(normal_9, normal_8)
  107. refute SimplePoker.win?(normal_9, normal_9)
  108. refute SimplePoker.win?(normal_8, normal_9)
  109. end
  110. end
simple_poker_test.exs

 下回该建模游戏桌了

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号