-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
114 lines (103 loc) · 2.58 KB
/
main.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
113
114
//DECLARE VARIABLES
let turn;
let grid;
const winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [2, 4, 6], [0, 4, 8]];
let winner = null;
const playerTurn = document.getElementById("player-turn")
const restartBtn = document.getElementById("restartBtn")
//FUNCTIONS
//Declare init
function init() {
turn = "O"
grid = [null, null, null, null, null, null, null, null, null]
winner = null;
displayTurn()
}
// Alternate players
function switchPlayer() {
if (turn === "O") {
turn = "X"
} else {
turn = "O"
}
}
// Victory conditions
function victory() {
winningCombinations.forEach(function(oneCombination){
let elA = grid[oneCombination[0]];
let elB = grid[oneCombination[1]];
let elC = grid[oneCombination[2]];
console.log(elA, elB, elC, oneCombination)
if (elA === "O" && elB === "O" && elC === "O") {
winner = "O";
}
if (elA === "X" && elB === "X" && elC === "X") {
winner = "X";
}
})
}
// Create a function for evt. Include preventDefault()
function playerClick(evt) {
evt.preventDefault()
if(evt.target.tagName !== "DIV") {
return
}
let i = parseInt(evt.target.id, 10)
console.log("there was a click")
console.log(evt.target.id)
// Verify this element has not been clicked
if (grid[i] !== null) {
return
} else {
// Update grid array with player turn
grid[i] = turn
}
if (winner === "O" || winner === "X") {
return
}
switchPlayer()
victory()
renderGrid()
displayTurn()
tie()
playerDisplayWinner()
}
init()
//EVENTS
// Declare handleClick
document.getElementById("grid").addEventListener("click", playerClick);
restartBtn.addEventListener("click", restartGame);
// create render()
function renderGrid() {
grid.forEach(function(gridValue, gridIdx){
const foundEl = document.getElementById(gridIdx)
if(gridValue !== null) {
foundEl.innerText = gridValue;
} else {
foundEl.innerText = "";
}
})
}
function playerDisplayWinner() {
if (winner !== null) {
playerTurn.innerText = winner + " is the winner!";
}
}
function displayTurn() {
playerTurn.innerText = "It's " + turn + " turn";
}
function tie() {
let itsNull = false
grid.forEach(function(element){
if (element === null) {
itsNull = true
}
})
if (itsNull === false) {
playerTurn.innerText = "It's a tie"
}
}
function restartGame() {
init()
renderGrid()
}