-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdouble.html
More file actions
357 lines (342 loc) · 11.6 KB
/
double.html
File metadata and controls
357 lines (342 loc) · 11.6 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Backgammon Tutorial: When to double/take in race conditions</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<h1>
Backgammon Tutorial:</h1>
<h2>
When to double/take in race conditions</h2>
<div id="board">
<div id="13" class="pip top-pip red-pip">
</div>
<div id="14" class="pip top-pip black-pip">
</div>
<div id="15" class="pip top-pip red-pip">
</div>
<div id="16" class="pip top-pip black-pip">
</div>
<div id="17" class="pip top-pip red-pip">
</div>
<div id="18" class="pip top-pip black-pip">
</div>
<div id="black-bar" class="bar">
</div>
<div id="19" class="pip top-pip red-pip">
</div>
<div id="20" class="pip top-pip black-pip">
</div>
<div id="21" class="pip top-pip red-pip">
</div>
<div id="22" class="pip top-pip black-pip">
</div>
<div id="23" class="pip top-pip red-pip">
</div>
<div id="24" class="pip top-pip black-pip">
</div>
<div id="black-home" class="pip">
</div>
<br class="clear" />
<div id="12" class="pip bottom-pip black-pip">
</div>
<div id="11" class="pip bottom-pip red-pip">
</div>
<div id="10" class="pip bottom-pip black-pip">
</div>
<div id="9" class="pip bottom-pip red-pip">
</div>
<div id="8" class="pip bottom-pip black-pip">
</div>
<div id="7" class="pip bottom-pip red-pip">
</div>
<div id="red-bar" class="bar">
</div>
<div id="6" class="pip bottom-pip black-pip">
</div>
<div id="5" class="pip bottom-pip red-pip">
</div>
<div id="4" class="pip bottom-pip black-pip">
</div>
<div id="3" class="pip bottom-pip red-pip">
</div>
<div id="2" class="pip bottom-pip black-pip">
</div>
<div id="1" class="pip bottom-pip red-pip">
</div>
<div id="red-home" class="pip">
</div>
</div>
<br class="clear" />
<div id="question">
<h2>
Question:</h2>
<div>
What should Red do if Black doubles?<br class="clear" />
<button onclick="reveal()">
drop</button>
<button onclick="reveal()">
take</button>
</div>
<div>
What if it is Red's turn to play?<br class="clear" />
<button onclick="reveal()">
play on</button>
<button onclick="reveal()">
double, but not redouble</button>
<button onclick="reveal()">
double or redouble</button>
</div>
</div>
<div id="answer" style="display: none">
<h2>
Answer:</h2>
<div>
Black: <span id="black-pip-count" />pips
</div>
<div>
Red: <span id="red-pip-count" />pips
</div>
<div>
Correct play: <span id="correct-play" />
</div>
<button onclick="reset()">
New board</button>
</div>
<style type="text/css">
BODY
{
font-family: Arial;
}
#board
{
background-image: url('');
width: 100%;
}
DIV.counter
{
float: left;
width: 20px;
height: 20px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
DIV.red
{
background: red;
text-align: center;
color: Black;
font-weight: bold;
}
DIV.black
{
background: black;
text-align: center;
color: Red;
font-weight: bold;
}
DIV.bar
{
float: left;
width: 20px;
height: 100px;
background: brown;
}
DIV.pip
{
float: left;
width: 20px;
height: 100px;
}
DIV.bottom-pip
{
/*
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 100px solid;
*/
}
DIV.top-pip
{
/*
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 100px solid;
*/
}
DIV.red-pip
{
background: #F99;
border-top-color: #F99;
border-bottom-color: #F99;
}
DIV.black-pip
{
background: #999;
border-top-color: #999;
border-bottom-color: #999;
}
BR.clear
{
clear: both;
}
</style>
<script type="text/javascript">
Plays = {
PLAY: "Red should just play on",
DROP: "Red should drop if doubled",
TAKE: "Red should take if doubled",
DOUBLE: "Red should double, but not redouble",
REDOUBLE: "Red should double or redouble"
};
var _layout = new Array(25);
var _players = ["red", "black"];
function init() {
for (var i = 0; i < 25; i++) {
_layout[i] = [0, 0];
}
}
function getRandomPipNumber(max) {
if (max <= 6) {
// in home board - can include zeroes
return Math.floor(Math.random() * (max + 1));
}
else {
// only allow 1 -> max.
return Math.floor(Math.random() * max) + 1;
}
}
function setRandomPipForPlayerNumber(playerNumber, maxPip) {
var r = getRandomPipNumber(maxPip);
_layout[r][playerNumber]++;
}
function clearBoard() {
$("DIV#red-home").children().remove();
$("DIV#black-home").children().remove();
for (var i = 1; i < 25; i++) {
$("DIV#" + i).children().remove();
}
}
function drawBoard(layout) {
clearBoard();
for (var player = 0; player < 2; player++) {
var playerName = _players[player];
for (var pip = 0; pip < 25; pip++) {
var totalCounters = layout[pip][player];
if (totalCounters > 0) {
if (pip == 0) {
if (player == 0) {
for (var i = 0; i < 4; i++) {
// blank counter as placeholder
$("DIV#" + playerName + "-home").append($("<DIV>").addClass("counter"));
}
}
// home
$("DIV#" + playerName + "-home").append($("<DIV>").text(totalCounters).addClass("counter " + playerName));
}
else {
var pipDivNumber;
if (player == 0) {
pipDivNumber = pip;
} else {
pipDivNumber = 25 - pip;
}
var pipDiv = $("DIV#" + pipDivNumber);
for (var i = 0; i < 5; i++) {
if (pipDivNumber < 13) {
if (i >= 5 - totalCounters) {
if (totalCounters > 5 && i == 5 - 1) {
pipDiv.append($("<DIV>").text(totalCounters).addClass("counter " + playerName));
} else {
pipDiv.append($("<DIV>").addClass("counter " + playerName));
}
} else {
// blank counter as placeholder
pipDiv.append($("<DIV>").addClass("counter"));
}
}
else {
if (i < totalCounters) {
if (totalCounters > 5 && i == 0) {
pipDiv.append($("<DIV>").text(totalCounters).addClass("counter " + playerName));
} else {
pipDiv.append($("<DIV>").addClass("counter " + playerName));
}
} else {
// blank counter as placeholder
pipDiv.append($("<DIV>").addClass("counter"));
}
}
}
}
}
}
}
}
function countPips(layout, piece) {
var sum = 0;
for (var i = 1; i < layout.length; i++) {
sum += i * layout[i][piece];
}
return sum;
}
function doLayout() {
// find point from which race is on:
var racePivot = 4 + Math.floor(Math.random() * 18);
for (var i = 0; i < 15; i++) {
setRandomPipForPlayerNumber(0, racePivot);
setRandomPipForPlayerNumber(1, 24 - racePivot);
}
drawBoard(_layout);
var redPipCount = countPips(_layout, 0);
var blackPipCount = countPips(_layout, 1);
var correctPlay;
if (redPipCount < blackPipCount) {
var diff = blackPipCount / redPipCount;
if (diff > 1.09) {
correctPlay = Plays.REDOUBLE;
}
else if (diff > 1.08) {
correctPlay = Plays.DOUBLE;
}
else {
correctPlay = Plays.TAKE;
}
}
else {
var diff = redPipCount / blackPipCount;
if (diff > 1.12) {
correctPlay = Plays.DROP;
}
else {
correctPlay = Plays.TAKE;
}
}
$("#red-pip-count").text(redPipCount);
$("#black-pip-count").text(blackPipCount);
$("#correct-play").text(correctPlay);
}
function reset() {
$("#answer").hide();
init();
doLayout();
}
function reveal() {
$("#answer").show();
}
reset();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17476465-3']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>