最近、関数型プログラミングをもっと詳しく見てみることにしたので、私はElmから始めることにしました。反復配列中のエルムデクリメントカウンタ
私は実際にこのコンセプトと言語と戦っているように感じています。私の思考プロセスはすべて間違っているので、誰かが私の簡単なエクササイズで私を助けてくれることを望んでいました。
私はMinesweeperゲームを作成しようとしています。まず、私は鉱山でグリッドを作成しようとしていました。
ここに私のコードは、これまでもちろん
import Array exposing (Array)
type State = Hidden | Tagged | Revealed
type alias Cell = {isMine: Bool, nearbyMines: Int, state: State}
type alias Board = Array (Array Cell)
mines : Int
mines = 100
initCell : Cell
initCell =
{isMine = False, nearbyMines = 0, state = Hidden}
generate : Int -> Int -> Board
generate lines cols =
Array.initialize lines (\y ->
Array.initialize cols (\x -> initCell))
markAsMine : Int -> Int -> Cell -> Cell
markAsMine x y cell =
if mines > 0
then {cell | isMine = True}
else cell
fillMines : Int -> Board -> Board
fillMines amount board =
board
|> Array.indexedMap (\y row -> row |> Array.indexedMap (\x cell-> markAsMine x y cell))
だ、このすべてが行いますが、鉱山としてすべてのセルをマークしているので、どのようにカウンターに私は私のような細胞をマークするたびにデクリメントしますか? これは些細なものでなければならないので、なぜ私はさまざまなプログラミングパラダイムに苦しんでいると思いますか?
ご協力いただきありがとうございます。 乾杯!