-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.js
73 lines (62 loc) · 1.8 KB
/
Generator.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
function generateScore(score) {
$('.scoreval').text(`${score}`);
}
function changeScore(addScore) {
let actualScore = parseInt($('.scoreval').text());
let finalScore = actualScore + addScore;
$('.scoreval').text(finalScore);
}
function generateMap(grilleSize) {
for (let index = 0; index < grilleSize; index++) {
$('.grille').append(`<div class='column' id="${index}col" ></div>`);
}
for (let index = 0; index < grilleSize; index++) {
$('.column').append(`<div class='cells' id="${index}row" ></div>`);
}
var cellSize = 100 / grilleSize;
$('.column').css('width', cellSize + '%');
$('.cells').css('height', cellSize + '%');
}
function generateCell() {
let emptyCells = $('.cells:empty');
let emptyCellsLength = emptyCells.length;
let randomNumber = Math.floor(Math.random() * Math.floor(emptyCellsLength));
let selectedCell = emptyCells[randomNumber];
let random2or4 = generate2or4();
$(selectedCell).text(`${random2or4}`);
}
function generate2or4() {
let value = 2;
var randomNumber = Math.floor(Math.random() * Math.floor(10));
if (randomNumber === 1) {
value = 4;
}
return value;
}
function retry() {
$('.grille').css('background', 'lightblue');
let allCells = allCellsSelector();
allCells.text('');
generateCell();
}
function setBestScore(bestscore) {
localStorage.setItem('bestscore', bestscore);
}
function getBestScore() {
return localStorage.getItem('bestscore');
}
function displayBestScore() {
let bestScore = calculateBestScore();
$('.bestscoreval').text(`${bestScore}`);
}
function calculateBestScore() {
let bestScore = getBestScore();
let actualScore = parseInt($('.scoreval').text());
if (bestScore >= actualScore) {
return bestScore;
} else {
setBestScore(actualScore);
bestScore = getBestScore();
return bestScore;
}
}