forked from AustinCodingAcademy/TicTacToe-101
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
161 lines (120 loc) · 2.86 KB
/
script.js
File metadata and controls
161 lines (120 loc) · 2.86 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
const getPosts = () => {
return fetch('people.json')
.then(res => res.json())
.then(posts => console.log(posts))
}
//getPosts()
let post = {
firstName: 'My Fetch Title',
lastname: 'My Fetch body',
id: 999
}
let newPost = post => {
let options = {
method: 'POST',
body: JSON.stringify(post),
headers: new Headers({
'Content-Type': 'application/json'
})
}
//return fetch('http://jsonplaceholder.typicode.com/posts', options)
return fetch('people.json', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(error => console.error(`MyError: ${error}`))
}
newPost(post)
*/
let a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let p = document.createElement('p')
let wh = "50px"
let xo = 'X'
let av = { 1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: '' }
let h1, h2, h3, v1, v2, v3, d1, d2
let createTable = () => {
let tbl = document.createElement('table');
let addCell = (tr, v) => {
let td = document.createElement('td');
td.id = v;
tr.appendChild(td);
};
let addRow = (tbl, a) => {
let tr = document.createElement('tr');
a.forEach(e => {
addCell(tr, e);
});
tbl.appendChild(tr);
};
a.forEach(e => {
addRow(tbl, e);
});
tbl.border = 1
tbl.cellPadding = 0
tbl.cellSpacing = 0
document.body.appendChild(tbl);
};
createTable();
let tbl = document.querySelector('table');
let td = tbl.getElementsByTagName('td');
let styleTable = (td) => {
for (var cell of td) {
cell.style.border = '1px solid black';
cell.style.width = wh;
cell.style.height = wh;
cell.style.textAlign = 'center';
cell.style.fontSize = '40px'
};
};
styleTable(td);
let handleTdClick = (i) => {
let cell = document.getElementById(i);
if (cell.innerHTML == '') {
cell.innerHTML = xo;
av[i] = xo;
xo == 'X' ? xo = 'O' : xo = 'X';
checkForWinner();
}
}
let addClickListener = (arr) => {
for (var cell of arr) {
cell.addEventListener('click', e => {
handleTdClick(e.target.id);
})
}
}
let checkForWinner = () => {
h1 = av[1] + av[2] + av[3]
h2 = av[4] + av[5] + av[6]
h3 = av[7] + av[8] + av[9]
v1 = av[1] + av[4] + av[7]
v2 = av[2] + av[5] + av[8]
v3 = av[3] + av[6] + av[9]
d1 = av[1] + av[5] + av[9]
d2 = av[3] + av[5] + av[7]
let hvd = [h1, h2, h3, v1, v2, v3, d1, d2]
hvd.forEach(e => {
if (e == "XXX" || e == "OOO") {
p.innerHTML = 'Winner Winner Chicken Dinner!!!'
}
})
}
addClickListener(td);
let newGame = () => {
p.innerHTML = '';
for (let i in av) {
av[i] = ''
}
for (let i in td) {
td[i].innerHTML = ''
}
}
let createPandBtn = () => {
p.style.height = '25px'
document.body.appendChild(p)
let btn = document.createElement('button');
btn.textContent = "Start New Game"
btn.addEventListener('click', newGame);
document.body.appendChild(btn)
}
createPandBtn()