Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.7 kB
3
Indexable
Never
let balance = 500;
let bet = 0;
let playerHand = [];
let dealerHand = [];
let deck = [];

document.getElementById('balance-amount').textContent = balance;

document.getElementById('place-bet').addEventListener('click', () => {
    bet = parseInt(document.getElementById('bet-amount').value);
    if (bet > 0 && bet <= balance) {
        balance -= bet;
        document.getElementById('balance-amount').textContent = balance;
        startGame();
    } else {
        alert('Puntata non valida!');
    }
});

document.getElementById('hit').addEventListener('click', hit);
document.getElementById('stand').addEventListener('click', stand);
document.getElementById('double').addEventListener('click', double);
// Gestisci split (facoltativo)
document.getElementById('split').addEventListener('click', split);

function startGame() {
    deck = createDeck();
    shuffleDeck(deck);
    playerHand = [drawCard(), drawCard()];
    dealerHand = [drawCard(), drawCard()];

    updateHands();
    checkBlackjack();
}

function createDeck() {
    const suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
    const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
    let deck = [];
    for (let suit of suits) {
        for (let value of values) {
            deck.push({ value, suit });
        }
    }
    return deck;
}

function shuffleDeck(deck) {
    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]];
    }
}

function drawCard() {
    return deck.pop();
}

function updateHands() {
    document.getElementById('player-hand').textContent = `La tua mano: ${handToString(playerHand)}`;
    document.getElementById('dealer-hand').textContent = `Mano del banco: ${handToString(dealerHand)}`;
}

function handToString(hand) {
    return hand.map(card => `${card.value} di ${card.suit}`).join(', ');
}

function calculateHandValue(hand) {
    let value = 0;
    let aceCount = 0;
    hand.forEach(card => {
        if (['J', 'Q', 'K'].includes(card.value)) {
            value += 10;
        } else if (card.value === 'A') {
            aceCount += 1;
            value += 11;
        } else {
            value += parseInt(card.value);
        }
    });
    while (value > 21 && aceCount > 0) {
        value -= 10;
        aceCount -= 1;
    }
    return value;
}

function hit() {
    playerHand.push(drawCard());
    updateHands();
    if (calculateHandValue(playerHand) > 21) {
        endGame('Hai sballato!');
    }
}

function stand() {
    while (calculateHandValue(dealerHand) < 17) {
        dealerHand.push(drawCard());
    }
    updateHands();
    determineWinner();
}

function double() {
    if (balance >= bet) {
        balance -= bet;
        bet *= 2;
        hit();
        if (calculateHandValue(playerHand) <= 21) {
            stand();
        }
    } else {
        alert('Saldo insufficiente per raddoppiare!');
    }
}

function split() {
    // Implementazione Split
}

function checkBlackjack() {
    if (calculateHandValue(playerHand) === 21) {
        endGame('Blackjack! Hai vinto!');
    }
}

function determineWinner() {
    const playerValue = calculateHandValue(playerHand);
    const dealerValue = calculateHandValue(dealerHand);

    if (dealerValue > 21 || playerValue > dealerValue) {
        endGame('Hai vinto!');
    } else if (playerValue < dealerValue) {
        endGame('Hai perso!');
    } else {
        endGame('Pareggio!');
    }
}

function endGame(message) {
    alert(message);
    balance += bet; // Aggiungi vincite in base al risultato
    document.getElementById('balance-amount').textContent = balance;
    // Gestisci reset del gioco
}
Leave a Comment