generated from projeto-de-algoritmos/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (73 loc) · 2.6 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
const chessBoard = document.querySelector('#chessBoard');
const restartButton = document.querySelector('#restartButton');
const tutorialButton = document.querySelector('#tutorialButton');
const size = 8;
const gridSize = size ** 2;
g = new Graph(size);
g.createAdjacencyList()
createBoard(size);
initializeKnightTour();
restartButton.addEventListener('click', () => {
restartChessBoard(size)
});
tutorialButton.addEventListener('click', () => {
changeTutorialPage(document.querySelector('img'))
});
function initializeKnightTour(){
const chessSquares = document.querySelectorAll(".chessSquares");
for(let i = 0; i < chessSquares.length; i++){
chessSquares[i].addEventListener('click', () => knight(i))
}
}
function createBoard(n){
let colorSquare = 1;
for(let i = 0; i < gridSize; i++){
let chessSquare = document.createElement('div');
chessSquare.classList.add('chessSquares');
if((i % n == 0) && i > 0){
colorSquare = colorSquare * (-1);
}
if(colorSquare == 1){
chessSquare.style.background = `rgb(227,193,111)`;
}else{
chessSquare.style.background = `rgb(184,139,74)`;
}
colorSquare = colorSquare * (-1);
chessBoard.appendChild(chessSquare);
};
}
function changeTutorialPage(img) {
if (img.getAttribute('src') == 'imgs/tutorial1.svg'){
img.src = "imgs/tutorial2.svg"
}
else {
img.src = "imgs/tutorial1.svg"
}
}
function restartChessBoard(n){
g = new Graph(size)
g.createAdjacencyList()
chessBoard.setAttribute('style', `pointer-events: all`)
const chessSquares = document.querySelectorAll(".chessSquares");
for(let i = 0; i < chessSquares.length; i++){
chessSquares[i].innerHTML = '';
}
}
async function knight(initialNode){
g.dfsKnight(initialNode);
const chessSquares = document.querySelectorAll(".chessSquares");
const chessKnight = document.createElement('img');
chessKnight.setAttribute('src', 'imgs/Chess-Knight.svg');
chessKnight.classList.add('chessKnight');
chessBoard.setAttribute('style', `pointer-events: none`)
for(let i = 0; i < g.path.length; i++){
chessSquares[g.path[i]].appendChild(chessKnight);
await new Promise(resolve => setTimeout(resolve, 500));
if (i != (g.path.length - 1)){
let visitedSquare = document.createElement('div');
visitedSquare.innerHTML = 'X';
visitedSquare.classList.add('visitedSquares');
chessSquares[g.path[i]].appendChild(visitedSquare);
}
}
}