-
Notifications
You must be signed in to change notification settings - Fork 164
/
connect4.js
93 lines (64 loc) · 3.03 KB
/
connect4.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
val_c1 = 1
val_c2 = 1
val_c3 = 1
val_c4 = 1
val_c5 = 1
val_c6 = 1
val_c7 = 1
turn = 1
//checking the winner
function check(player) {
setTimeout(() => {
for (i = 1; i <= 7; i++) {
for (j = 1; j <= 3; j++) {
if (document.getElementById(`c${i}r${j}`).style.backgroundColor == `${player}` && document.getElementById(`c${i}r${j + 1}`).style.backgroundColor == `${player}` && document.getElementById(`c${i}r${j + 2}`).style.backgroundColor == `${player}` && document.getElementById(`c${i}r${j + 3}`).style.backgroundColor == `${player}`) {
alert(`${player} wins`)
location.reload()
}
}
}
for (i = 1; i <= 6; i++) {
for (j = 1; j <= 4; j++) {
if (document.getElementById(`c${j}r${i}`).style.backgroundColor == `${player}` && document.getElementById(`c${j + 1}r${i}`).style.backgroundColor == `${player}` && document.getElementById(`c${j + 2}r${i}`).style.backgroundColor == `${player}` && document.getElementById(`c${j + 3}r${i}`).style.backgroundColor == `${player}`) {
alert(`${player} wins`)
location.reload()
}
}
}
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 3; j++) {
if (document.getElementById(`c${i}r${j}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 1}r${j + 1}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 2}r${j + 2}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 3}r${j + 3}`).style.backgroundColor == `${player}`) {
alert(`${player} wins`)
location.reload()
}
}
}
for (i = 1; i <= 4; i++) {
for (j = 6; j >= 4; j--) {
if (document.getElementById(`c${i}r${j}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 1}r${j - 1}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 2}r${j - 2}`).style.backgroundColor == `${player}` && document.getElementById(`c${i + 3}r${j - 3}`).style.backgroundColor == `${player}`) {
alert(`${player} wins`)
location.reload()
}
}
}
}, 200)
}
//playing
document.querySelectorAll(".column").forEach((e) => {
e.addEventListener("click", () => {
sum = eval(`val_${e.id}`)
eval(`val_${e.id}++`)
if (sum <= 6 && turn % 2 != 0) {
document.getElementById(`${e.id}r${sum}`).style.backgroundColor = "red"
turn++
check('red')
document.getElementById("whosturn").innerText = "Yellow's Turn"
}
else if (sum <= 6 && turn % 2 == 0) {
document.getElementById(`${e.id}r${sum}`).style.backgroundColor = "yellow"
turn++
check('yellow')
document.getElementById("whosturn").innerText = "Red's Turn"
}
})
})