-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
ConnectFour.html
228 lines (204 loc) · 5.3 KB
/
ConnectFour.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connect Four</title>
<style>
/* General Styling */
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: 'Roboto', sans-serif;
background-color: #222;
color: white;
margin: 0;
padding: 0;
}
/* Connect Four Layout */
.connect-four {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
/* Neon Board Styling */
.board {
display: flex;
flex-direction: column;
background-color: #003366;
padding: 20px;
border-radius: 15px;
border: 5px solid #00FFFF;
box-shadow: 0 0 20px #00FFFF;
animation: neonPulse 1.5s infinite alternate;
}
@keyframes neonPulse {
from { box-shadow: 0 0 10px #00FFFF, 0 0 20px #00FFFF; }
to { box-shadow: 0 0 20px #00FFFF, 0 0 40px #00FFFF; }
}
/* Row and Cell Styling */
.row {
display: flex;
}
.cell {
width: 80px;
height: 80px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
margin: 2px;
cursor: pointer;
}
/* Hole Styling */
.hole {
width: 70px;
height: 70px;
background-color: #004080;
border-radius: 50%;
box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.6);
transition: transform 0.2s ease;
}
.hole:hover {
transform: scale(1.1);
}
/* Piece Colors */
.piece {
width: 70px;
height: 70px;
border-radius: 50%;
position: absolute;
}
.R { background: radial-gradient(circle at 30% 30%, #f48282, #cc0000, #330d0d); }
.Y { background: radial-gradient(circle at 30% 30%, #ffff99, #cccc00, #5b5b03); }
/* Title Styling */
.neon-title {
font-size: 3rem;
color: #8800ff;
text-align: center;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 5px;
margin-bottom: 20px;
}
/* Button Styling */
button {
margin-top: 20px;
padding: 12px 25px;
font-size: 18px;
background-color: #004080;
color: white;
border: none;
border-radius: 12px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
}
button:hover {
background-color: #0059b3;
transform: scale(1.05);
}
</style>
</head>
<body>
<div class="connect-four">
<h1 class="neon-title">CONNECT FOUR</h1>
<div id="board" class="board"></div>
<button onclick="restartGame()">Restart Game</button>
<div id="status" class="status"></div>
</div>
<script>
const ROWS = 6;
const COLS = 7;
let currentPlayer = 'R'; // Red starts
let board = Array(ROWS).fill().map(() => Array(COLS).fill(null));
// Create board layout
const boardElement = document.getElementById("board");
function createBoard() {
boardElement.innerHTML = '';
for (let row = 0; row < ROWS; row++) {
const rowElement = document.createElement('div');
rowElement.classList.add('row');
for (let col = 0; col < COLS; col++) {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.addEventListener('click', () => handleClick(col));
const holeElement = document.createElement('div');
holeElement.classList.add('hole');
cellElement.appendChild(holeElement);
rowElement.appendChild(cellElement);
}
boardElement.appendChild(rowElement);
}
}
// Handle player click to place piece
function handleClick(col) {
for (let row = ROWS - 1; row >= 0; row--) {
if (!board[row][col]) {
board[row][col] = currentPlayer;
updateBoard();
if (checkWin(row, col)) {
document.getElementById('status').textContent = `${currentPlayer} wins!`;
} else {
switchPlayer();
}
return;
}
}
}
// Update the board UI
function updateBoard() {
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
if (board[row][col]) {
const cell = boardElement.children[row].children[col];
const piece = document.createElement('div');
piece.classList.add('piece', board[row][col]);
cell.appendChild(piece);
}
}
}
}
// Switch current player
function switchPlayer() {
currentPlayer = currentPlayer === 'R' ? 'Y' : 'R';
}
// Check for win condition
function checkWin(row, col) {
const directions = [
{ r: 1, c: 0 }, { r: 0, c: 1 }, { r: 1, c: 1 }, { r: 1, c: -1 }
];
for (let {r, c} of directions) {
let count = 1;
count += countPieces(row, col, r, c);
count += countPieces(row, col, -r, -c);
if (count >= 4) return true;
}
return false;
}
// Count pieces in a direction
function countPieces(row, col, r, c) {
let count = 0;
let color = board[row][col];
let i = row + r;
let j = col + c;
while (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] === color) {
count++;
i += r;
j += c;
}
return count;
}
// Restart the game
function restartGame() {
board = Array(ROWS).fill().map(() => Array(COLS).fill(null));
currentPlayer = 'R';
document.getElementById('status').textContent = '';
createBoard();
}
// Initialize the game
createBoard();
</script>
</body>
</html>