Local storage for play again done

This commit is contained in:
2021-12-27 22:37:43 -03:00
parent 658c5b2419
commit 69cc467810
3 changed files with 51 additions and 10 deletions

View File

@@ -2,12 +2,23 @@ import accessToCombinations from "./accessToCombinations.js"
import checkVictory from "./checkVictory.js"
import victoryActions from "./victoryActions.js"
export default function gameLogic(checkInputsResult) {
export default function gameLogic(checkInputsResult, replay) {
function Player(position) {
this.name = checkInputsResult[position][0].value
this.symbol = checkInputsResult[position][1].value
console.log(replay)
if (replay) {
this.name = checkInputsResult[position].name
this.symbol = checkInputsResult[position].symbol
} else {
this.name = checkInputsResult[position][0].value
this.symbol = checkInputsResult[position][1].value
}
this.markedBoxes = []
}
@@ -42,10 +53,15 @@ export default function gameLogic(checkInputsResult) {
const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes)
console.log(checkVictoryResult)
const data = {
player1: player1,
player2: player2,
}
if (checkVictoryResult[0] === 'WIN') {
victoryActions(checkVictoryResult[1], actualPlayer.name)
victoryActions(checkVictoryResult[1], actualPlayer.name, data)
}
if (players.indexOf(actualPlayer) === 0) {

View File

@@ -1,4 +1,4 @@
export default function victoryActions(combination, playerName) {
export default function victoryActions(combination, playerName, data) {
const stylesActions = (() => {
@@ -39,10 +39,16 @@ export default function victoryActions(combination, playerName) {
game.classList.add('animate__fadeOutDown')
console.log(data)
setTimeout(() => {
game.classList.add('hidden')
localStorage.setItem('data', JSON.stringify(data))
location.reload();
}, 1000)
})
}

View File

@@ -17,11 +17,30 @@ function main(game) {
disableEnterInputs(game)
addStartButtonFunctionality(game)
const data = localStorage.getItem('data')
if (data) {
const checkInputsResult = Object.values(JSON.parse(data))
localStorage.removeItem('data')
game(checkInputsResult, true)
}
}
function game() {
function game(checkInputsResult, replay) {
const checkInputsResult = checkInputs()
if (!checkInputsResult) {
checkInputsResult = checkInputs()
}
if (!replay) {
replay = false
}
console.log(checkInputsResult)
@@ -33,7 +52,7 @@ function game() {
hidePlayerSelection()
showTicTacToe()
gameLogic(checkInputsResult)
gameLogic(checkInputsResult, replay)
}
window.onload = () => main(game)