SlideShare a Scribd company logo
Please, correct the following ELM code:
module Main exposing (..)
import Browser
import Html exposing (Html, button, div, text)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
import List
import Random
import Basics exposing (modBy)
type alias Model =
{ board : List Int
, gridSize : Int
, emptyIndex : Int
}
init : Model
init =
{ board = List.range 1 15 ++ [ 0 ]
, gridSize = 4
, emptyIndex = 15
}
type Msg
= Shuffle
| Move Int
update : Msg -> Model -> Model
update msg model =
case msg of
Shuffle ->
let
shuffledBoard =
Random.shuffle model.board
in
{ model | board = shuffledBoard }
Move idx ->
if isAdjacent model model.gridSize idx then
{ model | board = swap model.board model.emptyIndex idx, emptyIndex = idx }
else
model
isAdjacent : Model -> Int -> Int -> Bool
isAdjacent model gridSize idx =
let
rowDiff =
abs <| (idx // gridSize) - (model.emptyIndex // gridSize)
colDiff =
abs <| (idx `modBy` gridSize) - (model.emptyIndex `modBy` gridSize)
in
(rowDiff == 1 && colDiff == 0) || (rowDiff == 0 && colDiff == 1)
swap : List Int -> Int -> Int -> List Int
swap list index1 index2 =
let
value1 = List.nth index1 list
value2 = List.nth index2 list
in
case ( value1, value2 ) of
( Just a, Just b ) ->
List.indexedMap (i x -> if i == index1 then b else if i == index2 then a else x) list
_ ->
list
view : Model -> Html Msg
view model =
let
cell idx number =
button
[ style "width" "50px"
, style "height" "50px"
, onClick <| Move idx
]
[ if number == 0 then text "" else text <| String.fromInt number ]
in
div []
[ div [ style "display" "grid", style "grid-template-columns" "repeat(4, 50px)" ]
<| List.indexedMap cell model.board
, button [ onClick (Shuffle) ] [ text "Shuffle" ]
]
main : Program () Model Msg
main =
Browser.sandbox { init = init, update = update, view = view }
//Dependencies:
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
//HTML
Fifteen Puzzle

More Related Content

PDF
Elm 0.17 at Dublin Elm Meetup May 2016
PDF
Rethink Frontend Development With Elm
PPTX
dotnetConf2019 meetup in AICHI / Elmish
PDF
Inventory management
PPTX
Python programming workshop session 3
PDF
The Ring programming language version 1.3 book - Part 83 of 88
PPTX
Nokia Asha App Development - Part 2
PDF
PyData Paris 2015 - Track 1.2 Gilles Louppe
Elm 0.17 at Dublin Elm Meetup May 2016
Rethink Frontend Development With Elm
dotnetConf2019 meetup in AICHI / Elmish
Inventory management
Python programming workshop session 3
The Ring programming language version 1.3 book - Part 83 of 88
Nokia Asha App Development - Part 2
PyData Paris 2015 - Track 1.2 Gilles Louppe

Similar to Please, correct the following ELM codemodule Main exposing (..).pdf (20)

PDF
The Ring programming language version 1.3 book - Part 32 of 88
PDF
Chainable datasource
PDF
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
PDF
Introducción a Elixir
PDF
The Ring programming language version 1.6 book - Part 44 of 189
PDF
Documentation For Tab Setup
PPT
Lists and scrollbars
PPTX
Unit – I-AWT-updated.pptx
PDF
PureScript & Pux
PPTX
Online birth certificate system and computer engineering
PDF
program#include iostreamusing namespace std;void calculatio.pdf
PDF
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
PDF
A proper introduction to Elm
PPTX
Introduction to python programming 1
KEY
An Introduction to Functional Programming using Haskell
ODT
Eclipse Tricks
PPT
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
PDF
Ch 4
PDF
The Ring programming language version 1.5.4 book - Part 42 of 185
PPTX
Gui programming
The Ring programming language version 1.3 book - Part 32 of 88
Chainable datasource
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Introducción a Elixir
The Ring programming language version 1.6 book - Part 44 of 189
Documentation For Tab Setup
Lists and scrollbars
Unit – I-AWT-updated.pptx
PureScript & Pux
Online birth certificate system and computer engineering
program#include iostreamusing namespace std;void calculatio.pdf
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
A proper introduction to Elm
Introduction to python programming 1
An Introduction to Functional Programming using Haskell
Eclipse Tricks
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
Ch 4
The Ring programming language version 1.5.4 book - Part 42 of 185
Gui programming
Ad

More from agarshailenterprises (20)

PDF
pls help! 1. Understanding the implications of taxes on welfare The .pdf
PDF
Please write one paragraph below demonstratingDecide on a hedging.pdf
PDF
Please use these instructions to help.Use the flexbox layout mode .pdf
PDF
Please use these instructions to help. Please only edit the CSS, and.pdf
PDF
Please write a program with a simple INTERFACE. Provide a design for.pdf
PDF
please tell me what lines do i alter to make this stack a queue. tel.pdf
PDF
Please show how to do these calculation based on 2021 Starbucks An.pdf
PDF
Please review the two journals on Endogenous growth Theory. 1. Shaw..pdf
PDF
Please read the case study below and answer the following questions .pdf
PDF
Please modify the following code in C. Make sure your code matches t.pdf
PDF
Please provide a detailed explanation as to how you get to the answe.pdf
PDF
please I need explanation for the whole queston and why the answer I.pdf
PDF
Please help with part b. Taylor Talbot receives wages amounting to .pdf
PDF
Please explain how to find The heights of a certain population of co.pdf
PDF
Please help me get the right answers. The Economist publishes a year.pdf
PDF
Please explain in detail. Thank you! 2. Consider two independent sta.pdf
PDF
Please explain in detail. Thank you! 10. The joint probability distr.pdf
PDF
please help 3. Which of the four classification schemes (suggested b.pdf
PDF
Please expound on the specific details and examples of each componen.pdf
PDF
please dont solve 1) [12 marks] Suppose Y1 and Y2 be independent.pdf
pls help! 1. Understanding the implications of taxes on welfare The .pdf
Please write one paragraph below demonstratingDecide on a hedging.pdf
Please use these instructions to help.Use the flexbox layout mode .pdf
Please use these instructions to help. Please only edit the CSS, and.pdf
Please write a program with a simple INTERFACE. Provide a design for.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdf
Please show how to do these calculation based on 2021 Starbucks An.pdf
Please review the two journals on Endogenous growth Theory. 1. Shaw..pdf
Please read the case study below and answer the following questions .pdf
Please modify the following code in C. Make sure your code matches t.pdf
Please provide a detailed explanation as to how you get to the answe.pdf
please I need explanation for the whole queston and why the answer I.pdf
Please help with part b. Taylor Talbot receives wages amounting to .pdf
Please explain how to find The heights of a certain population of co.pdf
Please help me get the right answers. The Economist publishes a year.pdf
Please explain in detail. Thank you! 2. Consider two independent sta.pdf
Please explain in detail. Thank you! 10. The joint probability distr.pdf
please help 3. Which of the four classification schemes (suggested b.pdf
Please expound on the specific details and examples of each componen.pdf
please dont solve 1) [12 marks] Suppose Y1 and Y2 be independent.pdf
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
master seminar digital applications in india
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Lesson notes of climatology university.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PDF
RMMM.pdf make it easy to upload and study
PDF
Trump Administration's workforce development strategy
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
master seminar digital applications in india
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Orientation - ARALprogram of Deped to the Parents.pptx
Lesson notes of climatology university.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
RMMM.pdf make it easy to upload and study
Trump Administration's workforce development strategy
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Complications of Minimal Access Surgery at WLH

Please, correct the following ELM codemodule Main exposing (..).pdf

  • 1. Please, correct the following ELM code: module Main exposing (..) import Browser import Html exposing (Html, button, div, text) import Html.Attributes exposing (style) import Html.Events exposing (onClick) import List import Random import Basics exposing (modBy) type alias Model = { board : List Int , gridSize : Int , emptyIndex : Int } init : Model init = { board = List.range 1 15 ++ [ 0 ] , gridSize = 4 , emptyIndex = 15 } type Msg = Shuffle | Move Int update : Msg -> Model -> Model update msg model = case msg of Shuffle -> let shuffledBoard = Random.shuffle model.board in
  • 2. { model | board = shuffledBoard } Move idx -> if isAdjacent model model.gridSize idx then { model | board = swap model.board model.emptyIndex idx, emptyIndex = idx } else model isAdjacent : Model -> Int -> Int -> Bool isAdjacent model gridSize idx = let rowDiff = abs <| (idx // gridSize) - (model.emptyIndex // gridSize) colDiff = abs <| (idx `modBy` gridSize) - (model.emptyIndex `modBy` gridSize) in (rowDiff == 1 && colDiff == 0) || (rowDiff == 0 && colDiff == 1) swap : List Int -> Int -> Int -> List Int swap list index1 index2 = let value1 = List.nth index1 list value2 = List.nth index2 list in case ( value1, value2 ) of ( Just a, Just b ) -> List.indexedMap (i x -> if i == index1 then b else if i == index2 then a else x) list _ -> list view : Model -> Html Msg view model = let cell idx number = button [ style "width" "50px" , style "height" "50px"
  • 3. , onClick <| Move idx ] [ if number == 0 then text "" else text <| String.fromInt number ] in div [] [ div [ style "display" "grid", style "grid-template-columns" "repeat(4, 50px)" ] <| List.indexedMap cell model.board , button [ onClick (Shuffle) ] [ text "Shuffle" ] ] main : Program () Model Msg main = Browser.sandbox { init = init, update = update, view = view } //Dependencies: { "type": "application", "source-directories": [ "src" ], "elm-version": "0.19.1", "dependencies": { "direct": { "elm/browser": "1.0.2", "elm/core": "1.0.5", "elm/html": "1.0.0" }, "indirect": { "elm/json": "1.1.3", "elm/time": "1.0.0", "elm/url": "1.0.0", "elm/virtual-dom": "1.0.2" } },