Compare commits
2 Commits
854a6135da
...
7c3e6851aa
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c3e6851aa | |||
| e7c73920d4 |
@ -241,7 +241,7 @@ body {
|
||||
</div>
|
||||
|
||||
<script>
|
||||
/* === 8 GAMBAR (16 kartu) === */
|
||||
|
||||
const images = [
|
||||
"asset/alpukat.jpg",
|
||||
"asset/anggur.jpg",
|
||||
@ -280,12 +280,32 @@ function showComboPopup(targetCard, combo, bonus) {
|
||||
setTimeout(() => popup.remove(), 1500);
|
||||
}
|
||||
|
||||
function shuffleArray(array) {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
|
||||
function shuffleDistant(cards, minDistance = 5) {
|
||||
let valid = false;
|
||||
let result;
|
||||
|
||||
while (!valid) {
|
||||
result = [...cards].sort(() => Math.random() - 0.5);
|
||||
valid = true;
|
||||
const checked = new Set();
|
||||
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
const value = result[i];
|
||||
if (checked.has(value)) continue;
|
||||
checked.add(value);
|
||||
|
||||
const firstIndex = result.indexOf(value);
|
||||
const lastIndex = result.lastIndexOf(value);
|
||||
const distance = Math.abs(lastIndex - firstIndex);
|
||||
|
||||
if (distance < minDistance) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function startTimer() {
|
||||
@ -359,7 +379,7 @@ function startGame() {
|
||||
const board = document.getElementById("game-board");
|
||||
board.innerHTML = "";
|
||||
|
||||
shuffleArray([...cards]).forEach(image => {
|
||||
shuffleDistant(cards, 4).forEach(image => {
|
||||
const card = document.createElement("div");
|
||||
card.className = "card";
|
||||
card.innerHTML = `
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user