Win game logic done

This commit is contained in:
2021-12-27 16:28:27 -03:00
parent 0102f9b106
commit bc65015c21
6 changed files with 148 additions and 14 deletions

View File

@@ -0,0 +1,60 @@
export default function accessToCombinations() {
function Combination(indexes) {
indexes = indexes.map(index => `box-${index}`);
return indexes
}
function HorizontalCombination(startIndex) {
const x = startIndex
const indexes = [x, x + 1, x + 2]
return Combination(indexes)
}
function VerticalCombination(startIndex) {
const x = startIndex
const indexes = [x, x + 3, x + 6]
return Combination(indexes)
}
function DiagonalCombination(startIndex) {
const x = startIndex
let indexes
if (x === 1) {
indexes = [x, 5, 9]
}
if (x === 3) {
indexes = [x, 5, 7]
}
return Combination(indexes)
}
const combinations = [
HorizontalCombination(1),
HorizontalCombination(4),
HorizontalCombination(7),
VerticalCombination(1),
VerticalCombination(2),
VerticalCombination(3),
DiagonalCombination(1),
DiagonalCombination(3)
]
return combinations
}

View File

@@ -44,4 +44,6 @@ export default function checkInputs() {
return true
}
return [inputsPlayer1, inputsPlayer2]
}

View File

@@ -0,0 +1,13 @@
export default function checkVictory(combinations, boxes) {
combinations.forEach(combination => {
const boxesSorted = boxes.sort()
if (boxesSorted[0] === combination[0], boxesSorted[1] === combination[1], boxesSorted[2] === combination[2]) {
alert('Win')
}
});
}

View 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]
}
})
})
}

View File

@@ -6,6 +6,7 @@ import checkInputs from './game-functions/checkInputs.js'
import disableEnterInputs from './emoji-picker-functions/disableEnterInputs.js'
import hidePlayerSelection from './game-functions/hidePlayerSelection.js'
import showTicTacToe from './game-functions/showTicTacToe.js'
import gameLogic from './game-functions/gameLogic.js'
function main(game) {
@@ -20,13 +21,19 @@ function main(game) {
function game() {
if(checkInputs()) {
const checkInputsResult = checkInputs()
console.log(checkInputsResult)
if(checkInputsResult && typeof(checkInputsResult) === 'boolean') {
return
}
hidePlayerSelection()
showTicTacToe()
gameLogic(checkInputsResult)
}
window.onload = () => main(game)