Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.1 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);
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() {
    const playerHandContainer = document.getElementById('player-hand');
    const dealerHandContainer = document.getElementById('dealer-hand');
    playerHandContainer.innerHTML = 'La tua mano:';
    dealerHandContainer.innerHTML = 'Mano del banco:';

    playerHand.forEach(card => {
        const cardElement = createCardElement(card);
        playerHandContainer.appendChild(cardElement);
    });

    dealerHand.forEach(card => {
        const cardElement = createCardElement(card);
        dealerHandContainer.appendChild(cardElement);
    });
}

function createCardElement(card) {
    const cardElement = document.createElement('div');
    cardElement.className = 'card';
    cardElement.style.backgroundImage = `url('images/${card.value}_of_${card.suit}.png')`;
    return cardElement;
}

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);
    // Aggiorna il saldo in base al risultato e gestisci il reset del gioco
}
Leave a Comment