JSFiddle - React, Tailwind, and code Playground
by Ben Gillbanks
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BeepBattles Prototype</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="game-container">
<section class="game-info info">
<h1>BeepBattles Prototype</h1>
<div id="spinner">
<h3>Attribute</h3>
<p id="current-attribute">Attack</p>
</div>
<div id="scoreboard">
<h3>Scores</h3>
<p>Player 1: <span id="player1-score">0</span></p>
<p>Player 2: <span id="player2-score">0</span></p>
</div>
</section>
<!-- Player Areas -->
<section class="play-info info">
<div id="player1-area" class="player-area">
<h2>Player 1</h2>
<div id="player1-hand" class="hand"></div>
<div id="player1-ability"></div>
</div>
<div id="player2-area" class="player-area">
<h2>Player 2</h2>
<div id="player2-hand" class="hand"></div>
<div id="player2-ability"></div>
</div>
</section>
<button onclick="play()" class="play-hand">Play</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 0;
}
* { margin: 0; padding: 0; }
.player-area {
margin: 20px;
}
.info {
display: flex;
gap: 20px;
}
.card {
display: flex;
flex-direction: column;
text-align: left;
font-size: 14px;
}
.card span {
display: block;
width: 100%;
gap:5px;
}
.card span.points {
font-weight: bold;
}
.card span.name {
color: green;
text-transform: uppercase;
}
.card span.selected {
font-weight: bold;
color: blue;
}
.hand {
display: flex;
justify-content: center;
margin: 10px 0;
gap: 40px;
}
#spinner, #scoreboard {
font-size: 12px;
}
.play-hand {
padding: 5px 10px;
}
JavaScript
// Card and attribute data
const attributes = ["Attack", "Defense", "Speed", "Magic"];
let player1Score = 0, player2Score = 0;
const createCard = (name, attack, defense, speed, magic, points) => ({ name, attack, defense, speed, magic, points });
const createAbilityCard = (name, effect) => ({ name, effect });
let player1Deck = [], player2Deck = [];
let player1Hand = [], player2Hand = [];
let abilityDeck = [], player1Ability = null, player2Ability = null;
// Function to shuffle a deck
const 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]];
}
};
// Draw cards for a player
function drawCards(player, count = 3) {
const hand = player === 'player1' ? player1Hand : player2Hand;
const deck = player === 'player1' ? player1Deck : player2Deck;
hand.length = 0; // Reset hand
for (let i = 0; i < count && deck.length > 0; i++) {
hand.push(deck.pop());
}
updateHandHTML(player);
}
// Draw an ability card for a player
function drawAbilityCard(player) {
if (abilityDeck.length > 0) {
const abilityCard = abilityDeck.pop();
if (player === 'player1') {
player1Ability = abilityCard;
updateAbilityHTML('player1');
} else {
player2Ability = abilityCard;
updateAbilityHTML('player2');
}
}
}
// Update player's hand HTML
function updateHandHTML(player) {
const playerHandElement = document.getElementById(`${player}-hand`);
const hand = player === 'player1' ? player1Hand : player2Hand;
playerHandElement.innerHTML = hand.map((card, index) => `
<div class="card">
<input type="radio" name="${player}-card" value="${index}" id="${player}-card-${index}">
<label for="${player}-card-${index}">
<span class="name">${card.name}</span>
${Object.entries(card).filter(([key]) =>...