When i console.log the winConditions array it shows all zeroes when its supposed to have some 1's in it aswell.
I'm changing the value of a, b and c at the click event listener but idk why they are not changing
Someone please help me
let a = 0,
b = 0,
c = 0,
d = 0,
e = 0,
f = 0,
g = 0,
h = 0,
i = 0;
const test = document.querySelector(".gameboard");
test.addEventListener("click", () => {
a = 1;
b = 1;
c = 1;
gameBoard.applyWinConditions;
console.log(gameBoard.winConditions)
gameBoard.gameFlow.checkScore();
});
const gameBoard = (() => {
let gameBoard = {};
let applyWinConditions = () => {
gameBoard.winConditions = [[a, b, c], [a, d, g], [a, e, i], [b ,e, h], [c, e, g], [c, f, i], [d, e, f], [g, h, i]];
return gameBoard.winConditions;
}
const gameFlow = (function() {
const _isEveryElementOne = (arr) => {
return arr.every(el => el === 1)
}
const _announceWinner = () => console.log("game ends");
return {
checkScore: () => {
console.log(gameBoard.winConditions);
if (gameBoard.winConditions.some(cond => _isEveryElementOne(cond))) return _announceWinner()
console.log("game continues")
},
};
})();
return {
gameFlow,
applyWinConditions: applyWinConditions(),
winConditions: gameBoard.winConditions,
}
})();
gameBoard.applyWinConditions;
console.log(gameBoard.applyWinConditions)
<button class="gameboard">Click me!</button>
I refactored your code as a class because I had trouble reading it. Note: I did not code all your methods. (e.g. checkscore and gameflow). This seems to work:
class Gameboard{
winConditions = [];
announceWinner = "Game Over";
constructor(){
this.winConditions = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]];
}
applyWinConditions(m){
this.winConditions = [
[m[0],m[1],m[2]],
[m[0],m[4],m[5]],
[m[0],m[4],m[8]],
[m[2],m[4],m[6]],
[m[2],m[4],m[5]],
[m[2],m[5],m[7]],
[m[3],m[3],m[4]],
[m[6],m[7],m[8]]
];
alert(this.winConditions);
}
getWinConditions(){
return this.WinConditions;
}
gameflow(){
}
checkScore(){
}
}// class
const gameBoard = new Gameboard();
const test = document.querySelector(".gameboard");
test.addEventListener("click", () => {
gameBoard.applyWinConditions([1,1,1,0,0,0,0,0,0]);
});