-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (74 loc) · 2.58 KB
/
script.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
const table = document.querySelectorAll('.square');
const history = [];
const historyList = document.getElementById('history');
let moveNumber = 0;
let currentPlayer = 'cross';
let currentPlayerDisplay = document.getElementById('current-player');
table.forEach((square, i) => {
square.addEventListener('click', () => {
if (table[i].innerHTML === '') {
addMove(i);
addHistory(i);
verifyVictory();
alterPlayer();
}
});
});
// Adiona um movimento na tabela e incrementa o número de movimentos
function addMove(index) {
const move = document.createElement('div');
move.classList.add(currentPlayer);
table[index].appendChild(move);
moveNumber++;
}
// Alterna o jogador atual entre 'cross' e 'circle' e atualiza o display
function alterPlayer() {
currentPlayer = currentPlayer === 'cross' ? 'circle' : 'cross';
currentPlayerDisplay.innerHTML = currentPlayer === 'cross' ? 'X' : 'O';
currentPlayerDisplay.style.color = currentPlayer === 'cross' ? '#0000ff' : '#ff0000';
}
// Verifica se houve vitória de um dos jogadores e exibe um alerta com o vencedor,
// caso não haja vitória, remove o último movimento após 7 movimentos
function verifyVictory() {
const winningCombos = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
let victoryFound = false;
winningCombos.forEach(combo => {
const [a, b, c] = combo;
if (table[a].innerHTML && table[a].innerHTML === table[b].innerHTML && table[a].innerHTML === table[c].innerHTML) {
[a, b, c].forEach(i => table[i].style.backgroundColor = '#4a6d42');
let winningPlayer = currentPlayer;
setTimeout(() => {
alert(`Player ${winningPlayer} wins!`);
resetGame();
}, 100);
victoryFound = true;
}
});
if (!victoryFound && moveNumber >= 7) {
table[history[moveNumber - 7].position].innerHTML = '';
}
}
// Adiciona um item ao histórico de jogadas e exibe na tela o movimento realizado
function addHistory(position) {
history.push({ moveNumber, player: currentPlayer, position });
const historyItem = document.createElement('li');
historyItem.innerHTML = `Move ${moveNumber}: ${currentPlayer === 'cross' ? 'X' : 'O'} at position ${position}`;
historyList.appendChild(historyItem);
}
// Reinicia o jogo
function resetGame() {
table.forEach(square => {
square.innerHTML = '';
square.style.backgroundColor = '';
});
history.length = 0;
moveNumber = 0;
currentPlayer = 'cross';
currentPlayerDisplay.innerHTML = 'X';
currentPlayerDisplay.style.color = '#0000ff';
historyList.innerHTML = '';
}