{"rsdb":{"rid":"239127","subhead":"","postdate":"0","aid":"164831","fid":"88","uid":"1","topic":"1","content":"
\n

\u5efa\u6a21\u684c\u5b50\u548c\u5ea7\u4f4d<\/p> \n

\u4ee3\u7801\u6bd4\u8f83\u77ed\u5c0f\uff0c\u76f4\u63a5\u8d34\u5427\u3002<\/p> \n

model \u91cc\u65b0\u5efasimple_table.ex \u548c seat.ex\uff0c \u9879\u76ee\u91cc\u65b0\u589e\u5bf9\u5e94\u7684test.<\/p> \n

\u5ea7\u4f4d\u90e8\u5206<\/p> \n

\n \"\"\n \"\" \n
\n
defmodule Seat do\r\n    <\/span>def<\/span> init(player) do\r\n        <\/span>%{\r\n            id: player <\/span>|> Player.get_id,\r\n            player: player,\r\n            score: 0,\r\n        }\r\n    end\r\n\r\n    <\/span>def<\/span> get_id(seat), do: seat.id\r\n    \r\n    <\/span>def<\/span> update_player(seat, player), do: put_in(seat.player, player)\r\n    <\/span>def<\/span> get_player(seat), do: seat.player\r\n    <\/span>def<\/span> get_player_id(seat), do: seat.player |> Player.get_id\r\n\r\n    <\/span>def<\/span> add_score(seat, num) when num >=0 , do: update_in(seat.score, &(&1 + num))\r\n    <\/span>def<\/span> sub_score(seat, num) when num >= 0 , do: update_in(seat.score, &(&1 - num))\r\n    <\/span>def<\/span> get_score(seat), do: seat.score\r\n\r\nend<\/span><\/pre> \n  <\/div> \n  seat.ex<\/span>\n <\/div> \n 
\n \"\"\n \"\" \n
\n
defmodule SeatTest do\r\n  use ExUnit.Case\r\n  doctest Seat\r\n  \r\n  setup do\r\n\r\n    <\/span>%{seat: Seat.init(Player.init)}\r\n  end\r\n\r\n  test <\/span>"<\/span>init<\/span>"<\/span>, %{seat: seat} do\r\n    <\/span>assert<\/span> 0 == seat |> Seat.get_score\r\n  end\r\n\r\n  test <\/span>"<\/span>add_and_remove_score<\/span>"<\/span>, %{seat: seat} do\r\n    num <\/span>= 10\r\n    old_score <\/span>= seat |> Seat.get_score\r\n    add_score_seat <\/span>= seat |> Seat.add_score(num) \r\n    <\/span>assert<\/span> old_score + num == add_score_seat |> Seat.get_score\r\n    sub_score_seat <\/span>= seat |> Seat.sub_score(num)\r\n    <\/span>assert<\/span> old_score - num == sub_score_seat |> Seat.get_score\r\n  end\r\n \r\nend<\/span><\/pre> \n  <\/div> \n  seat_test.exs<\/span>\n <\/div> \n 

\u684c\u5b50\u90e8\u5206<\/p> \n

\n \"\"\n \"\" \n
\n
defmodule SimpleTable do\r\n    \r\n    <\/span>def<\/span> init() do\r\n        <\/span>%{\r\n            cards: SimplePoker.init_cards,\r\n            creator: nil,\r\n            seat_map: <\/span>%{},\r\n            seat_order: []\r\n        }\r\n    end\r\n\r\n    <\/span>def<\/span> seat_count(table), do: table.seat_order |> Enum.count\r\n    <\/span>def<\/span> seat_order(table), do: table.seat_order\r\n\r\n    <\/span>def<\/span> find_seat(table, %{} = player), do: find_seat(table, player |> Player.get_id)\r\n    <\/span>def<\/span> find_seat(table, player_id), do: table.seat_map[player_id]\r\n\r\n    <\/span>def<\/span> add_seat(table, player) do\r\n        seat <\/span>= Seat.init(player)\r\n        seat_id <\/span>= seat |> Seat.get_id\r\n        table <\/span>= put_in(table.seat_map[seat_id], seat)\r\n        add_to_order(table, seat_id)\r\n    end\r\n\r\n    <\/span>def<\/span> add_to_order(table, seat_id), do: update_in(table.seat_order, &(&1 ++ [seat_id]))\r\n\r\n    <\/span>def<\/span> remove_seat(table, %{} = player), do: remove_seat(table, player |> Player.get_id)\r\n    <\/span>def<\/span> remove_seat(table, player_id) do\r\n        table <\/span>= update_in(table.seat_map, fn m -> Map.delete(m, player_id) end)\r\n        update_in(table.seat_order, fn o <\/span>-> List.delete(o, player_id) end)\r\n    end\r\n\r\n    \r\nend<\/span><\/pre> \n  <\/div> \n  simple_table.ex<\/span>\n <\/div> \n 
\n \"\"\n \"\" \n
\n
defmodule SimpleTableTest do\r\n  use ExUnit.Case\r\n  doctest SimpleTable\r\n  \r\n  <\/span>def<\/span> create_player(id), do: Player.init |> Player.set_id(id)\r\n\r\n  setup do\r\n    <\/span>%{\r\n        table: SimpleTable.init,\r\n          player1: create_player(<\/span>1),\r\n          player2: create_player(<\/span>2),\r\n          player3: create_player(<\/span>3),\r\n          player4: create_player(<\/span>4)\r\n    }\r\n  end\r\n\r\n  test <\/span>"<\/span>init<\/span>"<\/span>, %{table: table} do\r\n      <\/span>assert<\/span> 0 = table |> SimpleTable.seat_count\r\n  end\r\n\r\n  test <\/span>"<\/span>add_and_remove_seat<\/span>"<\/span>, %{table: table, player1: player1, player2: player2, player3: player3, player4: player4} do\r\n        table <\/span>= table |> SimpleTable.add_seat(player1) \r\n                        <\/span>|> SimpleTable.add_seat(player2)\r\n                        <\/span>|> SimpleTable.add_seat(player3)\r\n                        <\/span>|> SimpleTable.add_seat(player4)\r\n\r\n        <\/span>assert<\/span> 4 == SimpleTable.seat_count(table)\r\n        expect_seat_order <\/span>= [player1 |> Player.get_id,\r\n                player2 <\/span>|> Player.get_id,\r\n                  player3 <\/span>|> Player.get_id,\r\n                  player4 <\/span>|> Player.get_id]\r\n        <\/span>assert<\/span> ^expect_seat_order = SimpleTable.seat_order(table)\r\n\r\n        new_expect_seat_order <\/span>= [\r\n                player2 <\/span>|> Player.get_id,\r\n                  player3 <\/span>|> Player.get_id,\r\n                  player4 <\/span>|> Player.","orderid":"0","title":"\u7b80\u5355Elixir\u6e38\u620f\u670d\u8bbe\u8ba1-\u684c\u5b50\u548c\u5ea7\u4f4d(\u4e00)","smalltitle":"","mid":"0","fname":"Erlang","special_id":"0","bak_id":"0","info":"0","hits":"6457","pages":"2","comments":"0","posttime":"2017-10-16 18:18:42","list":"1508149122","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/images.cnblogs.com\/OutliningIndicators\/ContractedBlock.gif","ispic":"1","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u7b80\u5355<\/A> Elixir<\/A> \u6e38\u620f<\/A> \u8bbe\u8ba1<\/A> \u684c\u5b50<\/A> \u5ea7\u4f4d<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u7b80\u5355Elixir\u6e38\u620f\u670d\u8bbe\u8ba1-\u684c\u5b50\u548c\u5ea7\u4f4d","lastview":"1713291321","digg_num":"6383","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}