[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
Op woensdag 12 oktober 2016 06:17:37 UTC+2 schreef hoang...@gmail.com: > > But if I do it this way, it's gonna return both the player's move and the > AI's move at the same time. What I want to have is that the game displays > the player's move THEN call minimax function on current model and THEN > display the AI's move > The recursive call still needs a separate Msg (AIMove) for the AIMove. This separate Msg is not needed: there is no outside event (user click or server or subscription) that calls this message. It is the update function itself (during processing of user moves) that calls this. So in that case, it would be better to use setup from Luke, to limit all Msg to outside events, and remove the AIMove message. But it sounds like you want something else: you need 2 discrete model states (1 after player move, 1 after AI move), because you want *both model states to be displayed separately*. And for that (as far as I know) you need the setup with subscription (+ some 'isProcessing' flag like OvermindDL1 suggested), and you also need to keep the AIMove message for the subscription command to call. -- 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
I'm sorry. Don't why your answer wasn't loaded that day. Aren't "set and unset whenever processing is started/finished" side-effects? We can do that easily in imperative languages but all functions in ELM are pure. Vào 03:55:33 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, OvermindDL1 đã viết: > > I'd set a flag on the model for when it is 'processing' (good flag name, > maybe `isProcessing` or so?) and just test that in the model and set and > unset it whenever processing is started/finished. :-) > > Just think of it as another piece of 'state'. In fact if it can be > gleaned from another piece of state (such as the animationframe being 'on') > then check that instead, you do not want duplicate pieces in your model > that hold essentially the same information, only have one place per piece > of data. :-) > > > On Monday, October 10, 2016 at 2:07:59 PM UTC-6, hoang...@gmail.com wrote: >> >> The AnimationFrame works perfectly. Thank you :D >> I have another question came in mind. How do you think can we make the >> view display, for example "Thinking", while the minimax algorithm is being >> executed? >> >> Thank you a lot for your help. :D >> >> Vào 00:11:59 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, OvermindDL1 đã viết: >>> >>> 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, hoang...@gmail.com >>> 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, hoang...@gmail.com > 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, hoang...@gmail.com >>> 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
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
But if I do it this way, it's gonna return both the player's move and the AI's move at the same time. What I want to have is that the game displays the player's move THEN call minimax function on current model and THEN display the AI's move Vào 06:41:14 UTC+7 Thứ Tư, ngày 12 tháng 10 năm 2016, Luke Westby đã viết: > > 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, hoang...@gmail.com 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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, hoang...@gmail.com 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
PS: The subscription to time is useful if you want some sort of timeout or delay between the user move and the AI move. If you want to do the AI move immediately after the user move (without delay), a pattern with a recursive update call (and without subscription) is probably more suited. Like so: update msg model = case msg of Toggle indexToToggle -> let modelAfterUserMove = { model | board = move indexToToggle model.turn model.board } in update AIMove modelAfterUserMove -- here you do a recursive update call without any delay AIMove -> ( fst (miniMax model 0) , Cmd.none ) -- 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
I had the solution. Just show thinking when its AI's turn and the game is not over. :D Vào 03:07:59 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, hoang...@gmail.com đã viết: > > The AnimationFrame works perfectly. Thank you :D > I have another question came in mind. How do you think can we make the > view display, for example "Thinking", while the minimax algorithm is being > executed? > > Thank you a lot for your help. :D > > Vào 00:11:59 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, OvermindDL1 đã viết: >> >> 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, hoang...@gmail.com >> 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, hoang...@gmail.com 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, hoang...@gmail.com >> 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
I'd set a flag on the model for when it is 'processing' (good flag name, maybe `isProcessing` or so?) and just test that in the model and set and unset it whenever processing is started/finished. :-) Just think of it as another piece of 'state'. In fact if it can be gleaned from another piece of state (such as the animationframe being 'on') then check that instead, you do not want duplicate pieces in your model that hold essentially the same information, only have one place per piece of data. :-) On Monday, October 10, 2016 at 2:07:59 PM UTC-6, hoang...@gmail.com wrote: > > The AnimationFrame works perfectly. Thank you :D > I have another question came in mind. How do you think can we make the > view display, for example "Thinking", while the minimax algorithm is being > executed? > > Thank you a lot for your help. :D > > Vào 00:11:59 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, OvermindDL1 đã viết: >> >> 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, hoang...@gmail.com >> 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, hoang...@gmail.com 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, hoang...@gmail.com >> 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
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
The AnimationFrame works perfectly. Thank you :D I have another question came in mind. How do you think can we make the view display, for example "Thinking", while the minimax algorithm is being executed? Thank you a lot for your help. :D Vào 00:11:59 UTC+7 Thứ Ba, ngày 11 tháng 10 năm 2016, OvermindDL1 đã viết: > > 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, hoang...@gmail.com > 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, hoang...@gmail.com >>> 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, hoang...@gmail.com > 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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, hoang...@gmail.com 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, hoang...@gmail.com >> 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, hoang...@gmail.com 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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, hoang...@gmail.com > 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, hoang...@gmail.com >>> 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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, hoang...@gmail.com 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, hoang...@gmail.com >> 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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, hoang...@gmail.com > 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[elm-discuss] Re: Subscribe to a model value then call update function when condition is met
(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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.