私は、ユーザーに5個の四角形のグリッドが表示され、9個がハイライト表示されているメモリゲームに取り組んでいます。ユーザはこれらの9つの四角形を覚えて選択しなければならない。エルムのメモリゲーム、ランダム化またはシャッフルグリッド
予想される/獲得したボード構成を生成するにはどうすればよいですか?
現在、私は命令型言語ではList (List Bool)
としてBoard
を持っている必要があり、JSのように、私はランダムに突然変異する列と行のインデックスを選択グリッド上で9回を繰り返すでしょう。それはエルムでは不可能なので、私はちょっと立ち往生しています。
カップルの戦略が心に来た:
戦略1
が使用Array
戦略2
無作為化グリッドを生成し、すべての座標をシャッフル、最初に選びます9.
注意タイルを選択する方法も必要ですのでBoard
はList (List Tile)
になります。Tile
は{ x: Int, y: Int, selected: Bool}
です。
私はすでにvanilla JSでゲームを書いています。あなたはここでそれを試してみることができます。
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as App
import Random
import Random.Array
import Array exposing (..)
-- model
type alias Board =
Array (Array Bool)
type alias Tile =
{ x : Int
, y : Int
, selected : Bool
}
type alias Model =
{ board : Board
, expectedBoard : Board
, gameOver : Bool
, playerWon : Bool
, turnCount : Int
}
initModel : (Model, Cmd Msg)
initModel =
({ board = generateSquareMatrix 5 False
, expectedBoard = Array.fromList []
, gameOver = False
, playerWon = False
, turnCount = 0
}
, Cmd.none
)
generateSquareMatrix : Int -> Bool -> Array (Array Bool)
generateSquareMatrix num value =
Array.repeat num (Array.repeat num value)
-- update
type Msg
= SelectTile Bool
| RestartGame
| SuffleBoard
| NewBoard (Array (Array Bool))
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case (Debug.log "msg" msg) of
SelectTile tile ->
({ model
| turnCount = model.turnCount + 1
}
, Cmd.none
)
RestartGame ->
({ model
| turnCount = 0
}
, Cmd.none
)
SuffleBoard ->
(model, Random.generate NewBoard (Random.Array.shuffle model.board))
NewBoard newBoard ->
({ model | board = newBoard }
, Cmd.none
)
-- view
view : Model -> Html Msg
view model =
div [ class "scoreboard" ]
[ h1 [] [ text "Recall" ]
, grid model
, button [ onClick SuffleBoard ] [ text "New Board" ]
, p [] [ text (toString model) ]
]
grid : Model -> Html Msg
grid model =
div [ class "recall-grid" ]
(List.map
(\row ->
div
[ class "recall-grid-row" ]
(List.map
(\tile ->
div [ class "recall-grid-tile", onClick (SelectTile tile) ] []
)
(Array.toList row)
)
)
(Array.toList model.board)
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
main : Program Never
main =
App.program
{ init = initModel
, view = view
, update = update
, subscriptions = subscriptions
}
ありがとう、チャド!ここで解析する方法はたくさんあります。私はそれがどのように行くかを見て実行してみましょう。フォローアップの質問があればコメントします。 –