# 简单Elixir游戏服设计-玩法simple_poker

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

`defmodule SimplePoker do@cards for i <- 1..4, j<- 1..13, do: {i, j}@ten 10@ace 1@tian_gong [8,9]@ignore 0def init_cards, do: @cards# 洗牌def shuffle(cards), do: cards |> Enum.shuffle# 初始发牌def init_deal(cards, seat_num) do{cs, left} = cards |> Enum.split(seat_num * 2){:ok, Enum.chunk_every(cs, 2), left}end# 补单张def deal([card| left]), do: {:ok, card, left}def single_point({_, p}) when p < @ten, do: pdef single_point(_), do: @tendef normal_power(cards) dosum = cards |> Enum.map( &single_point(&1) ) |> Enum.sumrem(sum, @ten)end# 牌力计算, 需参考是否开牌def power([_a, _b] = cards, is_open) dop = normal_power(cards)cond dop in @tian_gong and is_open -> {:tian_gong, p}true ->{:normal, p}endenddef power(cards, false) docond dois_flush_straight?(cards) -> {:flush_straight, @ignore}is_three?(cards) -> {:three, @ignore}is_flush?(cards) -> {:flush, @ignore}is_straight?(cards) -> {:straight, @ignore}true -> {:normal, normal_power(cards)}endend# a 是否赢 b# 都是天公，比点数def win?({:tian_gong, p1}, {:tian_gong, p2}), do: p1 > p2# 天公比其他都大def win?({:tian_gong, _}, _), do: truedef win?(_, {:tian_gong, _}), do: false# 非普通牌，通牌型一样大def win?({same, _}, {same, _}) when same != :normal, do: false# 同花顺比余下都大, 以下类推def win?({:flush_straight, _}, _), do: truedef win?(_, {:flush_straight, _}), do: falsedef win?({:three, _}, _), do: truedef win?(_, {:three, _}), do: falsedef win?({:flush, _}, _), do: truedef win?(_, {:flush, _}), do: falsedef win?({:straight, _}, _), do: truedef win?(_, {:straight, _}), do: false# 普通牌需要比较点数def win?({:normal, p1}, {:normal, p2}), do: p1 > p2# 赢多少倍def multiply({:tian_gong, _}), do: 1def multiply({:flush_straight, _}), do: 16def multiply({:three, _}), do: 8def multiply({:flush, _}), do: 4def multiply({:straight, _}), do: 2def multiply({:normal, _}), do: 1def is_flush?([{s, _}, {s, _}, {s, _}]), do: truedef is_flush?(_), do: falsedef is_straight?([{_, p1}, {_, p2}, {_, p3}]) do[n1, n2, n3] = [p1, p2, p3] |> Enum.sortcond don1 + 1 == n2 and n2 + 1 == n3 -> truen1 == @ace and n2 + 1 == n3 -> truen1 == @ace and n2 + 2 == n3 -> truetrue -> falseendenddef is_three?([{_, p}, {_, p}, {_, p}]), do: truedef is_three?([{_, p1}, {_, p2}, {_, p3}]) docase [p1, p2, p3] |> Enum.sort do[@ace, @ace, _] -> true[@ace, n, n] -> true_other -> falseendenddef is_flush_straight?(cards), do: is_flush?(cards) and is_straight?(cards)end# SimplePoker.init_cards |> SimplePoker.shuffle |> IO.inspect# SimplePoker.init_cards |> SimplePoker.init_deal(2) |> IO.inspect`

simple_poker.ex

