如何在此榆木效果示例中添加第二个骰子? [英] How do I add a second die to this elm effects example?

查看:55
本文介绍了如何在此榆木效果示例中添加第二个骰子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Elm的新手,一直在看下面的示例(请注意,这是在更新的0.17体系结构下,其中Action现在是Command):
http://elm-lang.org/examples/random

I am new to Elm and have been looking at the following example (note this is under the newer 0.17 architecture, where Action is now Command): http://elm-lang.org/examples/random

存在后续挑战在示例中添加第二个骰子,因此单击该按钮将为每个骰子滚动一个新值。我的想法是更改模型以容纳两个单独的值,每个骰子一个,ala

There is a follow up challenge to add a second die to the example, so that a single click of the button rolls a new value for each die. My idea is to change the model to hold two separate values, one for each die, ala

type alias Model =
       { dieFace1 : Int
       , dieFace2 : Int
       }

直到我到达更新块。我不确定如何更新随机数生成器以创建两个值。

This works fine until I get to the update block. I am not sure how to update the Random number generator to create two values. The function is a bit confusing to me.

type Msg
  = Roll
  | NewFace Int Int


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll ->
      **(model, Random.generate NewFace (Random.int 1 6))** <-- not sure what to do here

    NewFace newFace1 newFace2 ->
      (Model newFace1 newFace2, Cmd.none)

Random.generate函数的文档有点轻-

The documentation for the Random.generate function is a bit light -


generate:(a-> msg)->生成器a-> Cmd msg

generate : (a -> msg) -> Generator a -> Cmd msg

创建一个将生成随机值的命令。

Create a command that will generate random values.

这是处理两个骰子的正确方法吗? , 或者,还有更好的方法?我是榆木菜鸟,请保持友善:)

Is this even the correct approach to handle two dice, or is there a better way? I am an elm noob, please be nice :)

推荐答案

Random.int 是为您提供单个随机int的原始生成器。您需要一个生成器,该生成器恰好为您提供两个随机整数。

Random.int is a primitive generator that gives you a single random int. You need a generator that gives you exactly two random integers.

随机数生成器可以由更多原始生成器组成,从而创建更复杂的生成器。幸运的是,Elm具有这样的功能, Random.pair ,您可以为元组的每个部分指定要使用的两个生成器。

Random number generators can be built up from more primitive generators to create more complex generators. Fortunately, Elm has just such a function, Random.pair which lets you specify which two generators you want for each part of the tuple.

将die生成器放到自己的函数中以避免重复自己:

Let's pull the die generator into its own function to avoid repeating ourselves:

dieGenerator : Random.Generator Int
dieGenerator =
  Random.int 1 6

现在,我们可以构建另一个生成器,该生成器为我们提供一对模具的随机值:

Now we can build another generator that gives us the random value of a pair of die:

diePairGenerator : Random.Generator (Int, Int)
diePairGenerator =
  Random.pair dieGenerator dieGenerator

由于我们正在处理一个整数元组,因此让我们更新 NewFace Int Int Msg 定义>到 NewFaces(Int,Int)。这将使您的 Roll 处理程序变得干净整洁:

Since we're dealing with a tuple of ints, let's update your Msg definition of NewFace Int Int to NewFaces (Int, Int). That will allow your Roll handler to be nice and clean:

Roll ->
  (model, Random.generate NewFaces diePairGenerator)

如果您想尝试超越此范围,考虑允许任意数量的骰子滚动所需的时间。采取从更多原始生成器构建复杂生成器的想法,并使用 Random 为我们提供了指南。

If you want to try moving beyond this, think about what it would take to allow for any number of die to be rolled. Take this idea of building complex generators from more primitive generators and use the documentation for the Random module us a guide.

这篇关于如何在此榆木效果示例中添加第二个骰子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