HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container text-white mx-auto w-full max-w-xl mt-10 p-4 rounded-2xl">
<h1 class="text-2xl text-center font-bold mb-6">BitNile Bingo Verifier</h1>
<label for="serverSeed">Server Seed</label>
<input id="serverSeed" type='text' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />
<label for="clientSeed">Client Seed</label>
<input id="clientSeed" type='text' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />
<label for="nonce">Nonce</label>
<input id="nonce" type='number' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />
<label for="numCards">Number of Cards</label>
<input id="numCards" type='number' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />
<div id="verify" class='bg-white hover:bg-white/90 cursor-pointer text-center p-3 rounded-xl mt-3 text-black text-md font-medium'>
Verify Game
</div>
<div id="result" class='my-6'>
</div>
</div>
body, input, select {
background: #2a2e31;
}
.container {
background: #1B1B1D;
}
label {
color: #aaa;
}
import lodash from "https://esm.sh/lodash";
const createHmacString = (privateKey, message) => {
const key = CryptoJS.enc.Utf8.parse(privateKey);
const hmac = CryptoJS.HmacSHA256(message, key);
return CryptoJS.enc.Hex.stringify(hmac);
}
function* byteGenerator({
serverSeed,
clientSeed,
nonce,
cursor,
}: {
serverSeed: string;
clientSeed: string;
nonce: number;
cursor: number;
}) {
let currentRound = Math.floor(cursor / 32);
let currentRoundCursor = cursor;
currentRoundCursor -= currentRound * 32;
while (true) {
const message = `${clientSeed}:${nonce}:${currentRound}`;
const hmacHex = createHmacString(serverSeed, message);
const buffer = CryptoJS.enc.Hex.parse(hmacHex);
while (currentRoundCursor < 32) {
yield Number(buffer.words[Math.floor(currentRoundCursor / 4)] >> (8 * (3 - (currentRoundCursor % 4))) & 0xff);
currentRoundCursor += 1;
}
currentRoundCursor = 0;
currentRound += 1;
}
}
function generateFloats({
serverSeed,
clientSeed,
nonce,
cursor,
count,
}: {
serverSeed: string;
clientSeed: string;
nonce: number;
cursor: number;
count: number;
}): number[] {
const generator = byteGenerator({ serverSeed, clientSeed, nonce, cursor });
const bytes = [];
while (bytes.length < count * 4) {
const nextValue = generator.next().value;
if (typeof nextValue === "number") {
bytes.push(nextValue);
} else {
throw new Error("Generator did not yield a number");
}
}
return lodash.chunk(bytes, 4).map((bytesChunk) => {
return bytesChunk.reduce((result, value, i) => {
const divider = 256 ** (i + 1);
const partialResult = value / divider;
return result + partialResult;
}, 0);
});
}
function generateBingoCards(numberOfBingoCards: number, seeds: Seeds): Promise<BingoCard[]> {
const cards: BingoCard[] = [];
let floatIndex = 0;
const floats = generateFloats({
serverSeed: seeds.serverSeed,
clientSeed: seeds.clientSeed,
nonce: seeds.nonce,
cursor: 76, // Starting cursor
count: numberOfBingoCards * 50, // Number of random floats to generate
});
// Helper function to generate unique random numbers within a range
const getRandomNumbers = (min: number, max: number, count: number): number[] => {
const numbers = new Set<number>();
while (numbers.size < count) {
const newNumber = Math.floor(floats[floatIndex] * (max - min + 1)) + min;
if (!numbers.has(newNumber)) {
numbers.add(newNumber);
}
floatIndex++;
}
return Array.from(numbers);
};
for (let i = 0; i < numberOfBingoCards; i++) {
const card = {
id: i,
b: getRandomNumbers(1, 15, 5), // Column B (1-15)
i: getRandomNumbers(16, 30, 5), // Column I (16-30)
n: getRandomNumbers(31, 45, 4), // Column N (31-45), only 4 numbers (free space in middle)
g: getRandomNumbers(46, 60, 5), // Column G (46-60)
o: getRandomNumbers(61, 75, 5), // Column O (61-75)
};
// Add the free space (-1) in the middle of the "n" column (index 2)
card.n.splice(2, 0, -1);
cards.push(card);
}
return cards;
}
function generateNumbers(seeds: Seeds): Promise<number[]> {
// Generate 75 random floats using the provided generateFloats function
const floats = generateFloats({
serverSeed: seeds.serverSeed,
clientSeed: seeds.clientSeed,
nonce: seeds.nonce,
cursor: 0, // Starting cursor
count: 75, // Number of random floats to generate
});
// Create an array of numbers from 1 to 75
const numbers = Array.from({ length: 75 }, (_, i) => i + 1);
// Pair each number with its corresponding float
const paired = numbers.map((number, index) => ({
number,
float: floats[index],
}));
// Sort the paired array based on the random floats to shuffle the numbers
paired.sort((a, b) => a.float - b.float);
// Extract the sorted numbers into a new array
const shuffledNumbers = paired.map((pair) => pair.number);
return shuffledNumbers;
}
document.getElementById('verify').onclick = () => {
const serverSeed = (document.getElementById('serverSeed') as HTMLInputElement).value;
const nonce = Number((document.getElementById('nonce') as HTMLInputElement).value);
const clientSeed = (document.getElementById('clientSeed') as HTMLInputElement).value;
const numCards = Number((document.getElementById('numCards') as HTMLInputElement).value);
// Input validation
if (!serverSeed) return error('Server seed is required');
if (isNaN(nonce)) return error('Nonce must be a number');
if (!clientSeed) return error('Client seed is required');
if (!numCards) return error('Number of cards is required');
if (numCards > 8) return error('Number of cards exceeds the maximum allowed');
// Generate Bingo cards and called numbers
const cards = generateBingoCards(numCards, { serverSeed, clientSeed, nonce });
const numbers = generateNumbers({ serverSeed, clientSeed, nonce });
// Function to create HTML table for a single Bingo card
const createCardHTML = (card: BingoCard) => {
return `
<div class="bingo-card" style="display: inline-block; margin: 10px;">
<h3>Card ${card.id + 1}</h3>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>B</th>
<th>I</th>
<th>N</th>
<th>G</th>
<th>O</th>
</tr>
</thead>
<tbody>
${[0,1,2,3,4].map(row => `
<tr>
<td>${card.b[row]}</td>
<td>${card.i[row]}</td>
<td>${card.n[row] === -1 ? 'FREE' : card.n[row]}</td>
<td>${card.g[row]}</td>
<td>${card.o[row]}</td>
</tr>
`).join('')}
</tbody>
</table>
</div>
`;
};
// Generate HTML for all cards
const cardsHTML = cards.map(createCardHTML).join('');
// Generate HTML for called numbers
const numbersHTML = numbers.join(', ');
// Update the result container with formatted HTML
(document.getElementById('result') as HTMLElement).innerHTML = `
<div class="text-center">
<div class="text-gray-300">
<h2>Bingo Cards:</h2>
<div>${cardsHTML}</div>
<h2>Called Numbers:</h2>
<div>${numbersHTML}</div>
</div>
</div>
`;
};
Also see: Tab Triggers