-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvvh.js
467 lines (452 loc) · 13.6 KB
/
vvh.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
window.drawVVH = function(canvas, moveList) {
var c, cenPad, colSpace, disTop, dotDefault, draw, drawDot, drawDots, drawGrid, drawLine, grid, horCen, lineColor, linePadding, linecolor, loseC, maxH, maxRemote, maxVal, maxW, padx, pady, prevBestMoveVal, prevBestMoveX, prevBestMoveY, rowSpace, setFinalBestMove, setLineColor, setTempBestMove, setTurnNum, sketchLine, tempBestMoveVal, tempBestMoveX, tempBestMoveY, tempMoveVal, tempMoveX, tempMoveY, textcolor, tieC, turnNum, turnPadding, winC, xLabelPad, xlabel, yLabelPad, ylabel;
c = canvas;
turnNum = 0;
maxRemote = null;
lineColor = null;
tempBestMoveVal = null;
prevBestMoveVal = null;
tempMoveVal = null;
tempMoveX = null;
tempMoveY = null;
maxH = c.height;
maxW = c.width;
horCen = maxW / 2;
tempBestMoveX = null;
tempBestMoveY = null;
prevBestMoveX = null;
prevBestMoveY = null;
colSpace = null;
rowSpace = null;
padx = 15;
pady = 5;
disTop = 25;
xLabelPad = disTop - 5;
yLabelPad = disTop + 3;
cenPad = 10;
dotDefault = disTop + 3;
turnPadding = 25;
linePadding = disTop + 2.5;
tieC = "rgb(255, 255, 0)";
winC = "rgb(0, 127, 0)";
loseC = "rgb(139, 0, 0)";
textcolor = "white";
linecolor = "white";
/*
Determines which turn it is
*/
setTurnNum = function() {
return turnNum = moveList.length - 1;
};
/*
Gets the color of the selected move to use in drawing the line between
points on the VVH. Because the turns alternate, in order to draw the
line with the correct color, it's necessary to invert the color.
*/
setLineColor = function(turn) {
tempMoveVal = moveList[turn].board.value;
if (tempMoveVal === "lose") {
return lineColor = "win";
} else if (tempMoveVal === "win") {
return lineColor = "lose";
} else {
return lineColor = tempMoveVal;
}
};
/*
Sets the tempBestMove variable values to those associated
with the best move of the turn passed in. Add one to the
remoteness value to account for difference in indexing.
*/
setTempBestMove = function(turn) {
var i, loopTempVal, loopTempX;
loopTempX = null;
loopTempVal = null;
tempBestMoveY = turn;
i = 0;
/*
console.log turnNum
console.log i
console.log moveList
*/
tempMoveVal = moveList[turn].moves[i].value;
tempMoveX = moveList[turn].moves[i].remoteness;
i += 1;
while (i < moveList[turn].moves.length) {
loopTempX = moveList[turn].moves[i].remoteness;
loopTempVal = moveList[turn].moves[i].value;
if (tempMoveVal === "lose") {
if (loopTempVal === "lose") {
if (loopTempX > tempMoveX) {
tempMoveX = loopTempX;
}
} else if (loopTempVal === "tie") {
tempMoveX = loopTempX;
tempMoveVal = loopTempVal;
} else {
tempMoveX = loopTempX;
tempMoveVal = loopTempVal;
}
} else if (tempMoveVal === "tie") {
if (loopTempVal === "win") {
tempMoveX = loopTempX;
tempMoveVal = loopTempVal;
}
} else {
if (loopTempVal === "win") {
if (loopTempX < tempMoveX) {
tempMoveX = loopTempX;
}
}
}
i += 1;
}
tempBestMoveX = tempMoveX + 1;
return tempBestMoveVal = tempMoveVal;
};
setFinalBestMove = function() {
tempBestMoveX -= 1;
return tempBestMoveVal = "lose";
};
/*
Determines the maximum number of moves until the endgame based on the initial
moveList passed in. Add two to the largest value in the initial moveList so
that there are (largest value in moveList) number of spots on each side of
the grid's center line
*/
maxVal = function() {
var i;
i = 0;
maxRemote = moveList[0].moves[i].remoteness;
i += 1;
while (i < moveList[0].moves.length) {
if (moveList[0].moves[i].remoteness > maxRemote) {
maxRemote = moveList[0].moves[i].remoteness;
}
i += 1;
}
return maxRemote += 2;
};
/*
Draws the x-labels displaying how many moves until win for each
player.
*/
xlabel = function() {
var ctx, i, j, label;
ctx = c.getContext("2d");
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillStyle = textcolor;
label = maxRemote;
i = horCen;
j = horCen;
ctx.fillText("D", horCen, 10);
while (label >= 0) {
if (label % rowSpace === 0) {
ctx.fillText(label, i, xLabelPad);
ctx.fillText(label, j, xLabelPad);
}
}
i += colSpace;
j -= colSpace;
return label--;
};
/*
Draws the y-labels along the side of the grid, one
value at a time.
*/
ylabel = function(turn) {
var ctx, label;
ctx = c.getContext("2d");
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillStyle = textcolor;
label = turn + 1;
ctx.fillText(label, pady, yLabelPad + (turn * turnPadding));
return ctx.fillText(label, maxW - pady, yLabelPad + (turn * turnPadding));
};
/*
Draws the gridlines
*/
grid = function() {
var adjRemote, ctx, i, j, label, _results;
i = horCen;
j = horCen;
adjRemote = null;
if (maxRemote > 15) {
adjRemote = maxRemote / 10;
colSpace = (horCen - padx) / adjRemote;
} else {
adjRemote = maxRemote;
}
ctx = c.getContext("2d");
ctx.strokeStyle = linecolor;
ctx.lineWidth = 1;
label = adjRemote;
while (label >= 0) {
ctx.moveTo(i, disTop);
ctx.lineTo(i, maxH - pady);
ctx.stroke();
i += colSpace;
label -= 1;
}
label = adjRemote;
_results = [];
while (label >= 0) {
ctx.moveTo(j, disTop);
ctx.lineTo(j, maxH - pady);
ctx.stroke();
j -= colSpace;
_results.push(label -= 1);
}
return _results;
};
/*
Draws the gridlines as well as the x-labels across the top.
*/
drawGrid = function() {
colSpace = (horCen - padx) / maxRemote;
rowSpace = Math.floor(maxRemote / 4);
xlabel();
return grid();
};
/*
Draws individual dots based on the remoteness, the move value,
and the turn number.
*/
drawDot = function(remoteness, value, turn) {
var color, ctx, radius, turnRemote, xpos, ypos;
color = null;
xpos = null;
ypos = dotDefault + (turnPadding * turn);
turnRemote = remoteness;
radius = 5;
ctx = c.getContext("2d");
if (value === "win") {
color = winC;
} else if (value === "lose") {
color = loseC;
} else {
color = tieC;
}
if (value === "tie") {
xpos = horCen + ((maxRemote - turnRemote) * colSpace);
ctx.beginPath();
ctx.arc(xpos, ypos, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = color;
ctx.stroke();
xpos = horCen - ((maxRemote - turnRemote) * colSpace);
ctx.beginPath();
ctx.arc(xpos, ypos, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = color;
return ctx.stroke();
} else if (turn % 2 === 0) {
if (value === "lose") {
xpos = horCen + ((maxRemote - turnRemote) * colSpace);
} else {
xpos = horCen - ((maxRemote - turnRemote) * colSpace);
}
ctx.beginPath();
ctx.arc(xpos, ypos, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = color;
return ctx.stroke();
} else {
if (value === "lose") {
xpos = horCen - ((maxRemote - turnRemote) * colSpace);
} else {
xpos = horCen + ((maxRemote - turnRemote) * colSpace);
}
ctx.beginPath();
ctx.arc(xpos, ypos, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = color;
return ctx.stroke();
}
};
/*
Handles drawing all of the dots necessary for the given turn.
*/
drawDots = function(turn) {
if (turn !== 0) {
drawDot(prevBestMoveX, prevBestMoveVal, turn - 1);
}
console.log(tempBestMoveX, tempBestMoveVal);
drawDot(tempBestMoveX, tempBestMoveVal, turn);
return ylabel(turn);
};
/*
Slaps one line onto the grid.
*/
sketchLine = function(startX, endX, startY, endY, color) {
var ctx;
ctx = c.getContext("2d");
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
return ctx.stroke();
};
/*
Draws a colored line based using data from the previous move as well as
the most recent move
*/
drawLine = function(lineVal, turn) {
var color, firstPlayerEnd, firstPlayerStart, secondPlayerEnd, secondPlayerStart, xend, xstart, yend, ystart;
xstart = null;
xend = null;
ystart = linePadding + (prevBestMoveY * turnPadding);
yend = linePadding + (tempBestMoveY * turnPadding);
firstPlayerStart = horCen + ((maxRemote - prevBestMoveX) * colSpace);
secondPlayerStart = horCen - ((maxRemote - prevBestMoveX) * colSpace);
firstPlayerEnd = horCen - ((maxRemote - tempBestMoveX) * colSpace);
secondPlayerEnd = horCen + ((maxRemote - tempBestMoveX) * colSpace);
color = null;
if (lineVal === "win") {
color = winC;
} else if (lineVal === "lose") {
color = loseC;
} else {
color = tieC;
}
if (prevBestMoveVal === "tie") {
if (tempBestMoveVal === "tie") {
xstart = firstPlayerStart;
xend = secondPlayerEnd;
sketchLine(xstart, xend, ystart, yend, color);
xstart = secondPlayerStart;
xend = firstPlayerEnd;
return sketchLine(xstart, xend, ystart, yend, color);
} else if (turn % 2 === 0) {
xstart = firstPlayerStart;
xend = firstPlayerEnd;
sketchLine(xstart, xend, ystart, yend, color);
xstart = secondPlayerStart;
xend = firstPlayerEnd;
return sketchLine(xstart, xend, ystart, yend, color);
} else {
xstart = firstPlayerStart;
xend = secondPlayerEnd;
sketchLine(xstart, xend, ystart, yend, color);
xstart = secondPlayerStart;
xend = secondPlayerEnd;
return sketchLine(xstart, xend, ystart, yend, color);
}
} else if (tempBestMoveVal === "tie") {
if (turn % 2 === 0) {
xstart = firstPlayerStart;
xend = firstPlayerEnd;
sketchLine(xstart, xend, ystart, yend, color);
xstart = firstPlayerStart;
xend = secondPlayerEnd;
return sketchLine(xstart, xend, ystart, yend, color);
} else {
xstart = secondPlayerStart;
xend = secondPlayerEnd;
sketchLine(xstart, xend, ystart, yend, color);
xstart = secondPlayerStart;
xend = firstPlayerEnd;
return sketchLine(xstart, xend, ystart, yend, color);
}
} else if (prevBestMoveVal === "win") {
if (lineVal === "win" && turn % 2 === 0) {
xstart = firstPlayerStart;
xend = secondPlayerEnd;
} else if (lineVal === "win" && turn % 2 === 1) {
xstart = secondPlayerStart;
xend = firstPlayerEnd;
} else if (lineVal === "lose" && turn % 2 === 0) {
xstart = firstPlayerStart;
xend = firstPlayerEnd;
} else {
xstart = secondPlayerStart;
xend = secondPlayerEnd;
}
return sketchLine(xstart, xend, ystart, yend, color);
} else if (prevBestMoveVal === "lose") {
if (lineVal === "lose" && turn % 2 === 0) {
xstart = secondPlayerStart;
xend = firstPlayerEnd;
} else if (lineVal === "lose" && turn % 2 === 1) {
xstart = firstPlayerStart;
xend = secondPlayerEnd;
} else if (lineVal === "win" && turn % 2 === 0) {
xstart = secondPlayerStart;
xend = secondPlayerEnd;
} else {
xstart = firstPlayerStart;
xend = firstPlayerEnd;
}
return sketchLine(xstart, xend, ystart, yend, color);
}
};
/*
Updates the VVH after every turn.
*/
draw = function() {
var ctx, i, _results, _results1;
setTurnNum();
console.log(moveList[turnNum].board.value);
if (moveList[turnNum].board.remoteness !== 0) {
ctx = c.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0, 0, maxW, maxH);
if (moveList[turnNum].moves.length !== 0) {
maxVal();
drawGrid();
i = 0;
_results = [];
while (i < turnNum + 1) {
setLineColor(i);
setTempBestMove(i);
if (i !== 0) {
drawLine(lineColor, i);
}
drawDots(i);
prevBestMoveX = tempBestMoveX;
prevBestMoveY = tempBestMoveY;
prevBestMoveVal = tempBestMoveVal;
_results.push(i += 1);
}
return _results;
}
} else if (moveList[turnNum].board.remoteness === 0 && moveList[turnNum - 1].board.remoteness !== 0) {
ctx = c.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0, 0, maxW, maxH);
maxVal();
drawGrid();
i = 0;
_results1 = [];
while (i < turnNum + 1) {
setLineColor(i);
setTempBestMove(i);
if (i === turnNum) {
setFinalBestMove();
}
if (i !== 0) {
drawLine(lineColor, i);
}
drawDots(i);
prevBestMoveX = tempBestMoveX;
prevBestMoveY = tempBestMoveY;
prevBestMoveVal = tempBestMoveVal;
_results1.push(i += 1);
}
return _results1;
}
};
console.log(moveList);
draw();
return draw();
};