-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
109 lines (92 loc) · 2.76 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import Game exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Lazy exposing (..)
import Array
import Json.Decode exposing (bool, (:=))
input = Signal.mailbox (Game.Try (0,0))
-- game = init 100 100 0.15 0
game = init 20 20 0.15 0
main = Signal.map view (Signal.foldp update game input.signal)
view game =
div []
[ viewHeader game
, viewField game.field
]
viewHeader game =
div []
[ h1 [] [text "Minesweeper"]
, viewStatus game.status
, p [] <| (mineCount game.mineCount) ++ [text ", "] ++ (flagCount game.flagCount)
]
viewStatus status =
case status of
Playing -> p [] [text "Click to open a cell, shift-click to toggle a flag."]
GameOver -> h2 [] [text "Game over!"]
GameWon -> h2 [] [text "You won!"]
mineCount mines =
[text "Number of mines: ", boldNumber mines]
flagCount flags =
case flags of
0 -> [text "no flags placed"]
_ -> [text "flags: ", boldNumber flags]
boldNumber number =
span [style [("font-weight", "bold")]] [text <| toString number]
viewField field =
field
|> Array.indexedMap (lazy2 viewRow)
|> Array.toList
|> table [ style tableStyle ]
viewRow y row =
row
|> Array.indexedMap (\x cell -> lazy2 viewCell (x, y) cell)
|> Array.toList
|> tr []
viewCell loc cell =
let handleClick shiftKeyPressed =
if shiftKeyPressed then
Signal.message input.address (Game.ToggleFlag loc)
else
Signal.message input.address (Game.Try loc)
in
td [ on "click" ("shiftKey" := bool) handleClick, style <| cellStyle cell ]
[ text <| case cell.status of
Closed -> ""
-- Closed -> if cell.mine then "*" else ""
Open -> if cell.closeMines == 0 then "" else toString cell.closeMines
Exploded -> "E"
Flagged -> "F"
]
-- STYLES
tableStyle =
[ ("border", "solid black 3px")
]
cellStyle cell =
let
background =
case cell.status of
Closed -> [ ("background", "silver") ]
Exploded -> [ ("background", "red") ]
Flagged -> [ ("background", "green") ]
_ -> []
text =
case cell.status of
Open -> [ ("color", textcolor cell) ]
_ -> []
in
baseCellStyle ++ background ++ text
baseCellStyle =
[ ("border", "solid black 1px")
, ("width", "20px")
, ("height", "20px")
, ("text-align", "center")
, ("vertical-align", "middle")
, ("font-weight", "bold")
]
textcolor cell =
case cell.closeMines of
1 -> "blue"
2 -> "green"
3 -> "red"
_ -> "black"