-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (91 loc) · 1.7 KB
/
script.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
rollSequence = [];
curIndex = 0;
lastRoll = 0;
function loadFairDice()
{
dice1 = [1,2,3,4,5,6];
dice2 = [1,2,3,4,5,6];
generateRollPossiblities();
performPleasingShuffle();
updateRollDisplay();
}
function generateRollPossiblities()
{
for(i = 0; i < dice1.length; i++)
{
for(j = 0; j < dice2.length; j++)
{
rollSequence.push(dice1[i] + dice2[j]);
}
}
}
function performPleasingShuffle()
{
performFisherYatesShuffle();
avoidDuplicatesInSequence(2);
}
function performFisherYatesShuffle()
{
for(i = 0; i < rollSequence.length; i++)
{
swapElementWithRandom(rollSequence, i);
}
}
function avoidDuplicatesInSequence(tolerance)
{
canSwapCount = 0;
for(i = 0; (i+1) < rollSequence.length; i++)
{
if(rollSequence[i] == rollSequence[i+1])
{
canSwapCount++;
if(canSwapCount > tolerance)
{
swapElementWithRandom(rollSequence, i);
}
}
}
}
function swapElementWithRandom(array, i)
{
j = Math.floor(Math.random() * (i + 1));
swapElements(array, i, j);
}
function swapElements(array, i ,j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function updateRollDisplay()
{
if(curIndex < rollSequence.length)
{
if(lastRoll == 0)
{
$('#last-roll').text("The First Roll:");
}
else
{
$('#last-roll').text("Last Roll Was: " + lastRoll);
}
lastRoll = rollSequence[curIndex];
$('#current-roll').text(rollSequence[curIndex]);
curIndex++;
}
else
{
performPleasingShuffle();
outputSequence();
curIndex = 0;
updateRollDisplay();
}
}
$( document ).ready(function()
{
loadFairDice();
$( document ).click(function()
{
updateRollDisplay();
});
});