add js 1
This commit is contained in:
parent
3fda1612b7
commit
2d23811cf9
242
ody git.js
Normal file
242
ody git.js
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
// Simple Blackjack implementation
|
||||||
|
const SUITS = ['♠','♥','♦','♣'];
|
||||||
|
const RANKS = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
|
||||||
|
|
||||||
|
const dealerHandEl = document.getElementById('dealer-hand');
|
||||||
|
const playerHandEl = document.getElementById('player-hand');
|
||||||
|
const dealerValueEl = document.getElementById('dealer-value');
|
||||||
|
const playerValueEl = document.getElementById('player-value');
|
||||||
|
const balanceEl = document.getElementById('balance');
|
||||||
|
const betInput = document.getElementById('bet-input');
|
||||||
|
const betBtn = document.getElementById('bet-btn');
|
||||||
|
const currentBetEl = document.getElementById('current-bet');
|
||||||
|
const hitBtn = document.getElementById('hit');
|
||||||
|
const standBtn = document.getElementById('stand');
|
||||||
|
const doubleBtn = document.getElementById('double');
|
||||||
|
const newRoundBtn = document.getElementById('new-round');
|
||||||
|
const messageEl = document.getElementById('message');
|
||||||
|
|
||||||
|
let deck = [];
|
||||||
|
let dealer = [];
|
||||||
|
let player = [];
|
||||||
|
let dealerHidden = true;
|
||||||
|
let balance = 1000;
|
||||||
|
let currentBet = 0;
|
||||||
|
let inRound = false;
|
||||||
|
|
||||||
|
// Membuat deck kartu
|
||||||
|
function makeDeck() {
|
||||||
|
deck = [];
|
||||||
|
for (let s of SUITS) {
|
||||||
|
for (let r of RANKS) {
|
||||||
|
deck.push({ suit: s, rank: r });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shuffle
|
||||||
|
function shuffle() {
|
||||||
|
for (let i = deck.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[deck[i], deck[j]] = [deck[j], deck[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nilai kartu
|
||||||
|
function cardValue(card) {
|
||||||
|
if (card.rank === 'A') return 11;
|
||||||
|
if (['J','Q','K'].includes(card.rank)) return 10;
|
||||||
|
return Number(card.rank);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nilai total tangan
|
||||||
|
function handValues(hand) {
|
||||||
|
let total = 0;
|
||||||
|
let aceCount = 0;
|
||||||
|
|
||||||
|
for (let c of hand) {
|
||||||
|
total += cardValue(c);
|
||||||
|
if (c.rank === 'A') aceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (total > 21 && aceCount > 0) {
|
||||||
|
total -= 10;
|
||||||
|
aceCount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render kartu
|
||||||
|
function renderHand(container, hand, hideFirst = false) {
|
||||||
|
container.innerHTML = '';
|
||||||
|
|
||||||
|
hand.forEach((card, i) => {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className = 'card';
|
||||||
|
|
||||||
|
// warna merah ♥ ♦
|
||||||
|
if (card.suit === '♥' || card.suit === '♦') {
|
||||||
|
div.classList.add('red');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hideFirst && i === 0) {
|
||||||
|
div.className = "card back";
|
||||||
|
div.textContent = "🂠";
|
||||||
|
} else {
|
||||||
|
div.innerHTML = `
|
||||||
|
<div>${card.rank}${card.suit}</div>
|
||||||
|
<div>${card.rank}${card.suit}</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
container.appendChild(div);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update UI
|
||||||
|
function updateUI() {
|
||||||
|
renderHand(playerHandEl, player, false);
|
||||||
|
renderHand(dealerHandEl, dealer, dealerHidden);
|
||||||
|
|
||||||
|
dealerValueEl.textContent = dealerHidden ? "?" : handValues(dealer);
|
||||||
|
playerValueEl.textContent = handValues(player);
|
||||||
|
|
||||||
|
balanceEl.textContent = balance;
|
||||||
|
currentBetEl.textContent = currentBet;
|
||||||
|
|
||||||
|
hitBtn.disabled = !inRound;
|
||||||
|
standBtn.disabled = !inRound;
|
||||||
|
doubleBtn.disabled = !inRound;
|
||||||
|
|
||||||
|
if (!inRound) {
|
||||||
|
messageEl.textContent = "Pasang taruhan untuk mulai.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mulai ronde
|
||||||
|
function startRound() {
|
||||||
|
if (inRound) return;
|
||||||
|
|
||||||
|
const bet = Number(betInput.value);
|
||||||
|
|
||||||
|
if (bet <= 0 || bet > balance) {
|
||||||
|
messageEl.textContent = "Taruhan tidak valid!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
balance -= bet;
|
||||||
|
currentBet = bet;
|
||||||
|
|
||||||
|
makeDeck();
|
||||||
|
shuffle();
|
||||||
|
|
||||||
|
dealer = [deck.pop(), deck.pop()];
|
||||||
|
player = [deck.pop(), deck.pop()];
|
||||||
|
|
||||||
|
dealerHidden = true;
|
||||||
|
inRound = true;
|
||||||
|
|
||||||
|
messageEl.textContent = "Permainan dimulai!";
|
||||||
|
updateUI();
|
||||||
|
|
||||||
|
// Natural blackjack
|
||||||
|
if (handValues(player) === 21) {
|
||||||
|
playerStand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player hit
|
||||||
|
function playerHit() {
|
||||||
|
if (!inRound) return;
|
||||||
|
player.push(deck.pop());
|
||||||
|
updateUI();
|
||||||
|
|
||||||
|
if (handValues(player) > 21) {
|
||||||
|
dealerHidden = false;
|
||||||
|
messageEl.textContent = "Kamu bust! Dealer menang.";
|
||||||
|
inRound = false;
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player stand
|
||||||
|
function playerStand() {
|
||||||
|
if (!inRound) return;
|
||||||
|
|
||||||
|
dealerHidden = false;
|
||||||
|
|
||||||
|
while (handValues(dealer) < 17) {
|
||||||
|
dealer.push(deck.pop());
|
||||||
|
}
|
||||||
|
|
||||||
|
const pv = handValues(player);
|
||||||
|
const dv = handValues(dealer);
|
||||||
|
|
||||||
|
if (dv > 21 || pv > dv) {
|
||||||
|
messageEl.textContent = "Kamu menang!";
|
||||||
|
balance += currentBet * 2;
|
||||||
|
} else if (pv === dv) {
|
||||||
|
messageEl.textContent = "Seri!";
|
||||||
|
balance += currentBet;
|
||||||
|
} else {
|
||||||
|
messageEl.textContent = "Dealer menang!";
|
||||||
|
}
|
||||||
|
|
||||||
|
currentBet = 0;
|
||||||
|
inRound = false;
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double down
|
||||||
|
function playerDouble() {
|
||||||
|
if (!inRound) return;
|
||||||
|
if (balance < currentBet) {
|
||||||
|
messageEl.textContent = "Saldo tidak cukup untuk double!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
balance -= currentBet;
|
||||||
|
currentBet *= 2;
|
||||||
|
|
||||||
|
player.push(deck.pop());
|
||||||
|
updateUI();
|
||||||
|
|
||||||
|
if (handValues(player) > 21) {
|
||||||
|
dealerHidden = false;
|
||||||
|
messageEl.textContent = "Bust saat double! Dealer menang.";
|
||||||
|
inRound = false;
|
||||||
|
return updateUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
playerStand();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset game
|
||||||
|
newRoundBtn.addEventListener('click', () => {
|
||||||
|
balance = 1000;
|
||||||
|
currentBet = 0;
|
||||||
|
dealer = [];
|
||||||
|
player = [];
|
||||||
|
deck = [];
|
||||||
|
inRound = false;
|
||||||
|
dealerHidden = true;
|
||||||
|
messageEl.textContent = "Bank di-reset.";
|
||||||
|
updateUI();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Events
|
||||||
|
betBtn.addEventListener('click', startRound);
|
||||||
|
hitBtn.addEventListener('click', playerHit);
|
||||||
|
standBtn.addEventListener('click', playerStand);
|
||||||
|
doubleBtn.addEventListener('click', playerDouble);
|
||||||
|
|
||||||
|
// Keyboard shortcut
|
||||||
|
window.addEventListener('keydown', e => {
|
||||||
|
if (e.key === 'h') playerHit();
|
||||||
|
if (e.key === 's') playerStand();
|
||||||
|
if (e.key === 'd') playerDouble();
|
||||||
|
if (e.key === 'Enter') startRound();
|
||||||
|
});
|
||||||
|
|
||||||
|
updateUI();
|
||||||
Loading…
x
Reference in New Issue
Block a user