/
solution.html
249 lines (192 loc) · 6.14 KB
/
solution.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<html>
<head>
<meta charset="utf-8" />
<style>
body {
background: DarkCyan;
}
.card {
position: relative;
width: 80px;
height: 120px;
border: 1px solid black;
background-color: white;
font-weight: bold;
font-size: 25px;
border-radius: 5px;
box-shadow: 2px -2px 9px -1px black;
margin:15px;
padding:2px;
}
.diamonds, .hearts {
color: black;
}
.clubs, .spades {
color: red;
}
.top {
top: 2px;
left: 2px;
}
.bigsuit {
text-align: center;
font-size: 55px;
}
.bottom {
right: 2px;
text-align: right;
}
.controlpanel {
margin: auto;
text-align: center;
border: 3px solid #73AD21;
padding: 10px;
color: white;
font-family: "Comic Sans MS";
}
.controlpanel div {
padding: 20px;
}
button {
padding: 10px;
margin-right: 75px;
margin-left: 75px;
font-family: "Comic Sans MS";
font-size: 16px;
}
</style>
</head>
<body>
<div class="controlpanel">
<h1>Blackjack!</h1>
<div>
<button id="playBtn">New Game!</button>
<button id="hitBtn" style="display: none">Hit!</button>
<button id="stayBtn" style="display: none">Stay</button>
</div>
<div id="statusMsg" class="center">Press New Game button to start.</div>
<div>Total: <span id="totalSpan">0</span></div>
</div>
<div id="cards"></div>
<script>
const suits =
() => [
{ suit: "hearts", symbol: '♥' }, // symbol: '♥'
{ suit: 'diamonds', symbol: '♦' }, // symbol: '♦'
{ suit: 'spades', symbol: '♠' }, // symbol: '♠'
{ suit: 'clubs', symbol: '♣' } // symbol: '♣'
];
const rankNames =
() => ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
const ranks =
rankNames => rankNames.map(
(rank, index) => ({ rank, value: Math.min(10, index + 1) }));
const createOrderedDeck =
(suits, ranks) => suits.reduce(
(deck, suit) => {
const cards = ranks.map(rank => ({ ...rank, ...suit }));
return deck.concat(cards);
}, []);
const compose =
(...fns) => input => fns.reduceRight((prev, fn) => fn(prev), input);
const pipe = (...fns) => input => fns.reduce((prev, fn) => fn(prev), input);
const map = fx => arr => arr.map(fx);
const addRandom =
randomizer => deck => deck.map(card => ({
random: randomizer(),
card
}));
const sortByRandom =
deck => [...deck].sort((a, b) => a.random - b.random);
const shuffle =
(deck, randomizer) => {
const doShuffle = pipe(
addRandom(randomizer),
sortByRandom,
map(card => card.card)
);
return doShuffle(deck);
};
const draw =
(deck, n = 1) => [deck.slice(0, n), deck.slice(n)];
const sumCards =
cards => cards.reduce((total, card) => total + card.value, 0);
const isBust =
total => total > 21;
const isGameOver =
(bust, stay) => bust || stay;
const updateCardDisplay =
({ updateHTML }, hand) => {
const cardHtml = hand.map((card, index) =>
`<div class="card ${card.suit}"
style="top: -${index * 120}px;
left: ${index * 100}px;">
<div class="top rank">${card.rank}</div>
<div class="bigsuit">${card.symbol}</div>
<div class="bottom rank">${card.rank}</div>
</div>`);
updateHTML("cards", cardHtml.join(""));
};
const showOrHide =
(updateStyle, element, hide) =>
updateStyle(element, "display", hide ? "none" : "");
const updateStatusDisplay =
({ updateStyle, updateHTML }, hand, stay) => {
const total = sumCards(hand);
updateHTML("totalSpan", total);
const bust = isBust(total);
const gameover = isGameOver(bust, stay);
showOrHide(updateStyle, "playBtn", !gameover);
showOrHide(updateStyle, "hitBtn", gameover);
showOrHide(updateStyle, "stayBtn", gameover);
let statusMsg = gameover ?
"Game over. Press New Game button to start again." :
"Select Hit or Stay";
statusMsg = bust ? "You went bust!!! " + statusMsg : statusMsg;
updateHTML("statusMsg", statusMsg);
};
const playHandler = (randomizer, { getState, setState }) => () => {
const orderedDeck = createOrderedDeck(suits(), ranks(rankNames()));
let gameDeck = shuffle(orderedDeck, randomizer);
[hand, gameDeck] = draw(gameDeck, 2);
setState(hand, gameDeck);
};
const hitHandler = ({ getState, setState }) => () => {
[hand, gameDeck] = getState();
[card, gameDeck] = draw(gameDeck, 1);
setState(hand.concat(card), gameDeck);
};
const stayHandler = ({ getState, setState }) => () => {
[hand, gameDeck] = getState();
setState(hand, gameDeck, true);
};
// impure functions
const byId =
elementId => document.getElementById(elementId);
const updateHTML =
(elementId, html) => byId(elementId).innerHTML = html;
const updateStyle =
(elementId, style, value) => byId(elementId).style[style] = value;
const randomizer =
Math.random;
const dom = { updateHTML, updateStyle };
const startGame = (state) => {
byId("playBtn").addEventListener("click", playHandler(randomizer, state));
byId("hitBtn").addEventListener("click", hitHandler(state));
byId("stayBtn").addEventListener("click", stayHandler(state));
}
const createState = (dom) => {
let _state;
const getState = () => [..._state];
const setState =
(hand, gameDeck, stay = false) => {
_state = [hand, gameDeck];
updateCardDisplay(dom, hand);
updateStatusDisplay(dom, hand, stay);
};
return { getState, setState };
}
startGame(createState(dom));
</script>
</body>
</html>