-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnect4.js
More file actions
102 lines (87 loc) · 2.91 KB
/
Copy pathconnect4.js
File metadata and controls
102 lines (87 loc) · 2.91 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
const userMoves = [0, 0, 0, 0];
const yellowMoves = [1, 1, 1];
const board = document.getElementById('board');
let moveIndex = 0;
function createBoard() {
board.innerHTML = '';
for (let i = 0; i < 42; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
board.appendChild(cell);
}
showPopup(`<strong>Hi! Welcome to Connect4 instructions!</strong><br><br>I'm Tetris Cat and I will be teaching you how to play!<br><br>Click the left column to start!`);
}
function resetGame() {
moveIndex = 0;
createBoard();
showPopup('Click the left column to start!');
enableColumn(userMoves[moveIndex]);
}
function enableColumn(col) {
// Enable clicking the entire column
for (let row = 5; row >= 0; row--) {
const cell = board.children[row * 7 + col];
// We want to check if the cell is empty, and if so, add the onclick event
if (!cell.classList.contains('red') && !cell.classList.contains('yellow')) {
cell.onclick = () => userMove(col);
}
}
}
function userMove(col) {
dropPiece(col, 'red');
if (checkWin('red')) {
showPopup('You won vertically! You can also win diagonally or horizontally.');
disableBoard();
return;
}
setTimeout(() => {
if (moveIndex < yellowMoves.length) {
dropPiece(yellowMoves[moveIndex], 'yellow');
}
moveIndex++;
if (moveIndex < userMoves.length) {
showPopup(`Good! Now click the left column again.`);
enableColumn(userMoves[moveIndex]);
}
}, 400);
}
function dropPiece(col, color) {
for (let row = 5; row >= 0; row--) {
const cell = board.children[row * 7 + col];
if (!cell.classList.contains('red') && !cell.classList.contains('yellow')) {
cell.classList.add(color);
break;
}
}
}
function disableBoard() {
document.querySelectorAll('.cell').forEach(cell => cell.onclick = null);
}
function checkWin(color) {
// Vertical check
for (let c = 0; c < 7; c++) {
for (let r = 0; r < 3; r++) {
if ([0,1,2,3].every(i => board.children[(r+i)*7+c].classList.contains(color))) {
return true;
}
}
}
return false;
}
function showPopup(text) {
document.getElementById('popup-text').innerHTML = text;
document.getElementById('C4overlay').classList.add("show");
document.getElementById('C4popupBox').classList.add("show");
}
function closePopup() {
document.getElementById('C4overlay').classList.remove("show");
document.getElementById('C4popupBox').classList.remove("show");
}
resetGame();
document.addEventListener("DOMContentLoaded", function() {
const loggedInUser = localStorage.getItem("loggedInUser");
const loginBtn = document.getElementById("login-btn");
if (loggedInUser) {
loginBtn.textContent = `User: ${loggedInUser}`;
}
});