-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (57 loc) · 1.5 KB
/
Copy pathscript.js
File metadata and controls
65 lines (57 loc) · 1.5 KB
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
let tiles=document.querySelectorAll(".tile");
let resetButton = document.querySelector("button");
let currentTurn = "red";
let winningCombos = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6],
];
for (let i=0; i<tiles.length; i++){
tiles[i].addEventListener("click", setTile)
}
resetButton.addEventListener("click", resetGame)
function setTile(e){
let classes = e.target.classList;
let tileAlreadySelected = classes.contains("red") || classes.contains("blue");
if(tileAlreadySelected) {
console.log("Pick another tile please")
} else {
classes.add(currentTurn);
checkForWinner()
changeTurn()
}
}
function changeTurn(){
if(currentTurn =="red"){
currentTurn = "blue"
} else{
currentTurn = "red";
}
}
function resetGame(){
for(let i=0; i<tiles.length; i++){
tiles[i].classList.remove("red");
tiles[i].classList.remove("blue")
}
}
function checkForWinner(){
for(let i=0; i<winningCombos.length; i++){
let combination = winningCombos[i];
foundWinner = true;
for(let j=0; j<combination.length; j++){
let tileIndex = combination[j];
let hasTile = tiles[tileIndex].classList.contains(currentTurn);
if(hasTile!==true){
foundWinner = false
}
}
if(foundWinner==true){
console.log(`Player ${currentTurn} wins!`);
}
}
}