forked from HeinrichApfelmus/threepenny-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissingDollars.hs
134 lines (116 loc) · 4.48 KB
/
MissingDollars.hs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import Control.Monad
import Safe
import Paths
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Missing Dollars
------------------------------------------------------------------------------}
main :: IO ()
main = do
static <- getStaticDir
startGUI defaultConfig
{ tpPort = 10000
, tpStatic = Just static
} setup
setup :: Window -> UI ()
setup w = void $ do
return w # set title "Missing Dollars"
UI.addStyleSheet w "missing-dollars.css"
(headerView,headerMe) <- mkHeader
riddle <- mkMissingDollarRiddle headerMe
let layout = [element headerView] ++ riddle ++ attributionSource
getBody w #+ [UI.div #. "wrap" #+ layout]
mkHeader :: UI (Element, Element)
mkHeader = do
headerMe <- string "..."
view <- UI.h1 #+ [string "The ", element headerMe, string " Dollars"]
return (view, headerMe)
attributionSource :: [UI Element]
attributionSource =
[ UI.p #+
[ UI.anchor #. "view-source" # set UI.href urlSource
#+ [string "View source code"]
]
, UI.p #+
[ string "Originally by "
, UI.anchor # set UI.href urlAttribution #+ [string "Albert Lai"]
]
]
where
urlSource = "https://github.com/HeinrichApfelmus/threepenny-gui/blob/master/src/MissingDollars.hs"
urlAttribution = "http://www.vex.net/~trebla/humour/missing_dollar.html"
mkMissingDollarRiddle :: Element -> UI [UI Element]
mkMissingDollarRiddle headerMe = do
-- declare input and display values
(hotelOut : hotelCost : hotelHold : _)
<- sequence . replicate 3 $
UI.input # set (attr "size") "3" # set (attr "type") "text"
(hotelChange : hotelRet : hotelBal : hotelPocket :
hotelBal2 : hotelPocket2 : hotelSum : hotelMe : _)
<- sequence . replicate 8 $ UI.span
-- update procedure
let updateDisplay out cost hold = do
let change = out - cost
ret = change - hold
bal = out - ret
sum = bal + hold
diff = sum - out
element hotelOut # set value (show out)
element hotelCost # set value (show cost)
element hotelHold # set value (show hold)
element hotelChange # set text (show change)
element hotelRet # set text (show ret)
element hotelBal # set text (show bal)
element hotelPocket # set text (show hold)
element hotelBal2 # set text (show bal)
element hotelPocket2 # set text (show hold)
element hotelSum # set text (show sum)
if diff >= 0
then do element hotelMe # set text ("extra $" ++ show diff ++ " come from")
element headerMe # set text "Extra"
else do element hotelMe # set text ("missing $" ++ show (-diff) ++ " go")
element headerMe # set text "Missing"
return ()
-- initialize values
updateDisplay 30 25 2
-- calculate button
calculate <- UI.button #+ [string "Calculate"]
on UI.click calculate $ \_ -> do
result <- mapM readMay `liftM` getValuesList [hotelOut,hotelCost,hotelHold]
case result of
Just [getout,getcost,gethold] -> updateDisplay getout getcost gethold
_ -> return ()
return $
[ UI.h2 #+ [string "The Guests, The Bellhop, And The Pizza"]
, UI.p #+
[ string "Three guests went to a hotel and gave $"
, element hotelOut
, string " to the bellhop to buy pizza. The pizza cost only $"
, element hotelCost
, string ". Of the $"
, element hotelChange
, string " change, the bellhop kept $"
, element hotelHold
, string " to himself and returned $"
, element hotelRet
, string " to the guests."
]
, UI.p #+
[ string "So the guests spent $"
, element hotelBal
, string ", and the bellhop pocketed $"
, element hotelPocket
, string ". Now "
, string "$"
, element hotelBal2
, string "+$"
, element hotelPocket2
, string "=$"
, element hotelSum
, string ". Where did the "
, element hotelMe
, string "?"
]
, element calculate
]