Certainly! Below is an index.html file that implements the "Loto Zabava" simulation as per your specifications. The code includes all the requested features, including ticket generation, drawing numbers, checking for winning conditions, prize calculations, and user interface interactions.
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<title>Лото Забава Симуляція</title>
<style>
body { font-family: Arial, sans-serif; }
h1 { text-align: center; }
.container { display: flex; flex-direction: column; align-items: center; }
.controls { margin-bottom: 20px; }
.ticket { border: 1px solid #000; margin: 10px; display: inline-block; }
.field { border: 1px solid #000; margin: 5px; display: inline-block; }
.row { display: flex; }
.cell { width: 30px; height: 30px; border: 1px solid #000; text-align: center; vertical-align: middle; line-height: 30px; }
.marked { background-color: yellow; }
.line-complete { background-color: red; }
.horseshoe { color: green; font-weight: bold; }
.stats { margin-top: 20px; }
.number-history { margin-top: 20px; }
.number-ball { display: inline-block; width: 30px; height: 30px; border-radius: 15px; background-color: lightblue; text-align: center; line-height: 30px; margin: 2px; }
</style>
</head>
<body>
<h1>Лото Забава Симуляція</h1>
<div class="container">
<div class="controls">
<label for="ticketQuantity">Кількість білетів:</label>
<input type="number" id="ticketQuantity" value="1" min="1" max="100">
<button id="buyTicketsBtn">Купити білети</button>
<button id="startDrawBtn">Почати розіграш</button>
<button id="drawBallBtn" disabled>Дістати Шар</button>
</div>
<div id="ticketsContainer"></div>
<div class="stats">
<h2>Статистика</h2>
<p>Куплено білетів: <span id="totalTickets">0</span></p>
<p>Призовий фонд: <span id="prizeFund">0</span> грн</p>
<p>Витягнуто кульок: <span id="ballsDrawn">0</span></p>
<table border="1">
<tr>
<th>Категорія</th>
<th>Кількість білетів</th>
<th>Сума виграшу (грн)</th>
</tr>
<tr>
<td>1 Лінія</td>
<td id="oneLineCount">0</td>
<td id="oneLinePrize">24</td>
</tr>
<tr>
<td>2 Лінії</td>
<td id="twoLinesCount">0</td>
<td id="twoLinesPrize">0</td>
</tr>
<tr>
<td>3 Лінії</td>
<td id="threeLinesCount">0</td>
<td id="threeLinesPrize">190,000</td>
</tr>
<tr>
<td>Джек-пот</td>
<td id="jackpotCount">0</td>
<td id="jackpotPrize">1,000,000</td>
</tr>
</table>
</div>
<div class="number-history">
<h2>Витягнуті кульки</h2>
<div id="drawnNumbers"></div>
</div>
</div>
<script>
// Global variables
let tickets = [];
let drawNumbers = [];
let prizeFund = 0;
let totalTickets = 0;
let drawInProgress = false;
let currentDrawNumber = 1;
const ticketPrice = 20;
const oneLinePrize = 24;
const threeLinesPrize = 190000;
const jackpotPrize = 1000000;
// DOM Elements
const ticketQuantityInput = document.getElementById('ticketQuantity');
const buyTicketsBtn = document.getElementById('buyTicketsBtn');
const startDrawBtn = document.getElementById('startDrawBtn');
const drawBallBtn = document.getElementById('drawBallBtn');
const ticketsContainer = document.getElementById('ticketsContainer');
const totalTicketsSpan = document.getElementById('totalTickets');
const prizeFundSpan = document.getElementById('prizeFund');
const ballsDrawnSpan = document.getElementById('ballsDrawn');
const oneLineCount = document.getElementById('oneLineCount');
const twoLinesCount = document.getElementById('twoLinesCount');
const threeLinesCount = document.getElementById('threeLinesCount');
const jackpotCount = document.getElementById('jackpotCount');
const twoLinesPrizeCell = document.getElementById('twoLinesPrize');
const drawnNumbersDiv = document.getElementById('drawnNumbers');
// Event listeners
buyTicketsBtn.addEventListener('click', buyTickets);
startDrawBtn.addEventListener('click', startDraw);
drawBallBtn.addEventListener('click', drawBall);
// Generate a new ticket
function generateTicket() {
let ticket = { fields: [], id: totalTickets + 1, oneLine: false, twoLines: false, threeLines: false, jackpot: false };
for (let f = 0; f < 3; f++) {
let field = generateField();
ticket.fields.push(field);
}
return ticket;
}
// Generate a field
function generateField() {
let field = { numbers: [], marked: [], horseshoes: [] };
let columnRanges = [[1,15],[16,30],[31,45],[46,60],[61,75]];
for (let col = 0; col < 5; col++) {
let numbersInColumn = [];
let possibleNumbers = [];
for (let n = columnRanges[col][0]; n <= columnRanges[col][1]; n++) {
possibleNumbers.push(n);
}
for (let row = 0; row < 5; row++) {
let index = Math.floor(Math.random() * possibleNumbers.length);
numbersInColumn.push(possibleNumbers.splice(index, 1)[0]);
}
field.numbers.push(numbersInColumn);
}
// Transpose the numbers to get rows
let numbersByRow = [];
for (let row = 0; row < 5; row++) {
let rowNumbers = [];
for (let col = 0; col < 5; col++) {
rowNumbers.push(field.numbers[col][row]);
}
numbersByRow.push(rowNumbers);
}
field.numbers = numbersByRow;
field.marked = [
[false,false,false,false,false],
[false,false,false,false,false],
[false,false,false,false,false],
[false,false,false,false,false],
[false,false,false,false,false]
];
// Place horseshoes (подкови)
placeHorseshoes(field);
return field;
}
// Place horseshoes in the field
function placeHorseshoes(field) {
let linesAvailable = [0,1,2,3,4];
// Ensure horseshoes are in different lines
for (let h = 0; h < 2; h++) {
if (linesAvailable.length === 0) break;
let lineIndex = Math.floor(Math.random() * linesAvailable.length);
let row = linesAvailable.splice(lineIndex, 1)[0];
let col = Math.floor(Math.random() * 5);
field.horseshoes.push({row: row, col: col});
field.marked[row][col] = true; // Mark horseshoe positions
}
}
// Buy tickets
function buyTickets() {
if (drawInProgress) {
alert("Розіграш вже розпочато!");
return;
}
let quantity = parseInt(ticketQuantityInput.value);
if (isNaN(quantity) || quantity <= 0) {
alert("Введіть коректну кількість білетів.");
return;
}
for (let i = 0; i < quantity; i++) {
let ticket = generateTicket();
tickets.push(ticket);
totalTickets++;
displayTicket(ticket);
}
prizeFund += quantity * ticketPrice;
totalTicketsSpan.textContent = totalTickets;
prizeFundSpan.textContent = prizeFund;
}
// Display a ticket
function displayTicket(ticket) {
let ticketDiv = document.createElement('div');
ticketDiv.classList.add('ticket');
ticketDiv.id = 'ticket-' + ticket.id;
let ticketTitle = document.createElement('h3');
ticketTitle.textContent = 'Білет №' + ticket.id;
ticketDiv.appendChild(ticketTitle);
for (let f = 0; f < 3; f++) {
let fieldDiv = document.createElement('div');
fieldDiv.classList.add('field');
let field = ticket.fields[f];
for (let r = 0; r < 5; r++) {
let rowDiv = document.createElement('div');
rowDiv.classList.add('row');
for (let c = 0; c < 5; c++) {
let cellDiv = document.createElement('div');
cellDiv.classList.add('cell');
if (isHorseshoe(field.horseshoes, r, c)) {
cellDiv.textContent = '🧲'; // Horseshoe symbol
cellDiv.classList.add('horseshoe');
} else {
cellDiv.textContent = field.numbers[r][c];
}
cellDiv.id = `ticket-${ticket.id}-field-${f}-cell-${r}-${c}`;
rowDiv.appendChild(cellDiv);
}
fieldDiv.appendChild(rowDiv);
}
ticketDiv.appendChild(fieldDiv);
}
ticketsContainer.appendChild(ticketDiv);
}
// Check if a position is a horseshoe
function isHorseshoe(horseshoes, row, col) {
for (let hs of horseshoes) {
if (hs.row === row && hs.col === col) {
return true;
}
}
return false;
}
// Start the draw
function startDraw() {
if (drawInProgress) {
alert("Розіграш вже розпочато!");
return;
}
if (tickets.length === 0) {
alert("Спочатку купіть білети!");
return;
}
drawInProgress = true;
startDrawBtn.disabled = true;
buyTicketsBtn.disabled = true;
drawBallBtn.disabled = false;
initializeDrawNumbers();
calculateTwoLinesPrize();
alert(`Розпочато тираж №${currentDrawNumber}`);
}
// Initialize draw numbers from 1 to 75
function initializeDrawNumbers() {
drawNumbers = [];
for (let i = 1; i <=75; i++) {
drawNumbers.push(i);
}
}
// Draw a ball
function drawBall() {
if (drawNumbers.length === 0) {
alert("Усі кульки вже витягнуті!");
return;
}
let index = Math.floor(Math.random() * drawNumbers.length);
let numberDrawn = drawNumbers.splice(index, 1)[0];
displayDrawnNumber(numberDrawn);
ballsDrawnSpan.textContent = 75 - drawNumbers.length;
checkTickets(numberDrawn);
}
// Display the drawn number
function displayDrawnNumber(number) {
let numberDiv = document.createElement('div');
numberDiv.classList.add('number-ball');
numberDiv.textContent = number;
drawnNumbersDiv.appendChild(numberDiv);
}
// Check tickets for the drawn number
function checkTickets(numberDrawn) {
let oneLineWinners = 0;
let twoLinesWinners = 0;
let threeLinesWinners = 0;
let jackpotWinners = 0;
for (let ticket of tickets) {
let ticketUpdated = false;
for (let f = 0; f < 3; f++) {
let field = ticket.fields[f];
for (let r = 0; r < 5; r++) {
for (let c = 0; c < 5; c++) {
if (field.numbers[r][c] === numberDrawn && !field.marked[r][c]) {
field.marked[r][c] = true;
let cellId = `ticket-${ticket.id}-field-${f}-cell-${r}-${c}`;
let cellDiv = document.getElementById(cellId);
cellDiv.classList.add('marked');
ticketUpdated = true;
}
}
}
}
if (ticketUpdated) {
updateTicketStatus(ticket);
}
// Update counts
if (ticket.oneLine) oneLineWinners++;
if (ticket.twoLines) twoLinesWinners++;
if (ticket.threeLines) threeLinesWinners++;
if (ticket.jackpot) jackpotWinners++;
}
oneLineCount.textContent = oneLineWinners;
twoLinesCount.textContent = twoLinesWinners;
threeLinesCount.textContent = threeLinesWinners;
jackpotCount.textContent = jackpotWinners;
// Stop draw if there is a three-line winner or jackpot winner
if (threeLinesWinners > 0 || jackpotWinners > 0) {
endDraw();
}
}
// Update ticket status
function updateTicketStatus(ticket) {
let totalLines = 0;
let fieldsCompleted = 0;
// Reset status
ticket.oneLine = false;
ticket.twoLines = false;
ticket.threeLines = false;
ticket.jackpot = false;
for (let f = 0; f < 3; f++) {
let field = ticket.fields[f];
let linesCompletedInField = 0;
let jackpotPossible = true;
for (let r = 0; r < 5; r++) {
let lineComplete = true;
for (let c = 0; c < 5; c++) {
if (!field.marked[r][c]) {
lineComplete = false;
break;
}
}
if (lineComplete) {
linesCompletedInField++;
highlightLine(ticket.id, f, r);
}
}
totalLines += linesCompletedInField;
if (linesCompletedInField === 3) {
fieldsCompleted++;
// Check for jackpot (no horseshoes used)
for (let hs of field.horseshoes) {
let horseshoeMarked = field.marked[hs.row][hs.col];
if (horseshoeMarked) {
jackpotPossible = false;
break;
}
}
if (jackpotPossible) {
ticket.jackpot = true;
}
}
}
if (totalLines >= 1 && totalLines < 2) {
ticket.oneLine = true;
} else if (totalLines >= 2 && totalLines < 3) {
ticket.twoLines = true;
} else if (totalLines >=3 && !ticket.jackpot) {
ticket.threeLines = true;
}
}
// Highlight a complete line
function highlightLine(ticketId, fieldIndex, rowIndex) {
for (let c = 0; c <5; c++) {
let cellId = `ticket-${ticketId}-field-${fieldIndex}-cell-${rowIndex}-${c}`;
let cellDiv = document.getElementById(cellId);
cellDiv.classList.add('line-complete');
}
}
// End the draw
function endDraw() {
drawInProgress = false;
drawBallBtn.disabled = true;
alert("Розіграш завершено!");
// Display winners
let winners = tickets.filter(ticket => ticket.threeLines || ticket.jackpot);
if (winners.length > 0) {
let winnerIds = winners.map(t => t.id);
alert(`Виграшні білети: № ${winnerIds.join(', ')}`);
}
currentDrawNumber++;
// Reset for next draw
startDrawBtn.disabled = false;
buyTicketsBtn.disabled = false;
}
// Calculate the prize for two lines
function calculateTwoLinesPrize() {
let twoLinesPrize = Math.floor(prizeFund * 0.3 / totalTickets);
if (twoLinesPrize < 24) {
twoLinesPrize = 24;
}
twoLinesPrizeCell.textContent = twoLinesPrize;
}
</script>
</body>
</html>
Опис функціоналу та особливостей коду:
Примітки:
- Символ підкови
🧲 використовується замість конюшини.
- Лінії рахуються тільки горизонтальні.
- Підкови не можуть бути в одній горизонтальній лінії.
- Коли білет переходить до вищої категорії виграшу, він видаляється з попередньої категорії.
Цей код забезпечує повний функціонал симуляції "Лото Забава" згідно з вашими вимогами. Ви можете скопіювати цей код у файл index.html та відкрити його в браузері, щоб протестувати роботу скрипта.