本来是要弄回合时间限制
但发现怎样算开始呢?
集满两胜好像也还没有说谁获胜了。
建立游戏之後,正常的进行游戏的话,依照顺序会有
等等 等待guest 应该不是核心游戏这层的,在这边应该只会有两个
status 可能会有 :start, :host_win, :guest_win
来把它加进初始 Game struct
defstruct host: %{desk: [], hand: initial_hand, wins: 0},
guest: %{desk: [], hand: initial_hand, wins: 0},
turn: 1,
round: 1,
status: :start
检查胜利的时间点,好像又只能放在出牌後,感觉太多东西在那边了,
没关系,先放,有想到更好的再改
应该是接在 |> end_round
後面,来做一个方法检查有没有人 wins: 2
def handle_cast({:play_card, player, card}, game) do
game =
game
|> play_card_for(player, card)
|> end_turn()
|> add_wins()
|> end_round()
|> end_game()
{:noreply, game}
end
end_game 方法跟前几个比单纯很多
defp end_game(%{guest: %{wins: 2}} = game), do: Map.replace game, :status, :guest_win
defp end_game(%{host: %{wins: 2}} = game), do: Map.replace game, :status, :host_win
defp end_game(game), do: game
来做一个假的後期游戏来测
def start_late_game do
late_game = %Game{
guest: %{wins: 1, desk: [3, 2, 1, 3, 2], hand: [1]},
host: %{wins: 1, desk: [6, 3, 3, 5, 2], hand: [2]},
turn: 3,
round: 2
}
GenServer.start_link(__MODULE__, late_game)
end
iex(1)> import Game
Game
iex(2)> {:ok, pid} = start_late_game
{:ok, #PID<0.113.0>}
iex(3)> status pid
%Game{
guest: %{desk: [3, 2, 1, 3, 2], hand: [1], wins: 1},
host: %{desk: [6, 3, 3, 5, 2], hand: [2], wins: 1},
round: 2,
status: :start,
turn: 3
}
iex(4)> play_card pid, :guest, 1
:ok
iex(5)> play_card pid, :host, 2
:ok
iex(6)> status pid
%Game{
guest: %{desk: [3, 2, 1, 3, 2, 1], hand: [], wins: 1},
host: %{desk: [6, 3, 3, 5, 2, 2], hand: [], wins: 2},
round: 3,
status: :host_win,
turn: 1
}
状态变成获胜了
明天看要来弄每回合出牌限时
还是修一下就算手上没有要出的牌,也可以出的情况
>>: Day 12 - [爬虫] 02-储存爬虫资料-CSV格式
针对我们所谓的Mobile Security(移动装置安全、行动装置安全)。 经常会联想到智慧型手机...
前一天,我们使用了simple_form_for提到了新增表单写法,而今天要讲一个上传情境。这个上传...
大家好,我是毛毛。ヾ(´∀ ˋ)ノ 来到30天的最後一天解题Day啦~ 1078. Occurren...
前言 讲完 session 之後,接下来这几天要来讲讲跟密码有关的安全性议题,毕竟如果使用者的密码不...
前言 昨天知道一些背景的属性後,是不是觉得有点单调呀,所以我们今天就来谈谈渐层 基本语法 backg...