mirror of
https://github.com/FranP-code/Tic-Tac-Toe-Game.git
synced 2025-10-12 23:52:39 +00:00
Tie logic added
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
export default function checkVictory(combinations, boxes) {
|
||||
export default function checkVictory(combinations, boxes, globalBoxesMarked) {
|
||||
|
||||
const boxesSorted = boxes.sort()
|
||||
|
||||
const result = []
|
||||
|
||||
combinations.forEach(combination => {
|
||||
@@ -11,8 +9,16 @@ export default function checkVictory(combinations, boxes) {
|
||||
console.log('WIN')
|
||||
result.push('WIN')
|
||||
result.push(combination)
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
console.log(globalBoxesMarked.length)
|
||||
|
||||
if (globalBoxesMarked.length === 9 && result.length === 0) {
|
||||
|
||||
result.push('TIE')
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import accessToCombinations from "./accessToCombinations.js"
|
||||
import checkVictory from "./checkVictory.js"
|
||||
import tieActions from "./tieActions.js"
|
||||
import victoryActions from "./victoryActions.js"
|
||||
|
||||
export default function gameLogic(checkInputsResult, replay) {
|
||||
@@ -50,8 +51,7 @@ export default function gameLogic(checkInputsResult, replay) {
|
||||
|
||||
actualPlayer.markedBoxes.push(box.id)
|
||||
|
||||
const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes)
|
||||
console.log(checkVictoryResult)
|
||||
const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes, boxesMarked)
|
||||
|
||||
const data = {
|
||||
|
||||
@@ -64,6 +64,11 @@ export default function gameLogic(checkInputsResult, replay) {
|
||||
victoryActions(checkVictoryResult[1], actualPlayer.name, data)
|
||||
}
|
||||
|
||||
if (checkVictoryResult[0] === 'TIE') {
|
||||
|
||||
tieActions(data)
|
||||
}
|
||||
|
||||
if (players.indexOf(actualPlayer) === 0) {
|
||||
|
||||
actualPlayer = players[1]
|
||||
|
||||
41
script/game-functions/tieActions.js
Normal file
41
script/game-functions/tieActions.js
Normal file
@@ -0,0 +1,41 @@
|
||||
export default function tieActions(data) {
|
||||
|
||||
const stylesActions = (() => {
|
||||
|
||||
let victoryMessage = document.getElementsByClassName('victory-message')[0]
|
||||
victoryMessage.classList.remove('hidden')
|
||||
victoryMessage.classList.add('animate__fadeInDown')
|
||||
|
||||
let victoryMessageText = victoryMessage.children[0]
|
||||
victoryMessageText.innerText = 'Tie!'
|
||||
|
||||
let victoryBackground = document.getElementsByClassName('background-winner-div')[0]
|
||||
victoryBackground.classList.add('show')
|
||||
|
||||
let game = document.getElementsByClassName('tic-tac-toe')[0]
|
||||
game.classList.add('completed')
|
||||
})()
|
||||
|
||||
const button = document.getElementById('play-again-button')
|
||||
|
||||
button.addEventListener('click', (e) => {
|
||||
|
||||
e.preventDefault()
|
||||
|
||||
const game = document.getElementsByClassName('game')[0]
|
||||
|
||||
game.classList.add('animate__fadeOutDown')
|
||||
|
||||
console.log(data)
|
||||
|
||||
setTimeout(() => {
|
||||
|
||||
game.classList.add('hidden')
|
||||
|
||||
localStorage.setItem('data', JSON.stringify(data))
|
||||
|
||||
location.reload();
|
||||
|
||||
}, 1000)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user