`defmodule SimplePokerTest douse ExUnit.Casedoctest SimplePokersetup do%{s_ace: {1,1}, # 黑桃Ah_ace: {2, 1}, # 红桃A,c_ace: {3, 1}, # 梅花As_two: {1, 2}, # 黑桃2h_two: {2, 2}, # 红桃2c_two: {3, 2}, # 梅花2s_three: {1, 3}, # 黑桃3h_three: {2, 3}, # 红桃3s_four: {1, 4}, # 黑桃4h_four: {2, 4}, # 红桃4s_five: {1, 5}, # 黑桃5s_eight: {1, 8}, # 黑桃8s_nine: {1, 9}, # 黑桃9s_ten: {1, 10}, # 黑桃10s_jack: {1, 11}}endtest "同花: 黑桃A，黑桃2, 黑桃3 ", cards doflush_cards = [cards.s_ace, cards.s_two, cards.s_three]assert SimplePoker.is_flush?(flush_cards)endtest "非同花: 黑桃A, 红桃A, 黑桃2 ", cards donot_flush_cards = [cards.s_ace, cards.h_ace, cards.s_two]refute SimplePoker.is_flush?(not_flush_cards)endtest "三条: 普通", cards donormal_three = [cards.s_two, cards.h_two, cards.c_two]assert SimplePoker.is_three?(normal_three)endtest "三条：1张A + 1对 ", cards doone_ace_and_one_pair = [cards.s_two, cards.h_two, cards.s_ace]assert SimplePoker.is_three?(one_ace_and_one_pair)endtest "三条: 2张A + 1张2 ", cards dotwo_ace_and_one = [cards.s_ace, cards.h_ace, cards.s_two]assert SimplePoker.is_three?(two_ace_and_one)endtest "非三条: A, 2, 3", cards donot_three = [cards.s_ace, cards.s_two, cards.s_three]refute SimplePoker.is_three?(not_three)endtest "顺子： 普通 黑桃2， 黑桃3， 红桃4", cards donormal_straight = [cards.s_two, cards.s_three, cards.h_four]assert SimplePoker.is_straight?(normal_straight)endtest "顺子： 普通 黑桃A, 黑桃2， 红桃3", cards doone_ace_normal_straight = [cards.s_ace, cards.s_two, cards.h_three]assert SimplePoker.is_straight?(one_ace_normal_straight)endtest "顺子: 普通 黑桃A, 黑桃2， 红桃4", cards doone_ace_normal_straight = [cards.s_ace, cards.s_two, cards.h_four]assert SimplePoker.is_straight?(one_ace_normal_straight)endtest "非顺子: 黑桃A， 黑桃2, 红桃2", cards donot_straight = [cards.s_ace, cards.s_two, cards.h_two]refute SimplePoker.is_straight?(not_straight)endtest "同花顺: 普通", cards donormal_flush_straight = [cards.s_two, cards.s_three, cards.s_four]assert SimplePoker.is_flush_straight?(normal_flush_straight)endtest "普通三张", cards donormal = [cards.s_two, cards.s_two, cards.h_three]assert {:normal, _} = SimplePoker.power(normal, false)endtest "天公9点", cards doassert {:tian_gong, 9} = [cards.s_ace, cards.s_eight] |> SimplePoker.power(true)assert {:tian_gong, 9} = [cards.s_four, cards.s_five] |> SimplePoker.power(true)endtest "普通9点", cards doassert {:normal, 9} = [cards.s_ace, cards.s_eight] |> SimplePoker.power(false)endtest "single_point", cards doassert 1 == cards.s_ace |> SimplePoker.single_pointassert 10 == cards.s_ten |> SimplePoker.single_pointassert 10 == cards.s_jack |> SimplePoker.single_pointendtest "win?" dotian_gong_9 = {:tian_gong, 9}tian_gong_8 = {:tian_gong, 8}flush_straight = {:flush_straight, 0}three = {:three, 0}flush = {:flush, 0}straight = {:straight, 0}normal_9 = {:normal, 9}normal_8 = {:normal, 8}assert SimplePoker.win?(tian_gong_9, tian_gong_8)refute SimplePoker.win?(tian_gong_9, tian_gong_9)refute SimplePoker.win?(tian_gong_8, tian_gong_9)assert SimplePoker.win?(tian_gong_9, flush_straight)refute SimplePoker.win?(flush_straight, tian_gong_9)refute SimplePoker.win?(flush_straight, flush_straight)assert SimplePoker.win?(flush_straight, three)refute SimplePoker.win?(three, flush_straight)assert SimplePoker.win?(three, flush)refute SimplePoker.win?(flush, three)assert SimplePoker.win?(flush, straight)refute SimplePoker.win?(straight, flush)assert SimplePoker.win?(straight, normal_9)refute SimplePoker.win?(normal_9, straight)assert SimplePoker.win?(normal_9, normal_8)refute SimplePoker.win?(normal_9, normal_9)refute SimplePoker.win?(normal_8, normal_9)endend`

simple_poker_test.exs