-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (94 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
let actualSide = 0;
const circleImage = "./assets/available.png";
const cancelImage = "./assets/cancel.png";
let plays = [];
let alreadyClicked = [];
let matriceGame = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
const allEqual = arr => arr.every(val => val === arr[0]);
function clickThebox(event){
if(!alreadyClicked.includes(event.id)){
alreadyClicked.push(event.id)
if(actualSide % 2 == 0){
const circleComponent = document.createElement("img");
circleComponent.src = circleImage;
document.getElementById(event.id).appendChild(circleComponent);
plays.push({id:event.id, player: "circle"});
let matriceCoordinate = handleCoordinates(event.id);
setValueToCoordinate("O", matriceCoordinate);
}else{
const cancelComponent = document.createElement("img");
document.getElementById(event.id).appendChild(cancelComponent);
cancelComponent.src = cancelImage;
plays.push({id:event.id, player: "cancel"});
let matriceCoordinate = handleCoordinates(event.id);
setValueToCoordinate("X", matriceCoordinate);
}
verifyWinner();
console.table(matriceGame);
actualSide += 1;
}
}
function verifyWinner(){
for(indexLine = 0; indexLine <= 2; indexLine++){
if(matriceGame[indexLine][0] !== 0 && matriceGame[indexLine][1] !==0 && matriceGame[indexLine][2] !==0){
if(matriceGame[indexLine][0] === matriceGame[indexLine][1] && matriceGame[indexLine][1] === matriceGame[indexLine][2]){
callWinner(matriceGame[indexLine][0]);
}
}
}
for(indexColumn = 0; indexColumn <= 2; indexColumn++){
if(matriceGame[0][indexColumn] !== 0 && matriceGame[1][indexColumn] !==0 && matriceGame[2][indexColumn] !==0){
if(matriceGame[0][indexColumn] === matriceGame[1][indexColumn] && matriceGame[1][indexColumn] === matriceGame[2][indexColumn]){
callWinner(matriceGame[0][indexColumn]);
}
}
}
if(matriceGame[0][0] !== 0 && matriceGame[1][1] !==0 && matriceGame[2][2] !==0){
if(matriceGame[0][0] === matriceGame[1][1] && matriceGame[2][2] === matriceGame[2][2]){
callWinner(matriceGame[0][0]);
}
}
if(matriceGame[2][0] !== 0 && matriceGame[1][1] !==0 && matriceGame[0][2] !==0){
if(matriceGame[2][0] === matriceGame[1][1] && matriceGame[2][0] === matriceGame[0][2]){
callWinner(matriceGame[2][0]);
}
}
}
function resetRound(){
actualSide = 0;
alreadyClicked = [];
for(indexLine = 0; indexLine <= 2; indexLine++){
for(indexColumn = 0; indexColumn <= 2; indexColumn++){
let parentElement = document.getElementById(`${indexLine}-${indexColumn}`);
let childElement = parentElement.querySelector("img");
if (childElement) {
parentElement.removeChild(childElement);
}
}
}
matriceGame = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
}
function handleCoordinates(eventId){
let matriceCoordinateString = eventId.split("-");
let matriceCoordinate = matriceCoordinateString.map(str => {
return parseInt(str, 10);
});
return matriceCoordinate;
}
function setValueToCoordinate(value, matriceCoordinate){
matriceGame[ matriceCoordinate[0] ][ matriceCoordinate[1] ] = value;
}
function callWinner(winner){
alert(`we have a winner, congratulations ${winner}`);
resetRound();
}
function plusScore(){
}