Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
const gamearea = document.querySelector('.gamearea');
const highscoreElement = createEle(gamearea, 'div', 'Highscore: 0', 'highscore');
const score = createEle(gamearea, 'div', 'Coins: 0', 'score');
const btn = createEle(gamearea, 'button', 'Spin', 'btn');
const message = createEle(gamearea, 'div', 'Place a bet and press spin', 'message');
const output = createEle(gamearea, 'div', '', 'output');
// Game variables
let game = { x: 7, y: 9, coins: 50, bets: [], eles: [], winner: false, styler: ['red', 'white'], highscore: 0 };
const total = game.x * game.y;
let ranVal = -1;
// Event listener for the spin button
btn.addEventListener('click', spinner);
// Retrieving highscore from cookies
game.highscore = parseInt(getCookie('roulettehighscore')) || 0;
updateHighscore();
// Creating the game board
createBoard();
updateScore();
// Function to enable/disable the spin button based on the betting status
function updateSpinButton() {
btn.disabled = game.bets.length === 0;
}
// Function to handle the spinning of the roulette wheel
function spinner() {
if (game.bets.length === 0) {
// Display a message if no bets are placed
message.innerHTML = 'You need to place a bet before spinning.';
return;
}
btn.disabled = true;
ranVal = Math.floor(Math.random() * total) + 1;
game.winner = ranVal - 1;
game.styler = [game.eles[ranVal - 1].style.backgroundColor, game.eles[ranVal - 1].style.color];
// Set alternating colors for all boxes
for (let i = 0; i < game.eles.length; i++) {
if (i % 2) {
game.eles[i].style.backgroundColor = 'red';
} else {
game.eles[i].style.backgroundColor = 'black';
game.eles[i].style.color = 'white';
}
}
// Check and handle winning bets
const winBets = game.bets.filter(bet => bet.number === ranVal || bet.color === game.eles[ranVal - 1].style.backgroundColor);
const totalWinAmount = winBets.reduce((total, bet) => total + (bet.color ? bet.amount * 2 : bet.amount * 40), 0);
const winAmount = totalWinAmount;
// Remove existing bet elements
const eles = output.querySelectorAll('.bet');
eles.forEach((el) => el.remove());
if (winBets.length > 0) {
game.coins += winAmount;
message.innerHTML = `Winner on ${ranVal} you won ${winAmount}`;
game.eles[ranVal - 1].style.backgroundColor = 'green';
// Updating highscore if necessary
if (game.coins > game.highscore) {
game.highscore = game.coins;
setCookie('roulettehighscore', game.highscore, 365);
updateHighscore();
}
} else {
message.innerHTML = `Lost you did not bet on ${ranVal}`;
game.eles[ranVal - 1].style.backgroundColor = 'purple';
}
// Resetting bets and updating score display
game.bets = [];
updateScore();
// Resetting bet status for each box
game.eles.forEach((el) => {
el.bet = false;
});
// Enable spin button and clear message after a short delay
setTimeout(() => {
btn.disabled = false;
message.innerHTML = 'Place a bet and press spin';
}, 1000);
}
// Function to create the game board
function createBoard() {
ranVal = -1;
// Creating boxes and adding click event listeners
for (let i = 0; i < total; i++) {
const temp = createEle(output, 'div', `${i + 1}`, 'box');
if (i % 2) {
temp.style.backgroundColor = 'red';
} else {
temp.style.backgroundColor = 'black';
temp.style.color = 'white';
}
game.eles.push(temp);
temp.bet = false;
temp.addEventListener('click', (e) => {
// Enabling the spin button when a box is clicked
btn.disabled = false;
if (game.winner) {
game.winner = false;
}
// Handling the bet on box click
const existingBet = game.bets.find(bet => bet.number === i + 1);
if (existingBet) {
existingBet.amount++;
createEle(existingBet.element, 'div', `$${existingBet.amount}`, 'bet');
game.coins--;
} else {
const bet = { number: i + 1, amount: 1, element: temp };
game.bets.push(bet);
temp.bet = true;
game.coins--;
createEle(temp, 'div', '$', 'bet');
}
// Updating the score display
updateScore();
}, true);
}
// Adding additional boxes for red and black bets
const redBox = createEle(output, 'div', 'RED', 'box');
redBox.style.backgroundColor = 'red';
redBox.addEventListener('click', () => {
placeGeneralBet('red', redBox);
});
const blackBox = createEle(output, 'div', 'BLACK', 'box');
blackBox.style.backgroundColor = 'black';
blackBox.style.color = 'white';
blackBox.addEventListener('click', () => {
placeGeneralBet('black', blackBox);
});
// Adjusting grid template columns
output.style.setProperty(`grid-template-columns`, `repeat(${game.x},1fr)`);
}
// Function to place bets on red or black
function placeGeneralBet(color, source) {
const existingBet = game.bets.find(bet => bet.color === color);
if (existingBet) {
existingBet.amount++;
createEle(existingBet.element, 'div', `$${existingBet.amount}`, 'bet');
game.coins--;
} else {
const bet = { color, amount: 1, element: source };
game.bets.push(bet);
game.coins--;
createEle(bet.element, 'div', '$', 'bet');
}
// Updating the score display
updateScore();
}
// Function to update the highscore display
function updateHighscore() {
highscoreElement.innerHTML = `Highscore: ${game.highscore}`;
}
// Function to update the score display
function updateScore() {
score.innerHTML = `Coins: ${game.coins}`;
}
// Function to create a new HTML element with specified properties and append it to a parent element
function createEle(parent, eleType, html, eleClass) {
const ele = document.createElement(eleType);
ele.innerHTML = html;
ele.classList.add(eleClass);
// Apply smaller font size for double-digit bet amounts
if (html.length > 2) {
ele.classList.add('small-font');
}
return parent.appendChild(ele);
}
// Function to set a cookie with a specified name, value, and expiration in days
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
}
// Function to retrieve the value of a cookie by name
function getCookie(name) {
const cookieName = `${name}=`;
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i].trim();
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return null;
}