Skip to content

Commit

Permalink
fixed (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkshill authored Nov 4, 2020
1 parent 78e684a commit b6bf98a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"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",
"mgold/elm-nonempty-list": "4.1.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
Expand Down
73 changes: 32 additions & 41 deletions src/Game.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -57,11 +58,6 @@ type alias ChosenPiece =
Gamepiece


type GeneratorOptions
= GetGamepiece
| GetCell


type Turn
= ChoosingPiece
| ChoosingCellToPlay ChosenPiece
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)



Expand Down

0 comments on commit b6bf98a

Please sign in to comment.