-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
150 lines (127 loc) · 5.07 KB
/
app.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
147
148
149
150
while (!player1){
var player1 = prompt('Player One: Enter your name. You will be red.');
};
var player1Color = 'red';
while (!player2){
var player2 = prompt('Player Two: Enter your name. You will be yellow.');
};
var player2Color = 'yellow';
// Selectors
var tableRow = document.getElementsByTagName('tr');
var tableData = document.getElementsByTagName('td');
var playerTurn = document.querySelector('.player-turn');
const slots = document.querySelectorAll('.slot');
const resetBtn = document.querySelector('.reset');
var currentPlayer = 1;
let winner;
playerTurn.textContent = `${player1}'s turn!`
// Log cell coordinates when clicked
for (i = 0; i < tableData.length; i ++){
tableData[i].addEventListener('click', (e) =>{
console.log(`${e.target.parentElement.rowIndex},${e.target.cellIndex}`)
});
};
// Funtions
function changeColor(e){
// Get clicked column index
let column = e.target.cellIndex;
let row = [];
for (i = 5; i > -1; i--){
if (tableRow[i].children[column].style.backgroundColor == 'white'){
row.push(tableRow[i].children[column]);
if (currentPlayer === 1){
row[0].style.backgroundColor = 'red';
if (horizontalCheck() || verticalCheck() || diagonalCheck() || diagonalCheck2()){
playerTurn.textContent = `${player1} WINS!!`;
playerTurn.style.color = player1Color;
return alert(`${player1} WINS!!`);
}else if (drawCheck()){
playerTurn.textContent = 'DRAW!';
return alert('DRAW!');
}else{
playerTurn.textContent = `${player2}'s turn`
return currentPlayer = 2;
}
}else{
row[0].style.backgroundColor = 'yellow';
if (horizontalCheck() || verticalCheck() || diagonalCheck() || diagonalCheck2()){
playerTurn.textContent = `${player2} WINS!!`;
playerTurn.style.color = player2Color;
return alert(`${player2} WINS!!`);
}else if (drawCheck()){
playerTurn.textContent = 'DRAW!';
return alert('DRAW!');
}else{
playerTurn.textContent = `${player1}'s turn`;
return currentPlayer = 1;
}
}
}
}
}
Array.prototype.forEach.call(tableData, (cell) => {
cell.addEventListener('click', changeColor);
// Set all slots to white for new game.
cell.style.backgroundColor = 'white';
});
function colorMatchCheck(one, two, three, four){
return (one === two && one === three && one === four && one !== 'white' && one !== undefined);
}
function horizontalCheck(){
for (let row = 0; row < tableRow.length; row++){
for (let col =0; col < 4; col++){
if (colorMatchCheck(tableRow[row].children[col].style.backgroundColor,tableRow[row].children[col+1].style.backgroundColor,
tableRow[row].children[col+2].style.backgroundColor, tableRow[row].children[col+3].style.backgroundColor)){
return true;
}
}
}
}
function verticalCheck(){
for (let col = 0; col < 7; col++){
for (let row = 0; row < 3; row++){
if (colorMatchCheck(tableRow[row].children[col].style.backgroundColor, tableRow[row+1].children[col].style.backgroundColor,
tableRow[row+2].children[col].style.backgroundColor,tableRow[row+3].children[col].style.backgroundColor)){
return true;
};
}
}
}
function diagonalCheck(){
for(let col = 0; col < 4; col++){
for (let row = 0; row < 3; row++){
if (colorMatchCheck(tableRow[row].children[col].style.backgroundColor, tableRow[row+1].children[col+1].style.backgroundColor,
tableRow[row+2].children[col+2].style.backgroundColor,tableRow[row+3].children[col+3].style.backgroundColor)){
return true;
}
}
}
}
function diagonalCheck2(){
for(let col = 0; col < 4; col++){
for (let row = 5; row > 2; row--){
if (colorMatchCheck(tableRow[row].children[col].style.backgroundColor, tableRow[row-1].children[col+1].style.backgroundColor,
tableRow[row-2].children[col+2].style.backgroundColor,tableRow[row-3].children[col+3].style.backgroundColor)){
return true;
}
}
}
}
function drawCheck(){
let fullSlot = []
for (i=0; i < tableData.length; i++){
if (tableData[i].style.backgroundColor !== 'white'){
fullSlot.push(tableData[i]);
}
}
if (fullSlot.length === tableData.length){
return true;
}
}
resetBtn.addEventListener('click', () => {
slots.forEach(slot => {
slot.style.backgroundColor = 'white';
});
playerTurn.style.color = 'black';
return (currentPlayer === 1 ? playerTurn.textContent = `${player1}'s turn` : playerTurn.textContent = `${player2}'s turn`);
});