forked from AustinCodingAcademy/TicTacToe-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
146 lines (115 loc) · 5.15 KB
/
scripts.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ***********************
// INSTRUCTIONS
// ***********************
// 1. Read the code below one block at a time.
// 2. Look for the @TODOs, and figure out how to fix them.
// next to each @TODO you will find tasks that need to be finished
// The variable will change from X to O based on what player turn it is. We need to hold this so we can place an X or O on the board when they're clicked.
let currentMarker = 'X'
let board= [
["","",""],
["","",""],
["","",""]
]
// this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML.
// "this" is a special word in JS but "element" could have been "thing" or "el" or whatever we wanted it to be as long as we use it again in the "console.log" statement
const handleClick = (element) => {
// this uses the "log" method on the "console" to log out the element's id so we can see it with our human eyes
console.log(`The element you clicked on has an id: ${element.id}`)
// this next line prevents an X being changed to an O or an O being changed to an X by...
// checking to see if the square clicked has anything in it, if not continue
if(!document.getElementById(element.id).innerHTML){
addMarker(element.id)
}
}
// this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function.
const addMarker = (id) => {
const row = parseInt(id.charAt(0))
const column = parseInt(id.charAt(2))
board[row][column] = currentMarker
// @TODO-1: Open the console tab in your Chrome Inspector Tool and click on the top-left square to see what's logged to the console.
console.log(`*** The current marker is: ${currentMarker}. ***`)
console.log(`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`)
// @TODO-2: Build a line of code that will set the innerHTML property of the element that was clicked to the "currentMarker"
document.getElementById(id).innerHTML = currentMarker
console.log('good job')
// @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line:
// = currentMarker
// .getElementById(id)
// document
// .innerHTML
changeMarker()
}
// This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X"
const changeMarker = () => {
checkForWin()
if(currentMarker === "X"){
currentMarker = "O"
} else {
currentMarker = "X"
}
}
// This "resetBoard" function is called when the user clicks on the "Restart" button.
const resetBoard = () => {
// @TODO-3: To make your "Restart" button work you'll need to build a line of code here that:
// collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp
var squares = document.getElementsByTagName("td");
// @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line:
// squares
// .getElementsByTagName("TD")
// =
// document
// const
// loops over the HTML Collection of TDs and clears out the Xs and Os
for (i=0; i < squares.length; i++) {
// will log out the id of each square as it loops over them.
console.log(squares[i].id)
// sets the innerHTML to null to replace the "X" or "O"
squares[i].innerHTML = null
}
board= [
["","",""],
["","",""],
["","",""]
]
}
const checkForWin = () => {
console.log('horizontalWin', horizontalWin)
console.log('***********************', horizontalWin() || verticalWin() || diagonalWin())
if(horizontalWin() || verticalWin() || diagonalWin()) {
window.alert(`Player ${currentMarker} won!`)
} //else {
//changeMarker()
// }
}
const horizontalWin = () => {
if((board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X") || (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O")) {
return true
} else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") || (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O")) {
return true
} else if ((board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") || (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O")) {
return true
} else {
return false
}
}
const verticalWin = () => {
if((board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") || (board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O")) {
return true
} else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") || (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O")) {
return true
} else if ((board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") || (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O")) {
return true
} else {
return false
}
}
const diagonalWin = () => {
if((board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") || (board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O")) {
return true
} else if ((board[2][0] == "X" && board[1][1] == "X" && board[0][2] == "X") || (board[2][0] == "O" && board[1][1] == "O" && board[0][2] == "O")) {
return true
} else {
return false
}
}