reload js
This commit is contained in:
parent
4848d0d5b1
commit
c9a81f8119
373
ody git.js
373
ody git.js
@ -1,242 +1,171 @@
|
|||||||
// Simple Blackjack implementation
|
// Simple Blackjack implementation
|
||||||
const SUITS = ['♠','♥','♦','♣'];
|
const SUITS = ['♠','♥','♦','♣'];
|
||||||
const RANKS = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
|
const RANKS = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
|
||||||
|
|
||||||
const dealerHandEl = document.getElementById('dealer-hand');
|
// DOM
|
||||||
const playerHandEl = document.getElementById('player-hand');
|
const dealerHandEl = document.getElementById('dealer-hand');
|
||||||
const dealerValueEl = document.getElementById('dealer-value');
|
const playerHandEl = document.getElementById('player-hand');
|
||||||
const playerValueEl = document.getElementById('player-value');
|
const dealerValueEl = document.getElementById('dealer-value');
|
||||||
const balanceEl = document.getElementById('balance');
|
const playerValueEl = document.getElementById('player-value');
|
||||||
const betInput = document.getElementById('bet-input');
|
const balanceEl = document.getElementById('balance');
|
||||||
const betBtn = document.getElementById('bet-btn');
|
const betInput = document.getElementById('bet-input');
|
||||||
const currentBetEl = document.getElementById('current-bet');
|
const betBtn = document.getElementById('bet-btn');
|
||||||
const hitBtn = document.getElementById('hit');
|
const currentBetEl = document.getElementById('current-bet');
|
||||||
const standBtn = document.getElementById('stand');
|
const hitBtn = document.getElementById('hit');
|
||||||
const doubleBtn = document.getElementById('double');
|
const standBtn = document.getElementById('stand');
|
||||||
const newRoundBtn = document.getElementById('new-round');
|
const doubleBtn = document.getElementById('double');
|
||||||
const messageEl = document.getElementById('message');
|
const newRoundBtn = document.getElementById('new-round');
|
||||||
|
const messageEl = document.getElementById('message');
|
||||||
|
|
||||||
let deck = [];
|
let deck = [];
|
||||||
let dealer = [];
|
let dealer = [];
|
||||||
let player = [];
|
let player = [];
|
||||||
let dealerHidden = true;
|
let dealerHidden = true;
|
||||||
let balance = 1000;
|
let balance = 1000;
|
||||||
let currentBet = 0;
|
let currentBet = 0;
|
||||||
let inRound = false;
|
let inRound = false;
|
||||||
|
|
||||||
// Membuat deck kartu
|
function makeDeck(){
|
||||||
function makeDeck() {
|
deck = [];
|
||||||
deck = [];
|
for(const s of SUITS){
|
||||||
for (let s of SUITS) {
|
for(const r of RANKS){
|
||||||
for (let r of RANKS) {
|
deck.push({suit:s,rank:r});
|
||||||
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) {
|
function shuffle(){
|
||||||
div.className = "card back";
|
for(let i=deck.length-1;i>0;i--){
|
||||||
div.textContent = "🂠";
|
const j = Math.floor(Math.random()*(i+1));
|
||||||
} else {
|
[deck[i],deck[j]] = [deck[j],deck[i]];
|
||||||
div.innerHTML = `
|
}
|
||||||
<div>${card.rank}${card.suit}</div>
|
|
||||||
<div>${card.rank}${card.suit}</div>
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
container.appendChild(div);
|
function cardValue(card){
|
||||||
});
|
const r = card.rank;
|
||||||
}
|
if(r==='A') return [1,11];
|
||||||
|
if(['J','Q','K'].includes(r)) return [10];
|
||||||
|
return [parseInt(r,10)];
|
||||||
|
}
|
||||||
|
|
||||||
// Update UI
|
function handValues(hand){
|
||||||
function updateUI() {
|
// returns best value <=21 or smallest if bust
|
||||||
renderHand(playerHandEl, player, false);
|
let totals = [0];
|
||||||
renderHand(dealerHandEl, dealer, dealerHidden);
|
for(const c of hand){
|
||||||
|
const vals = cardValue(c);
|
||||||
|
const newTotals = [];
|
||||||
|
for(const t of totals){
|
||||||
|
for(const v of vals){ newTotals.push(t+v); }
|
||||||
|
}
|
||||||
|
totals = Array.from(new Set(newTotals));
|
||||||
|
}
|
||||||
|
// separate <=21 and >21
|
||||||
|
const valid = totals.filter(t=>t<=21);
|
||||||
|
if(valid.length) return Math.max(...valid);
|
||||||
|
return Math.min(...totals);
|
||||||
|
}
|
||||||
|
|
||||||
dealerValueEl.textContent = dealerHidden ? "?" : handValues(dealer);
|
function renderHand(el,hand,hideFirst=false){
|
||||||
playerValueEl.textContent = handValues(player);
|
el.innerHTML='';
|
||||||
|
hand.forEach((c,i)=>{
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className='card'+(c.suit==='♥'||c.suit==='♦'?' red':'');
|
||||||
|
if(hideFirst && i===0){ div.className='card back'; div.textContent='TERSEMBUNYI'; }
|
||||||
|
else{
|
||||||
|
const top = document.createElement('div'); top.textContent = c.rank + ' ' + c.suit; // top-left
|
||||||
|
const bot = document.createElement('div'); bot.style.alignSelf='flex-end'; bot.textContent = c.rank + ' ' + c.suit; // bottom-right
|
||||||
|
div.appendChild(top); div.appendChild(bot);
|
||||||
|
}
|
||||||
|
el.appendChild(div);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
balanceEl.textContent = balance;
|
function updateUI(){
|
||||||
currentBetEl.textContent = currentBet;
|
renderHand(dealerHandEl,dealer,dealerHidden);
|
||||||
|
renderHand(playerHandEl,player,false);
|
||||||
|
dealerValueEl.textContent = dealerHidden ? '??' : 'Nilai: '+handValues(dealer);
|
||||||
|
playerValueEl.textContent = 'Nilai: '+handValues(player);
|
||||||
|
balanceEl.textContent = balance;
|
||||||
|
currentBetEl.textContent = currentBet;
|
||||||
|
}
|
||||||
|
|
||||||
hitBtn.disabled = !inRound;
|
function startRound(){
|
||||||
standBtn.disabled = !inRound;
|
if(inRound) return;
|
||||||
doubleBtn.disabled = !inRound;
|
const bet = Number(betInput.value) || 0;
|
||||||
|
if(bet <=0 || bet > balance){ alert(' BANK TIDAK CUKUP! '); return; }
|
||||||
|
currentBet = bet; balance -= bet; inRound=true; dealerHidden=true; messageEl.textContent='';
|
||||||
|
|
||||||
if (!inRound) {
|
makeDeck(); shuffle();
|
||||||
messageEl.textContent = "Pasang taruhan untuk mulai.";
|
dealer = [deck.pop(), deck.pop()];
|
||||||
}
|
player = [deck.pop(), deck.pop()];
|
||||||
}
|
|
||||||
|
|
||||||
// Mulai ronde
|
// Check for natural blackjack
|
||||||
function startRound() {
|
updateUI();
|
||||||
if (inRound) return;
|
if(handValues(player)===21){
|
||||||
|
dealerHidden=false; updateUI();
|
||||||
|
const dealerVal = handValues(dealer);
|
||||||
|
if(dealerVal===21){ // push
|
||||||
|
balance += currentBet; messageEl.textContent = 'Tie (seri). Taruhan dikembalikan.';
|
||||||
|
} else {
|
||||||
|
// player blackjack 3:2
|
||||||
|
const payout = Math.floor(currentBet * 2.5);
|
||||||
|
balance += payout; messageEl.textContent = 'Blackjack! You Win.';
|
||||||
|
}
|
||||||
|
inRound=false; currentBet=0; updateUI();
|
||||||
|
} else {
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bet = Number(betInput.value);
|
function playerHit(){
|
||||||
|
if(!inRound) return; player.push(deck.pop()); updateUI();
|
||||||
|
const val = handValues(player);
|
||||||
|
if(val>21){ // bust
|
||||||
|
dealerHidden=false; messageEl.textContent='Bust! Dealer Win!'; inRound=false; currentBet=0; updateUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (bet <= 0 || bet > balance) {
|
function playerStand(){
|
||||||
messageEl.textContent = "Taruhan tidak valid!";
|
if(!inRound) return; dealerHidden=false; // reveal dealer
|
||||||
return;
|
// dealer plays
|
||||||
}
|
while(handValues(dealer)<17){ dealer.push(deck.pop()); }
|
||||||
|
const pv = handValues(player); const dv = handValues(dealer);
|
||||||
|
if(dv>21 || pv>dv){ // player wins
|
||||||
|
const payout = currentBet*2; balance += payout; messageEl.textContent='You Win!';
|
||||||
|
} else if(pv===dv){ // push
|
||||||
|
balance += currentBet; messageEl.textContent='Tie (seri).';
|
||||||
|
} else { messageEl.textContent='Dealer Win!'; }
|
||||||
|
inRound=false; currentBet=0; updateUI();
|
||||||
|
}
|
||||||
|
|
||||||
balance -= bet;
|
function playerDouble(){
|
||||||
currentBet = bet;
|
if(!inRound) return;
|
||||||
|
if(balance < currentBet){ alert('Bank tidak cukup untuk double.'); return; }
|
||||||
|
// double: take exactly one card then stand
|
||||||
|
balance -= currentBet; currentBet = currentBet*2; player.push(deck.pop()); updateUI();
|
||||||
|
const val = handValues(player);
|
||||||
|
if(val>21){ dealerHidden=false; messageEl.textContent='Bust setelah double. Dealer Win!'; inRound=false; currentBet=0; updateUI(); return; }
|
||||||
|
playerStand();
|
||||||
|
}
|
||||||
|
|
||||||
makeDeck();
|
// Events
|
||||||
shuffle();
|
betBtn.addEventListener('click', startRound);
|
||||||
|
hitBtn.addEventListener('click', playerHit);
|
||||||
|
standBtn.addEventListener('click', playerStand);
|
||||||
|
doubleBtn.addEventListener('click', playerDouble);
|
||||||
|
newRoundBtn.addEventListener('click', ()=>{
|
||||||
|
// reset everything
|
||||||
|
if(inRound){ if(!confirm('Masih dalam ronde. Ingin reset?')) return; }
|
||||||
|
balance = 1000; currentBet=0; inRound=false; dealer=[]; player=[]; deck=[]; dealerHidden=true; messageEl.textContent='Bank di-reset.'; updateUI();
|
||||||
|
});
|
||||||
|
|
||||||
dealer = [deck.pop(), deck.pop()];
|
// keyboard shortcuts
|
||||||
player = [deck.pop(), deck.pop()];
|
window.addEventListener('keydown', e=>{
|
||||||
|
if(e.key==='h') playerHit();
|
||||||
|
if(e.key==='s') playerStand();
|
||||||
|
if(e.key==='d') playerDouble();
|
||||||
|
if(e.key==='Enter') startRound();
|
||||||
|
});
|
||||||
|
|
||||||
dealerHidden = true;
|
// initial UI
|
||||||
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();
|
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