LoginSignup
2
0

More than 5 years have passed since last update.

ズンドコキヨシ with Elixir

Last updated at Posted at 2017-11-27

Elixirに真面目に取り組む一環として、練習がてらにズンドコキヨシをElixirで解いてみる。

Rubyで前に解いたのはこっち https://qiita.com/msky026/items/8a520dd8be324389bf6e

defmodule Zundoko do
  def run(list \\ []) do
    zundoko = Enum.random(["ズン", "ドコ"])
    IO.puts zundoko
    case complete?(last(list, 5)) do
      true -> show_zundoko(last(list, 5))
      _ -> run(trim(list) ++ [zundoko])
    end
  end

  def complete?(list) do
    list == ["ズン", "ズン","ズン", "ズン", "ドコ"]
  end

  def trim(list) do
    trimed_list =
      case Enum.count(list) do
        size when size >= 5 -> tl list
        _ -> list
      end
    trimed_list
  end

  def last(list, n) do
    list |> Enum.reverse |> Enum.take(n)
  end

  def show_zundoko(list) do
    list |> Enum.each(fn a -> IO.puts a end)
    IO.puts "キ・ヨ・シ!"
  end
end

Zundoko.run()

やってみると想像以上に手間取った。
追記:ifとかElixirっぽくないのと後少し手直しした。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0