-
Notifications
You must be signed in to change notification settings - Fork 19
/
user-interface.js
105 lines (86 loc) · 3.27 KB
/
user-interface.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
var UserInterface = (function () {
Snake.init();
Snake.setup.keyboard(false);
Snake.setup.wall(false);
Snake.setup.fixedTail(false);
const defaultFPS = 8;
QLearning.run();
QLearning.changeSpeed(4);
QLearning.changeFPS(defaultFPS);
/// EASTER EGG ///
document.addEventListener('keyup', KonamiCode);
var konamiOrder = [38,38,40,40,37,39,37,39,66,65];
var konamiIndex = 0;
function KonamiCode (evt) {
if(konamiOrder[konamiIndex] == evt.keyCode){
konamiIndex++
}
else {
konamiIndex = 0;
}
if (konamiIndex == konamiOrder.length) {
document.removeEventListener('keyup', KonamiCode);
QLearning.stop();
Snake.setup.keyboard(true);
Snake.setup.tileCount(20);
Snake.setup.fixedTail(false);
Snake.reset();
Snake.start();
}
}
///
var btnTrain = document.getElementById('btnTrain');
var btnReset = document.getElementById('btnReset');
btnTrain.addEventListener('click', speedFaster);
btnReset.addEventListener('click', resetSnake);
var infoScore = document.getElementById('score');
var infoMissed = document.getElementById('missed');
var rangerLR = document.getElementById('rangeLR');
var rangerDF = document.getElementById('rangeDF');
var rangerRandom = document.getElementById('rangeRandom');
var checkboxFullSet = document.getElementById('fullSet');
setInterval(loop, 100);
function loop () {
infoScore.innerHTML = 'scored: <b>' + QLearning.info.score() + (QLearning.info.score() == 1 ? ' point' : ' points') + '</b>';
infoMissed.innerHTML = 'died: <b>' + Math.abs(QLearning.info.missed()) + (Math.abs(QLearning.info.missed()) == 1 ? ' time' : ' times') + '</b>';
QLearning.changeConst.LearningRate(0.01*rangerLR.value);
QLearning.changeConst.DiscountFactor(0.01*rangerDF.value);
QLearning.changeConst.Randomization(0.01*rangerRandom.value);
QLearning.changeConst.FullSetOfStates(checkboxFullSet.checked);
}
async function resetSnake () {
// await speedSlower();
// Snake.reset();
QLearning.stop();
QLearning.reset();
Snake.reset();
Snake.clearTopScore();
await speedSlower();
}
async function speedFaster () {
modifyingThreads = true;
rangerLR.disabled = true;
rangerDF.disabled = true;
rangerRandom.disabled = true;
checkboxFullSet.disabled = true;
QLearning.stop();
QLearning.startTrain();
btnTrain.innerHTML = '| | TRAIN';
btnTrain.removeEventListener('click', speedFaster);
btnTrain.addEventListener('click', speedSlower);
modifyingThreads = false;
}
async function speedSlower () {
modifyingThreads = true;
QLearning.stopTrain();
QLearning.changeFPS(defaultFPS);
rangerLR.disabled = false;
rangerDF.disabled = false;
rangerRandom.disabled = false;
checkboxFullSet.disabled = false;
btnTrain.innerHTML = '▶ TRAIN';
btnTrain.removeEventListener('click', speedSlower);
btnTrain.addEventListener('click', speedFaster);
modifyingThreads = false;
}
})();