site-perso-poly/static/js/TP6.js
Djalim Simaila 904d8daee6
All checks were successful
DEPLOY / deploy (push) Successful in 12s
feat: update navigation links and add new pages for improved user experience
- Change "Surprise :)" link to "Vibe check" across multiple HTML files for consistency and clarity.
- Add a new "about.html" page to provide information about the author.
- Introduce a new "quizz.html" page for user interaction and engagement.
- Create new project pages for "Lieutenant Champignon" and "Pong" to showcase additional projects.
- Remove outdated project pages and links to streamline the project section.
- Add new assets including images and PDFs to enhance content presentation.
- Update JavaScript functions for form validation and score calculation in the quizz page.
- Improve styling in CSS for better visual hierarchy and layout consistency.
2025-02-23 19:55:42 +01:00

194 lines
5.4 KiB
JavaScript

/*
* TP6: Roulette
* Author: SIMAILA Djalim
* Date: 2025-01-27
* Description: A roulette game
* Version: 1.0
*/
/*
* The amount of money of a bet
* @type {Number}
* @example 1
*/
var bet = 1;
var currentSpin = 0;
/*
* Array of the n next spins
* @type {Array}
* @example [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
var spinArray;
var potentialQualifyingNumbers = [];
/*
* Object containing the qualifying numbers , and the remaining spins in which they are played
* @type {Object}
* @example {10: 3, 15: 5, 20: 1}
*/
var qualifyingNumbers = {};
/**
* Array containing the wining numbers , and the winning amount
* @type {Array}
* @example [[10, 5], [15, 10], [20, 15]]
*/
var winingNumbers = [];
/**
* Array containing the qualifying number that didn't win
* @type {Array}
* @example [10,15,20]
*/
var losingNumbers = [];
var spinInterval;
function lowerBound() {
return currentSpin - 30 < 0 ? 0 : currentSpin - 30;
}
/*
* Function to generate a random array of numbers
* @param len: length of the number array
* @returns {Array} numberArray
*/
function generate_random_array(len = 30) {
let numberArray = [];
for (let i = 0; i < len; i++)
numberArray.push(Math.floor(Math.random() * 37));
return numberArray;
}
/*
* Function to check if a number is qualifying
* @param num: number to check
* @returns {Boolean} true if the number is qualifying, false otherwise
*/
function qualifyingPreprocess() {
let currentNumber = spinArray[currentSpin];
if (!spinArray.slice(lowerBound(), currentSpin).includes(currentNumber)) {
potentialQualifyingNumbers.push({
number: currentNumber,
firstOccurence: currentSpin,
secondOccurence: null,
thirdOccurence: null,
});
}
potentialQualifyingNumbers = potentialQualifyingNumbers.filter(
(potientialNumber) => currentSpin - potientialNumber.firstOccurence < 18,
);
let qualifiedIndexes = new Set();
potentialQualifyingNumbers.forEach((potentialNumber, index) => {
if (potentialNumber.number == currentNumber) {
if (potentialNumber.firstOccurence == currentSpin) return;
if (potentialNumber.secondOccurence == null)
potentialNumber.secondOccurence = currentSpin;
else if (potentialNumber.thirdOccurence == null)
potentialNumber.thirdOccurence = currentSpin;
}
if (
potentialNumber.secondOccurence != null &&
potentialNumber.thirdOccurence != null
) {
let space1 =
potentialNumber.secondOccurence - potentialNumber.firstOccurence;
let space2 =
potentialNumber.thirdOccurence - potentialNumber.secondOccurence;
if (Math.abs(space2 - space1) <= 6) {
if (
spinArray[currentSpin] !== spinArray[currentSpin - 1] &&
spinArray[currentSpin] !== spinArray[currentSpin - 2]
) {
qualifyingNumbers[potentialNumber.number] = 9;
qualifiedIndexes.add(index);
}
}
}
});
potentialQualifyingNumbers = potentialQualifyingNumbers.filter(
(_, index) => !qualifiedIndexes.has(index),
);
}
function setupGame() {
let numberOfSpins = document.getElementById("maxSpinInput").value;
if (numberOfSpins > 1000000) numberOfSpins = 1000000;
if (numberOfSpins < 1) numberOfSpins = 1;
spinArray = generate_random_array(
document.getElementById("maxSpinInput").value,
);
document.getElementById("setupGame").classList.add("hidden");
spinInterval = setInterval(() => processSpin(), 0);
}
function processSpin() {
showCurrentSpin();
// Check if the number is qualifying
qualifyingPreprocess();
for (let number in qualifyingNumbers) {
// if the number is a wining number
if (number == currentSpin) {
// Add the wining number to the wining numbers Array
winingNumbers.push([
number,
36 * bet - (9 - qualifyingNumbers[number]), // loss
]);
}
qualifyingNumbers[number] -= 1;
if (qualifyingNumbers[number] == 0) {
losingNumbers.push(number);
delete qualifyingNumbers[number];
}
}
console.log(spinArray[currentSpin]);
console.log(potentialQualifyingNumbers);
console.log(qualifyingNumbers);
console.log(winingNumbers);
console.log(losingNumbers);
currentSpin += 1;
if (currentSpin == spinArray.length) {
clearInterval(spinInterval);
showReport();
}
}
function showCurrentSpin() {
document.getElementById("currentSpin").textContent =
"Current Spin :" + spinArray[currentSpin];
document.getElementById("lastSpins").replaceChildren();
spinArray.slice(lowerBound(), currentSpin).forEach((number) => {
let numberDiv = document.createElement("div");
numberDiv.classList.add("spinNumber");
numberDiv.textContent = number;
document.getElementById("lastSpins").appendChild(numberDiv);
});
document.getElementById("spinCounter").textContent =
"" + currentSpin + "/" + spinArray.length;
}
function showReport() {
let numberOfQualifying = winingNumbers.length + losingNumbers.length;
let winrate = winingNumbers.length / numberOfQualifying;
let winpercentage = winrate * 100;
let gains = -9 * losingNumbers.length;
winingNumbers.forEach((number) => {
gains += number[1];
});
document.getElementById("nbq").textContent =
"Number of qualifying numbers : " + numberOfQualifying;
document.getElementById("winrate").textContent = "wirate : " + winrate;
document.getElementById("winpercentage").textContent =
"win percentage : " + winpercentage;
document.getElementById("gains").textContent = gains;
}