简单Elixir游戏服设计-桌子和座位

来源:互联网 时间:2017-10-11

建模桌子和座位

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

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.get_id

]

table = table |> SimpleTable.remove_seat(player1)

assert 3 == SimpleTable.seat_count(table)

assert ^new_expect_seat_order = SimpleTable.seat_order(table)

end

end

simple_table_test.exs

暂时是随便编写了,后续需要再调整。

下回该起游戏进程了。

相关阅读:
Top