LoginSignup
5
5

More than 5 years have passed since last update.

ズンドコキヨシ with Elm

Last updated at Posted at 2016-03-17

Elm

4

Debug.log


使
Debug.log
module Main where

import Random exposing (bool,generate,initialSeed,Seed)
import Debug exposing (log)
import List exposing (drop)

import Graphics.Element exposing (show)

--Seedを入れるとズンかドコを返す
zunDoko : Seed -> (String,Seed)
zunDoko seed =
  let generator = Random.map (\x -> if x then "ドコ" else "ズン") bool
  in generate generator seed

--判定
decision : List String -> Bool
decision list = ["ズン","ズン","ズン","ズン","ドコ"] == (drop ((List.length list)-5) list)


zundokoList (seed,list) =
  if decision list then
      (seed , list ++ [log "" "キ!ヨ!シ!"])
  else
      let
          (zd , seed') = zunDoko seed
      in
          zundokoList (seed', list ++ [log "" zd])


main = show <|  zundokoList (initialSeed 0 , [])

StartApp(Elmアーキテクチャ)版


startapp使html
Effect使
SignalEffect使

debug.logSeedTask


module Main where

import Random exposing (bool,generate,initialSeed,Seed)
import TaskTutorial exposing(getCurrentTime)
import Signal exposing(..)
import Task exposing (Task)
import Debug exposing (log)

import Effects exposing (..)
import StartApp exposing (start)
import Html exposing (text,div,Html)
import List exposing (drop,take,append)

--App内で使うデータの定義。ランダムに使うSeed値と、画面に表示するズンドコの文字列、がある。
type alias Model =
            (Seed,List String)

--App内で起こるアクションの定義。今回のズンドコAppの場合は、Seed値を現在時間から取ってくる(きた)Settingアクション、ループのステップのアクション、すべてが終わって終了Endのアクション、を定義した。
type Action = Setting Int
            | Step
            | End

init : (Model, Effects Action)
init =
  ((initialSeed 0,[]),Effects.task initTask)

--Seed値に現在時間を使うために取ってくる。
initTask : Task x Action
initTask =
  getCurrentTime
   |> Task.map (\x -> Setting (round x))

--Seedを入れるとランダムにズンかドコを返す
zunDoko : Seed -> (String,Seed)
zunDoko seed =
             let generator = Random.map (\x -> if x then "ドコ" else "ズン") bool
              in generate generator seed

--リストの後ろ5つの文字列を見て判定する
decision : List String -> Bool
decision list = ["ズン","ズン","ズン","ズン","ドコ"] == (drop ((List.length list)-5) list)


toEffect action = action |> Task.succeed |> Effects.task


--返すEffectsでループさせているのがわかるだろうか。
update : Action -> Model -> (Model , Effects Action)
update action (seed,list) =
  case action of
    --Seed設定action
    Setting num ->((initialSeed num, list), toEffect Step )

  --ループ時に実行される
    Step -> let
                    (zd , seed') = zunDoko seed
                in
                   --判定
                   if decision list then
                      (( seed' , list ++ ["キ!ヨ!シ!"] ), toEffect End )
                   else
                      (( seed' , list ++ [zd] ), toEffect Step )

   --終了
    End -> ((seed,list),Effects.none)


view : Address Action -> Model -> Html
view address (seed,list) =
    div [] [text <| toString list]

app =
  start
    { init = init
    , update = update
    , view = view
    , inputs = []
    }


main =
  app.html


port tasks : Signal (Task.Task Never ())
port tasks =
  app.tasks

5
5
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

5
5