-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
232 lines (213 loc) · 8.97 KB
/
script.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function() {
var currentPlayer = "player1";
var currentHighlightedPlayer = $(".highlighted-player");
var gamePieces = $(".piece");
currentHighlightedPlayer.text("Player 1's Turn");
currentHighlightedPlayer.addClass("player-one");
gamePieces.addClass(currentPlayer);
gamePieces.addClass("invisible");
var pieceDropping = false;
for (var i = 0; i < gamePieces.length; i++) {
(function(pieceIndex) {
gamePieces.eq(pieceIndex).on("click", function(e) {
if (!pieceDropping) {
var columns = $(e.target.parentNode.parentNode)
.siblings()
.children();
var col = columns.eq(pieceIndex);
var slotsInCol = col.children();
var container = col.parent();
var totalSlots = container.children().children();
for (var i = slotsInCol.length - 1; i >= 0; i--) {
if (
!slotsInCol.eq(i).hasClass("player1") &&
!slotsInCol.eq(i).hasClass("player2")
) {
// put piece drop animation here instead of instantly adding class
// need to make piece go to position of lowest empty slot
// after piece falls into slot, add class to slot and move piece back
// instantly to starting position
var originalPieceYPosition = $(this).offset().top;
var slotYPosition = slotsInCol.eq(i).offset().top;
var dropDistance =
slotYPosition - originalPieceYPosition + 10;
$(this).css({
opacity: 1
});
pieceDropping = true;
$(this).animate(
{
top: "+=" + dropDistance
},
500,
function() {
pieceDropping = false;
slotsInCol.eq(i).addClass(currentPlayer);
$(this).css({
top: originalPieceYPosition,
opacity: ""
});
if (checkForVictory(slotsInCol)) {
// if i get here there has been a column victory..
// time to do the victory dance!
blink();
currentHighlightedPlayer.text(
"Player " +
currentPlayer[6] +
" wins!"
);
confirmPlayAgain();
} else if (checkForVictory(slotsInRow)) {
blink();
currentHighlightedPlayer.text(
"Player " +
currentPlayer[6] +
" wins!"
);
confirmPlayAgain();
} else if (
checkDiagonalVictory(totalSlots)
) {
currentHighlightedPlayer.text(
"Player " +
currentPlayer[6] +
" wins!"
);
blink();
confirmPlayAgain();
} else {
switchPlayer();
}
}
);
break;
}
}
var slotsInRow = $(".row" + i);
if (i === -1) {
return;
}
}
});
})(i);
gamePieces.eq(i).hover(
function() {
if (!pieceDropping) {
$(this).removeClass("invisible");
}
},
function() {
$(this).addClass("invisible");
}
);
}
var victoryBlink = [];
function checkForVictory(slots) {
// in here we will check to see if there are 4 in a row...
var count = 0;
// we want to loop through the slots, and see if there are 4 sequentially
// the same...
for (var i = 0; i < slots.length; i++) {
if (slots.eq(i).hasClass(currentPlayer)) {
// do something...
// this means we found a slot with current player.
victoryBlink.push(slots.eq(i));
count++;
if (count === 4) {
return true;
}
} else {
// do something else...
// this means there is no current player, so reset count
count = 0;
victoryBlink = [];
}
}
}
function checkDiagonalVictory(slots) {
//loop through all slots in all columns
for (var i = 0; i < slots.length; i++) {
if (
slots.eq(i).hasClass(currentPlayer) &&
!slots.eq(i).is(":last-child")
) {
victoryBlink.push(slots.eq(i));
if (
slots.eq(i + 7).hasClass(currentPlayer) &&
!slots.eq(i + 7).is(":last-child")
) {
victoryBlink.push(slots.eq(i + 7));
if (
slots.eq(i + 14).hasClass(currentPlayer) &&
!slots.eq(i + 14).is(":last-child")
) {
victoryBlink.push(slots.eq(i + 14));
if (slots.eq(i + 21).hasClass(currentPlayer)) {
victoryBlink.push(slots.eq(i + 21));
return true;
}
}
}
}
victoryBlink = [];
if (
slots.eq(i).hasClass(currentPlayer) &&
!slots.eq(i).is(":first-child")
) {
victoryBlink.push(slots.eq(i));
if (
slots.eq(i + 5).hasClass(currentPlayer) &&
!slots.eq(i + 5).is(":first-child")
) {
victoryBlink.push(slots.eq(i + 5));
if (
slots.eq(i + 10).hasClass(currentPlayer) &&
!slots.eq(i + 10).is(":first-child")
) {
victoryBlink.push(slots.eq(i + 10));
if (slots.eq(i + 15).hasClass(currentPlayer)) {
victoryBlink.push(slots.eq(i + 15));
return true;
}
}
}
}
victoryBlink = [];
}
}
function confirmPlayAgain() {
setTimeout(function() {
if (confirm("Player " + currentPlayer[6] + " wins! Play again?")) {
window.location.reload();
} else {
pieceDropping = true;
}
}, 3000);
}
function switchPlayer() {
gamePieces.removeClass(currentPlayer);
currentPlayer === "player1"
? (currentPlayer = "player2")
: (currentPlayer = "player1");
gamePieces.addClass(currentPlayer);
if (currentHighlightedPlayer.hasClass("player-one")) {
currentHighlightedPlayer.removeClass("player-one");
currentHighlightedPlayer.text("Player 2's Turn");
currentHighlightedPlayer.addClass("player-two");
} else {
currentHighlightedPlayer.removeClass("player-two");
currentHighlightedPlayer.text("Player 1's Turn");
currentHighlightedPlayer.addClass("player-one");
}
}
function blink() {
for (let i = 0; i < victoryBlink.length; i++) {
setInterval(function() {
victoryBlink[i]
.children()
.fadeOut(100)
.fadeIn(100);
}, 200);
}
}
})();