import { useState, useEffect } from 'react'; import { Coins, RefreshCcw, Plus, Minus, Volume2, VolumeX } from 'lucide-react'; export default function BlackjackGame() { const [playerName, setPlayerName] = useState(''); const [balance, setBalance] = useState(1000); const [bet, setBet] = useState(100); const [playerHand, setPlayerHand] = useState([]); const [dealerHand, setDealerHand] = useState([]); const [deck, setDeck] = useState([]); const [gameState, setGameState] = useState('betting'); const [message, setMessage] = useState('Place your bet to start!'); const [soundEnabled, setSoundEnabled] = useState(true); const [lastAction, setLastAction] = useState(null); const playSound = (action) => { if (!soundEnabled) return; const sounds = { card: new Audio('https://assets.mixkit.co/sfx/preview/mixkit-cards-sliding-in-stack-2001.mp3'), win: new Audio('https://assets.mixkit.co/sfx/preview/mixkit-winning-chimes-2015.mp3'), lose: new Audio('https://assets.mixkit.co/sfx/preview/mixkit-losing-bleeps-2026.mp3'), chip: new Audio('https://assets.mixkit.co/sfx/preview/mixkit-poker-chips-handling-2003.mp3') }; sounds[action]?.play().catch(() => {}); }; const createDeck = () => { const suits = ['♠', '♣', '♥', '♦']; const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']; const newDeck = []; for (let suit of suits) { for (let value of values) { newDeck.push({ suit, value }); } } return shuffle([...newDeck]); }; const shuffle = (array) => { let currentIndex = array.length; while (currentIndex !== 0) { const randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; }; const drawCard = () => { if (deck.length === 0) { setDeck(createDeck()); } return deck.pop(); }; const calculateHand = (hand) => { let sum = 0; let aces = 0; for (let card of hand) { if (card.value === 'A') { aces += 1; sum += 11; } else if (['K', 'Q', 'J'].includes(card.value)) { sum += 10; } else { sum += parseInt(card.value); } } while (sum > 21 && aces > 0) { sum -= 10; aces -= 1; } return sum; }; const startGame = () => { if (balance < bet) { setMessage("Not enough balance!"); return; } const newDeck = createDeck(); const playerCards = [newDeck.pop(), newDeck.pop()]; const dealerCards = [newDeck.pop()]; setBalance(prev => prev - bet); setDeck(newDeck); setPlayerHand(playerCards); setDealerHand(dealerCards); setGameState('playing'); setMessage('Your turn! Hit or Stand?'); playSound('card'); }; const hit = () => { const newHand = [...playerHand, drawCard()]; setPlayerHand(newHand); if (calculateHand(newHand) > 21) { setGameState('finished'); setMessage('Bust! You lose!'); playSound('lose'); } }; const stand = () => { setGameState('dealer'); let currentDealerHand = [...dealerHand]; while (calculateHand(currentDealerHand) < 17) { currentDealerHand.push(drawCard()); } setDealerHand(currentDealerHand); const dealerScore = calculateHand(currentDealerHand); const playerScore = calculateHand(playerHand); setGameState('finished'); if (dealerScore > 21) { setBalance(prev => prev + bet * 2); setMessage('Dealer bust! You win!'); playSound('win'); } else if (dealerScore > playerScore) { setMessage('Dealer wins!'); playSound('lose'); } else if (dealerScore < playerScore) { setBalance(prev => prev + bet * 2); setMessage('You win!'); playSound('win'); } else { setBalance(prev => prev + bet); setMessage('Push!'); } }; const doubleDown = () => { if (balance < bet) { setMessage("Not enough balance for double down!"); return; } setBalance(prev => prev - bet); setBet(prev => prev * 2); playSound('chip'); const newCard = drawCard(); const newHand = [...playerHand, newCard]; setPlayerHand(newHand); playSound('card'); if (calculateHand(newHand) > 21) { setGameState('finished'); setMessage('Bust! You lose!'); playSound('lose'); } else { stand(); } }; const handleNameSubmission = () => { const trimmedName = playerName.trim(); if (trimmedName.length >= 2) { setPlayerName(trimmedName); } else { setMessage("Please enter a valid name (at least 2 characters)"); } }; const renderCard = (card, index, isDealer = false) => { if (isDealer && index === 1 && gameState === 'playing') { return (
); } const isRed = card.suit === '♥' || card.suit === '♦'; const suitSymbols = { '♠': '♠', '♣': '♣', '♥': '♥', '♦': '♦' }; return (
{card.value}
{suitSymbols[card.suit]}
{card.value}
{suitSymbols[card.suit]}
{suitSymbols[card.suit]}
); }; return (
{!playerName.trim() ? (

Virtual Blackjack

setPlayerName(e.target.value)} onKeyPress={(e) => { if (e.key === 'Enter') { handleNameSubmission(); } }} />
) : (
${balance}
{playerName}
{gameState === 'betting' && (
${bet}
)} {gameState !== 'betting' && (

Dealer's Hand

{dealerHand.map((card, index) => renderCard(card, index, true))}

Your Hand

{playerHand.map((card, index) => renderCard(card, index))}
{message}
{gameState === 'playing' && (
{playerHand.length === 2 && balance >= bet && ( )}
)} {gameState === 'finished' && (
)}
)}
)}
); }