-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimilarGPT.html
168 lines (144 loc) · 5.1 KB
/
SimilarGPT.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
<!DOCTYPE html>
<html>
<head>
<title>Same Game</title>
<style>
body {
text-align: center;
}
#game-board {
display: inline-block;
}
.tile {
width: 50px;
height: 50px;
display: inline-block;
cursor: pointer;
border: 1px solid #000;
margin: 2px;
}
</style>
</head>
<body>
<h1>Same Game</h1>
<div id="game-board"></div>
<p>Score: <span id="score">0</span></p>
<script>
const colors = ["red", "blue", "green", "yellow"];
const boardSize = 8; // Change this to adjust the size of the board
const emptyCell = { color: "white", removed: false };
let board = [];
let score = 0;
const gameBoard = document.getElementById("game-board");
const scoreDisplay = document.getElementById("score");
function createBoard() {
for (let row = 0; row < boardSize; row++) {
const rowTiles = [];
for (let col = 0; col < boardSize; col++) {
const tile = createTile();
rowTiles.push(tile);
}
board.push(rowTiles);
}
}
function createTile() {
const color = colors[Math.floor(Math.random() * colors.length)];
const tile = document.createElement("div");
tile.className = "tile";
tile.style.backgroundColor = color;
tile.color = color;
tile.removed = false;
tile.addEventListener("click", () => removeTiles(tile));
return tile;
}
function renderBoard() {
gameBoard.innerHTML = "";
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[row].length; col++) {
const tile = board[row][col];
if (!tile.removed) {
gameBoard.appendChild(tile);
}
}
gameBoard.appendChild(document.createElement("br"));
}
}
function removeTiles(startingTile) {
const queue = [startingTile];
const color = startingTile.color;
const tilesToRemove = [];
while (queue.length > 0) {
const currentTile = queue.pop();
if (
currentTile &&
!currentTile.removed &&
currentTile.color === color
) {
currentTile.removed = true;
tilesToRemove.push(currentTile);
const row = Array.from(
currentTile.parentElement.children
).indexOf(currentTile);
const col = Array.from(
currentTile.parentElement.parentElement.children
).indexOf(currentTile.parentElement);
// Check adjacent tiles
const neighbors = [
[row - 1, col], // top
[row + 1, col], // bottom
[row, col - 1], // left
[row, col + 1], // right
];
neighbors.forEach(([r, c]) => {
if (
r >= 0 &&
r < boardSize &&
c >= 0 &&
c < boardSize
) {
queue.push(board[r][c]);
}
});
}
}
if (tilesToRemove.length > 1) {
// Remove tiles
tilesToRemove.forEach((tile) => {
tile.style.backgroundColor = emptyCell.color;
tile.removed = true;
});
// Update score
score += tilesToRemove.length;
scoreDisplay.textContent = score;
// Check for a win
if (score === boardSize * boardSize) {
alert("Congratulations! You cleared the board!");
resetGame();
}
// Collapse columns
for (let col = 0; col < boardSize; col++) {
const column = board.map((row) => row[col]);
const nonEmpty = column.filter((tile) => !tile.removed);
for (let row = 0; row < boardSize; row++) {
if (row < nonEmpty.length) {
board[row][col] = nonEmpty[row];
} else {
board[row][col] = createTile();
}
}
}
renderBoard();
}
}
function resetGame() {
board = [];
score = 0;
scoreDisplay.textContent = score;
createBoard();
renderBoard();
}
createBoard();
renderBoard();
</script>
</body>
</html>