-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (75 loc) · 2.72 KB
/
app.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
"use strict";
document.addEventListener('DOMContentLoaded', (e) => {
const resultDiv = document.querySelector('.summary-result');
const flaggedCounterDiv = document.querySelector('.flaggedCounter');
const timerDiv = document.querySelector('.timer');
const boardContainer = document.querySelector('.board');
const btnNewGame = document.getElementById('btnNewGame');
const lnkBeginner = document.getElementById('lnkBeginner');
const lnkIntermediate = document.getElementById('lnkIntermediate');
const lnkExpert = document.getElementById('lnkExpert');
let timer;
const minesPercentage = 15;
const emojiStart = '😀';
const emojiLost = '😩';
const emojiWon = '😎';
lnkBeginner.addEventListener('click', () => {
newBeginnerGame();
}, true);
lnkIntermediate.addEventListener('click', () => {
newIntermediateGame();
}, true);
lnkExpert.addEventListener('click', () => {
newExpertGame();
}, true);
btnNewGame.addEventListener('click', () => {
newCustomGame();
}, true);
const newCustomGame = () => {
const rows = +document.getElementById("numRows").value || 10;
const cols = +document.getElementById("numCols").value || 10;
newGame(rows, cols, minesPercentage);
};
const newBeginnerGame = () => {
newGame(8, 8, minesPercentage);
};
const newIntermediateGame = () => {
newGame(16, 16, minesPercentage);
};
const newExpertGame = () => {
newGame(16, 30, minesPercentage);
};
const newGame = (rows, cols, minesPercentage) => {
btnNewGame.innerHTML = emojiStart;
clearInterval(timer);
//resultDiv.innerHTML = '';
timerDiv.innerHTML = '0';
document.getElementById("numRows").value = rows;
document.getElementById("numCols").value = cols;
const config = new Config(rows, cols, minesPercentage);
const board = new Board(config);
const boardRenderer = new BoardRenderer(board, boardContainer);
const game = new Game(config, board, boardRenderer);
game.addEventListener('start', (e) => {
let second = 1;
timer = setInterval(() => {
timerDiv.innerHTML = `${second++}s`;
}, 1000);
}, false);
game.addEventListener('change', (e) => {
flaggedCounterDiv.innerHTML = e.detail.flaggedMinesCount;
}, false);
game.addEventListener('end', (e) => {
clearInterval(timer);
const result = e.detail.result;
if (result === GameResult.NONE) {
throw 'Something went wrong. The result cannot be "NONE" at the end of the game';
}
const emoji = (result === GameResult.LOST ? emojiLost : emojiWon);
btnNewGame.innerHTML = emoji;
}, false);
flaggedCounterDiv.innerHTML = config.minesNumber;
game.new();
};
newBeginnerGame();
});