forked from michaelrushton-dev/darts-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (91 loc) · 3.11 KB
/
index.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
let count = 501;
let turnsArray = [];
let normalFields = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
];
let specialFields = [
{ id: 'bullseye', points: 50 },
{ id: 'sngl_25', points: 25 },
{ id: 'missed-button', points: 0 },
{ id: 'outer_circle', points: 0 },
];
//Grabs all the normal fields (sngl, dbl, trpl points) and adds handleclicks to them:
normalFields.map((id) => {
document
.getElementById(`sngl_${id}`)
.addEventListener('click', (e) => handleClick(id, e));
document
.getElementById(`dbl_${id}`)
.addEventListener('click', (e) => handleClick(id * 2, e));
document
.getElementById(`trpl_${id}`)
.addEventListener('click', (e) => handleClick(id * 3, e));
});
//Grabs special fields and adds handleclikcs to them:
specialFields.map((item) => {
document
.getElementById(item.id)
.addEventListener('click', (e) => handleClick(item.points, e));
});
let turnsList = document.getElementById('turns-list');
//grabs the counter and puts the initial count in it:
const counter = document.getElementById('counter');
counter.innerText = String(count);
//The spacebar shortcut for missed throws:
document.body.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
handleClick(0);
}
});
/* ------FUNCTION DECLARATIONS--------*/
function clearTurns() {
//Clears the turns list that's displayed to the user.
for (let i = 0; i < turnsArray.length; i++) {
turnsList.removeChild(turnsList.firstChild);
}
//Clears the turns counter by reducing the length of turnsArray to 0.
turnsArray = [];
}
function handleClick(num, e) {
count -= num;
counter.innerText = String(count);
let turnsListItem = document.createElement('li');
turnsListItem.innerText = String(num);
turnsList.appendChild(turnsListItem);
//Adds current throw to the turnsArray. If the turnsArray is already full, clears it first before adding the throw.
if (turnsArray.length < 3) {
turnsArray.push(num);
} else {
clearTurns();
turnsArray = [num];
}
endgame(e);
}
//The endgame logic. Controls what happens when you try to checkout.
function endgame(e) {
if (
count === 0 &&
(e.target.id.includes('dbl') || e.target.id === 'bullseye')
) {
counter.innerText = 'Checked out! :D';
} else if (count < 0 || count === 1) {
updateScoreBoard("You're bust ");
//creating a span for the frown emoji so it doesn't wrap (treats it as a word)
const span = document.createElement('span');
counter.appendChild(span);
span.classList.add('nowrap');
span.textContent = ':(';
} else if (count === 0 && !e.target.id.includes('dbl')) {
updateScoreBoard('U need dbl or bullseye');
}
}
function updateScoreBoard(message) {
//Adds the previous turns BACK into the turnsArray:
count = count + turnsArray.reduce((prev, cur) => prev + cur, 0);
clearTurns();
counter.innerText = message;
setTimeout(() => {
counter.innerText = count;
}, '1000');
}
export { handleClick };