Victory actions done

This commit is contained in:
2021-12-27 20:25:33 -03:00
parent 2a866751f9
commit 658c5b2419
8 changed files with 236 additions and 12 deletions

View File

@@ -1,13 +1,18 @@
export default function checkVictory(combinations, boxes) {
const boxesSorted = boxes.sort()
const result = []
combinations.forEach(combination => {
if (boxesSorted[0] === combination[0] && boxesSorted[1] === combination[1] && boxesSorted[2] === combination[2]) {
if (boxes.includes(combination[0]) && boxes.includes(combination[1]) && boxes.includes(combination[2])) {
alert('Win')
console.log('WIN')
result.push('WIN')
result.push(combination)
}
});
return result
}

View File

@@ -1,5 +1,6 @@
import accessToCombinations from "./accessToCombinations.js"
import checkVictory from "./checkVictory.js"
import victoryActions from "./victoryActions.js"
export default function gameLogic(checkInputsResult) {
@@ -38,7 +39,14 @@ export default function gameLogic(checkInputsResult) {
actualPlayer.markedBoxes.push(box.id)
checkVictory(combinations, actualPlayer.markedBoxes)
const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes)
console.log(checkVictoryResult)
if (checkVictoryResult[0] === 'WIN') {
victoryActions(checkVictoryResult[1], actualPlayer.name)
}
if (players.indexOf(actualPlayer) === 0) {

View File

@@ -0,0 +1,48 @@
export default function victoryActions(combination, playerName) {
const stylesActions = (() => {
let victoryMessage = document.getElementsByClassName('victory-message')[0]
victoryMessage.classList.remove('hidden')
victoryMessage.classList.add('animate__fadeInDown')
let victoryBackground = document.getElementsByClassName('background-winner-div')[0]
victoryBackground.classList.add('show')
let victoryName = document.getElementById('victory-name')
victoryName.innerText = playerName
let game = document.getElementsByClassName('tic-tac-toe')[0]
game.classList.add('completed')
combination.forEach(element => {
const box = document.getElementById(element)
box.classList.add('winner-box')
setTimeout(() => {
box.classList.add('black-letters')
}, 350);
});
})()
const button = document.getElementById('play-again-button')
button.addEventListener('click', (e) => {
e.preventDefault()
const game = document.getElementsByClassName('game')[0]
game.classList.add('animate__fadeOutDown')
setTimeout(() => {
game.classList.add('hidden')
}, 1000)
})
}