设为首页 加入收藏

TOP

简单Elixir游戏服设计-桌子和座位(一)
2017-10-16 18:18:42 】 浏览:4953
Tags:简单 Elixir 游戏 设计 桌子 座位

建模桌子和座位

代码比较短小,直接贴吧。

model 里新建simple_table.ex 和 seat.ex, 项目里新增对应的test.

座位部分

defmodule Seat do
    def init(player) do
        %{
            id: player |> Player.get_id,
            player: player,
            score: 0,
        }
    end

    def get_id(seat), do: seat.id
    
    def update_player(seat, player), do: put_in(seat.player, player)
    def get_player(seat), do: seat.player
    def get_player_id(seat), do: seat.player |> Player.get_id

    def add_score(seat, num) when num >=0 , do: update_in(seat.score, &(&1 + num))
    def sub_score(seat, num) when num >= 0 , do: update_in(seat.score, &(&1 - num))
    def get_score(seat), do: seat.score

end
seat.ex
defmodule SeatTest do
  use ExUnit.Case
  doctest Seat
  
  setup do

    %{seat: Seat.init(Player.init)}
  end

  test "init", %{seat: seat} do
    assert 0 == seat |> Seat.get_score
  end

  test "add_and_remove_score", %{seat: seat} do
    num = 10
    old_score = seat |> Seat.get_score
    add_score_seat = seat |> Seat.add_score(num) 
    assert old_score + num == add_score_seat |> Seat.get_score
    sub_score_seat = seat |> Seat.sub_score(num)
    assert old_score - num == sub_score_seat |> Seat.get_score
  end
 
end
seat_test.exs

桌子部分

defmodule SimpleTable do
    
    def init() do
        %{
            cards: SimplePoker.init_cards,
            creator: nil,
            seat_map: %{},
            seat_order: []
        }
    end

    def seat_count(table), do: table.seat_order |> Enum.count
    def seat_order(table), do: table.seat_order

    def find_seat(table, %{} = player), do: find_seat(table, player |> Player.get_id)
    def find_seat(table, player_id), do: table.seat_map[player_id]

    def add_seat(table, player) do
        seat = Seat.init(player)
        seat_id = seat |> Seat.get_id
        table = put_in(table.seat_map[seat_id], seat)
        add_to_order(table, seat_id)
    end

    def add_to_order(table, seat_id), do: update_in(table.seat_order, &(&1 ++ [seat_id]))

    def remove_seat(table, %{} = player), do: remove_seat(table, player |> Player.get_id)
    def remove_seat(table, player_id) do
        table = update_in(table.seat_map, fn m -> Map.delete(m, player_id) end)
        update_in(table.seat_order, fn o -> List.delete(o, player_id) end)
    end

    
end
simple_table.ex
defmodule SimpleTableTest do
  use ExUnit.Case
  doctest SimpleTable
  
  def create_player(id), do: Player.init |> Player.set_id(id)

  setup do
    %{
        table: SimpleTable.init,
          player1: create_player(1),
          player2: create_player(2),
          player3: create_player(3),
          player4: create_player(4)
    }
  end

  test "init", %{table: table} do
      assert 0 = table |> SimpleTable.seat_count
  end

  test "add_and_remove_seat", %{table: table, player1: player1, player2: player2, player3: player3, player4: player4} do
        table = table |> SimpleTable.add_seat(player1) 
                        |> SimpleTable.add_seat(player2)
                        |> SimpleTable.add_seat(player3)
                        |> SimpleTable.add_seat(player4)

        assert 4 == SimpleTable.seat_count(table)
        expect_seat_order = [player1 |> Player.get_id,
                player2 |> Player.get_id,
                  player3 |> Player.get_id,
                  player4 |> Player.get_id]
        assert ^expect_seat_order = SimpleTable.seat_order(table)

        new_expect_seat_order = [
                player2 |> Player.get_id,
                  player3 |> Player.get_id,
                  player4 |> Player.
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇简单Elixir游戏服设计-测试驱动? 下一篇简单Elixir游戏服设计-桌子进程跑..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目