It's a common misunderstanding that every discrete change to a model must 
originate from a different message. In general messages are for expressing 
what is allowed to happen in your application as a result of outside 
behavior like user input and server responses, whereas you want to run your 
AI as a direct, immediate result of your user making a move. What you 
should do instead is write two functions, toggleAtIndex : Int -> Model -> 
Model which handles the model changes under Toggle indexToToggle, and runAi 
: Model -> Model, which handles the call to miniMax. Then in your single 
Toggle message branch, call them both in the right order:


toggleAtIndex indexToToggle model =
  { model | board = move indexToToggle model.turn model.board }

runAi model =
  fst (miniMax model 0)

update msg model =
  case msg of
    Toggle indexToToggle ->
      ( model
          |> toggleAtIndex indexToToggle
          |> runAi
      , Cmd.none
      )


On Monday, October 10, 2016 at 2:23:51 AM UTC-5, [email protected] wrote:
>
> I wrote an TicTacToe AI game. I published it here. 
> https://tronyeu0802.github.io/TicTacToe-elm/
>
> Currently, when you make a move, the game will calculate an AI move then 
> the browser will display both your move and the AI move at the same time. I 
> did this by create and perform a task that always succeeds with the AIMove 
>   message. 
>
> update msg model =
>
>     case msg of
>
>         Toggle indexToToggle ->
>
>                 ( { model
>
>                     | board = move indexToToggle model.turn model.board
>
>                   }
>
>                 , Task.perform (\_ -> Debug.crash "This failure cannot 
> happen.") identity (Task.succeed AIMove)
>
>                   --Cmd.Extra.message AIMove
>
>                 )
>
>
>         AIMove ->
>
>             ( fst (miniMax model 0)
>
>             , Cmd.none
>
>             )
>
>
>  How can I display the player's move, then the AI's move after that?
>
> I'm thinking that this could be done by subscribing to the number of 
> available moves left. If this is even then do an AI move else do nothing.
>
> I'm very new to ELM , I hope you teach better ways to achieve this. :D 
> Thank you
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to