Here's a complete Blackjack game in a single HTML file. It includes HTML structure, CSS styling, and JavaScript logic for a simple 1v1 Blackjack game against the computer.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blackjack Game</title>
<style>
body {
font-family: Arial, sans-serif;
background: #222;
color: #fff;
text-align: center;
padding: 20px;
}
.game-container {
max-width: 800px;
margin: 0 auto;
}
.hand {
margin: 20px 0;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.card {
width: 100px;
height: 140px;
background: #fff;
color: #000;
border: 2px solid #333;
border-radius: 8px;
margin: 5px;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 1.2em;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
}
.dealer {
opacity: 0.5;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
}
#status {
margin-top: 20px;
font-size: 1.2em;
font-weight: bold;
}
</style>
</head>
<body>
<div class="game-container">
<h1> Blackjack </h1>
<div id="dealer-hand" class="hand dealer"></div>
<div id="player-hand" class="hand"></div>
<div id="status"></div>
<button onclick="hit()">Hit</button>
<button onclick="stand()">Stand</button>
<button onclick="newGame()">New Game</button>
</div>
<script>
const suits = ['♠', '♥', '♦', '♣'];
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let deck = [];
let playerHand = [];
let dealerHand = [];
let gameActive = false;
function createDeck() {
deck = [];
for (let suit of suits) {
for (let value of values) {
deck.push({ suit, value });
}
}
shuffleDeck();
}
function shuffleDeck() {
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 dealCard(hand) {
const card = deck.pop();
hand.push(card);
return card;
}
function getScore(hand) {
let score = 0;
let aces = 0;
for (let card of hand) {
if (card.value === 'A') {
aces++;
score += 11;
} else if (['J', 'Q', 'K'].includes(card.value)) {
score += 10;
} else {
score += parseInt(card.value);
}
}
while (score > 21 && aces > 0) {
score -= 10;
aces--;
}
return score;
}
function updateDisplay() {
const dealerDiv = document.getElementById('dealer-hand');
dealerDiv.innerHTML = '';
dealerDiv.classList.add('dealer');
// Show one dealer card
if (dealerHand.length > 0) {
const firstCard = document.createElement('div');
firstCard.className = 'card';
firstCard.textContent = dealerHand[0].value + dealerHand[0].suit;
dealerDiv.appendChild(firstCard);
// Hide other cards
for (let i = 1; i < dealerHand.length; i++) {
const card = document.createElement('div');
card.className = 'card';
card.textContent = '??';
dealerDiv.appendChild(card);
}
}
const playerDiv = document.getElementById('player-hand');
playerDiv.innerHTML = '';
for (let card of playerHand) {
const cardDiv = document.createElement('div');
cardDiv.className = 'card';
cardDiv.textContent = card.value + card.suit;
playerDiv.appendChild(cardDiv);
}
const statusDiv = document.getElementById('status');
statusDiv.textContent = '';
}
function hit() {
if (!gameActive) return;
const card = dealCard(playerHand);
updateDisplay();
const playerScore = getScore(playerHand);
if (playerScore > 21) {
endGame('You busted! Dealer wins.');
}
}
function stand() {
if (!gameActive) return;
gameActive = false;
// Dealer's turn
while (getScore(dealerHand) < 17) {
dealCard(dealerHand);
}
updateDisplay();
const playerScore = getScore(playerHand);
const dealerScore = getScore(dealerHand);
if (dealerScore > 21) {
endGame('Dealer busted! You win!');
} else if (playerScore > dealerScore) {
endGame('You win!');
} else if (playerScore < dealerScore) {
endGame('Dealer wins!');
} else {
endGame("It's a tie!");
}
}
function endGame(message) {
document.getElementById('status').textContent = message;
}
function newGame() {
createDeck();
playerHand = [];
dealerHand = [];
gameActive = true;
// Deal initial cards
dealCard(playerHand);
dealCard(dealerHand);
dealCard(playerHand);
dealCard(dealerHand);
updateDisplay();
document.getElementById('status').textContent = 'Your turn!';
}
// Initialize game
newGame();
</script>
</body>
</html>
The game follows standard Blackjack rules with a few simplifications for demonstration purposes. You can expand it with features like betting, multiple players, or sound effects.