mirror of
https://github.com/FranP-code/Tic-Tac-Toe-Game.git
synced 2025-10-12 23:52:39 +00:00
Win game logic done
This commit is contained in:
52
script/game-functions/gameLogic.js
Normal file
52
script/game-functions/gameLogic.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import accessToCombinations from "./accessToCombinations.js"
|
||||
import checkVictory from "./checkVictory.js"
|
||||
|
||||
export default function gameLogic(checkInputsResult) {
|
||||
|
||||
function Player(position) {
|
||||
|
||||
this.name = checkInputsResult[position][0].value
|
||||
this.symbol = checkInputsResult[position][1].value
|
||||
this.markedBoxes = []
|
||||
}
|
||||
|
||||
const player1 = new Player(0)
|
||||
const player2 = new Player(1)
|
||||
const players = [player1, player2]
|
||||
|
||||
const combinations = accessToCombinations()
|
||||
|
||||
let actualPlayer = players[0]
|
||||
let boxesMarked = []
|
||||
|
||||
const boxes = [...document.getElementsByClassName('box')]
|
||||
|
||||
boxes.forEach(box => {
|
||||
|
||||
box.addEventListener('click', () => {
|
||||
|
||||
if (boxesMarked.includes(box.id)) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
box.innerText = actualPlayer.symbol
|
||||
boxesMarked.push(box.id)
|
||||
|
||||
actualPlayer.markedBoxes.push(box.id)
|
||||
|
||||
checkVictory(combinations, actualPlayer.markedBoxes)
|
||||
|
||||
if (players.indexOf(actualPlayer) === 0) {
|
||||
|
||||
actualPlayer = players[1]
|
||||
|
||||
} else {
|
||||
|
||||
actualPlayer = players[0]
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user