diff --git a/elm.json b/elm.json index 215b11e..ab2a225 100644 --- a/elm.json +++ b/elm.json @@ -11,6 +11,7 @@ "elm/html": "1.0.0", "elm/random": "1.0.0", "elm/svg": "1.0.1", + "elm/time": "1.0.0", "elm/url": "1.0.0", "elm-community/list-extra": "8.2.4", "mdgriffith/elm-ui": "1.1.7", @@ -18,7 +19,6 @@ }, "indirect": { "elm/json": "1.1.3", - "elm/time": "1.0.0", "elm/virtual-dom": "1.0.2" } }, diff --git a/src/Game.elm b/src/Game.elm index 51fc281..7754444 100644 --- a/src/Game.elm +++ b/src/Game.elm @@ -22,12 +22,13 @@ import Game.Board as Board , BoardStatus(..) ) import Game.Core exposing (Cellname(..), Gamepiece) -import Helpers exposing (andThen, map, noCmds, withCmd) +import Helpers exposing (andThen, map, noCmds) import List.Nonempty as Listn import Process -import Random +import Random exposing (Generator) import Shared exposing (Model) import Task +import Time @@ -57,11 +58,6 @@ type alias ChosenPiece = Gamepiece -type GeneratorOptions - = GetGamepiece - | GetCell - - type Turn = ChoosingPiece | ChoosingCellToPlay ChosenPiece @@ -115,8 +111,7 @@ update msg (Model model) = Model model |> noCmds |> map (nextPlayerStartsPlaying Human piece) - |> withCmd (wait 2) - |> andThen (computerChooses GetCell) + |> andThen (computerChooses ComputerSelectedCell Board.openCells) ( ComputerSelectedCell name, InPlay Computer (ChoosingCellToPlay piece) ) -> Model model @@ -150,27 +145,29 @@ nextPlayerStartsPlaying player piece (Model model) = Model { model | status = InPlay (switch player) (ChoosingCellToPlay piece) } -computerChooses : GeneratorOptions -> Model -> ( Model, Cmd Msg ) -computerChooses opt (Model model) = +msgGenerator : (a -> Msg) -> Generator a -> (Int -> Msg) +msgGenerator msgConstructor generator = + \num -> + Random.initialSeed num + |> Random.step generator + |> (\( value, _ ) -> msgConstructor value) + + +computerChooses : (a -> Msg) -> (Board -> List a) -> Model -> ( Model, Cmd Msg ) +computerChooses msgConstructor boardfunc (Model model) = let - helper : (a -> Msg) -> List a -> ( Model, Cmd Msg ) - helper msg lst = - lst - |> Listn.fromList - |> Maybe.map - (\items -> - ( Model model, Random.generate msg (Listn.sample items) ) - ) - |> Maybe.withDefault (Model model |> noCmds) + generator : Listn.Nonempty a -> Cmd Msg + generator items = + items + |> Listn.sample + |> msgGenerator msgConstructor + |> delay 2 in - case opt of - GetCell -> - Board.openCells model.board - |> helper ComputerSelectedCell - - GetGamepiece -> - Board.unPlayedPieces model.board - |> helper ComputerSelectedPiece + boardfunc model.board + |> Listn.fromList + |> Maybe.map generator + |> Maybe.withDefault Cmd.none + |> (\cmds -> ( Model model, cmds )) playerMakesPlay : Cellname -> Gamepiece -> Model -> Model @@ -189,8 +186,7 @@ checkForWin player (Model ({ board, status } as model)) = Model model |> noCmds |> map (playerStartsChoosing Computer) - |> withCmd (wait 2) - |> andThen (computerChooses GetGamepiece) + |> andThen (computerChooses ComputerSelectedPiece Board.unPlayedPieces) ( Human, CanContinue ) -> Model model @@ -214,20 +210,15 @@ playerStartsChoosing player (Model model) = -- Cmd Msg -type Seconds - = Seconds Int - - -wait : Int -> Cmd Msg -wait i = - delay (Seconds i) NoOp +type alias Seconds = + Int -delay : Seconds -> Msg -> Cmd Msg -delay (Seconds time) msg = +delay : Seconds -> (Int -> Msg) -> Cmd Msg +delay time generator = Process.sleep (toFloat <| time * 1000) - |> Task.andThen (always <| Task.succeed msg) - |> Task.perform identity + |> Task.andThen (\_ -> Time.now) + |> Task.perform (Time.posixToMillis >> generator)