Most callbacks in a browser, such as the timeout used for Time, is throttled to 4ms and behaves strangely below 16ms, if you want to execute as fast as possible then you should use an AnimationFrame subscription. Time subscriptions should only be used for 500ms or larger or so, any less and it starts to act odd.
You should probably still do a test to make sure it is the ai turn in your update for `AIMove` though, the browser can cache and send events 'later' than you expect as well. On Monday, October 10, 2016 at 11:06:07 AM UTC-6, [email protected] wrote: > > I just found a problem with using subscription though. I tried replace > second with millisecond, while the minimax was executing (about less than a > second) the program keep calling more AIMove. > > > subscriptions model = > if (gameIsNotFinished model && model.turn == AIToMove) then > Time.every millisecond AIMove > > > Vào 23:54:21 UTC+7 Thứ Hai, ngày 10 tháng 10 năm 2016, OvermindDL1 đã viết: >> >> The example he put above is fine: :) >> ``` >> subscriptions : Model -> Sub Msg >> subscriptions model = >> if (gameIsNotFinished model && model.turn == AIToMove) then >> Time.every second AIMove >> else >> Sub.none >> ``` >> Though if you want it in another function you can do that too (useful to >> batch functionality together): >> ``` >> subAITick : Model -> Sub Msg >> subAITick model = >> if (gameIsNotFinished model && model.turn == AIToMove) then >> Time.every second AIMove >> else >> Sub.none >> >> subscriptions : Model -> Sub Msg >> subscriptions model = >> subAITick >> ``` >> Or if you have other subscriptions too, batching them together: >> ``` >> >> subscriptions : Model -> Sub Msg >> subscriptions model = >> Sub.batch >> [ subAITick >> ; otherSubscriptionsHere >> ] >> ``` >> >> >> On Monday, October 10, 2016 at 10:48:34 AM UTC-6, [email protected] >> wrote: >>> >>> Can you show code example :D I'm quite new to Elm and javascript. >>> >>> Vào 23:45:45 UTC+7 Thứ Hai, ngày 10 tháng 10 năm 2016, OvermindDL1 đã >>> viết: >>>> >>>> Yep, just call it from the subscriptions callback then. >>>> >>>> >>>> On Monday, October 10, 2016 at 10:36:58 AM UTC-6, [email protected] >>>> wrote: >>>>> >>>>> This is quite a good idea. But can we define our own function that can >>>>> return a Sub Msg? >>>>> >>>>> Vào 20:41:40 UTC+7 Thứ Hai, ngày 10 tháng 10 năm 2016, Wouter In t >>>>> Velt đã viết: >>>>>> >>>>>> (disclaimer: I am also quite new to Elm) >>>>>> I always try to avoid creating my own Cmd: there is usually a better, >>>>>> less bug-prone way to achieve this (e.g. with resursive update calls). >>>>>> >>>>>> That said: I can see the logic in having a Msg originating from a >>>>>> "real" player, and another Msg from your AI player. >>>>>> >>>>>> What you could do, is make a subscription to Time, that is only >>>>>> active if it is the AI's turn to move. >>>>>> That way, you get a) a delay before the AI makes a move + b) you do >>>>>> not need to create your own command >>>>>> Whenever it is the user's turn, or as soon as the game is over, the >>>>>> subscription is turned off. >>>>>> >>>>>> Something like this: >>>>>> >>>>>> subscriptions : Model -> Sub Msg >>>>>> subscriptions model = >>>>>> if (gameIsNotFinished model && model.turn == AIToMove) then >>>>>> Time.every second AIMove >>>>>> else >>>>>> Sub.none >>>>>> >>>>>> You would of course have to define your own functions to check if the >>>>>> game is still playing (no winner and no draw) and if it is the AI's turn >>>>>> to >>>>>> play. >>>>>> >>>>> -- 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.
