const isTrue = (currentValue) => currentValue[0] == true; function generate_perso_level() { var isEtNonAllowed = false; var isOuNonAllowed = false; var isNotAllowed = false; var isOuAllowed = false; timeEnd = 25; double = false; isTuto = false; createStage("play_container"); stage.add(layer); var logiqueCount = 0; for (let i = 0; i < colonneTot; i++) { liveColonneNumber.push([]); } for (let i = 0; i < colonneTot; i++) { for (let j = 0; j < numberPerColonne[i]; j++) { logiqueCount++; let numberRandom = getRandomArbitrary(0, arrayPorte.length); insertLogiqueColonne("logique" + logiqueCount, arrayPorte[numberRandom][1], i); } } logiques.forEach(function (element) { createLinkAuto(element.name); }); initEndGame(); timer.stop(); } function loadPerso() { arrayPorte = [ [document.querySelector("#logique1").checked, "et"], [document.querySelector("#logique2").checked, "ou"], [document.querySelector("#logique3").checked, "inv"], [document.querySelector("#logique4").checked, "etnon"], [document.querySelector("#logique5").checked, "nonou"] ]; document.querySelector("#PersoSetupPage").style.display = 'none'; document.querySelector("#playPage").style.display = 'block'; resetAllTabs(false); niveauActuel = 1; timeTot = 0; windowWidth = window.innerWidth; windowHeight = window.innerHeight; if (document.querySelector("#colonne_number_setup").value == 0) { var porteTotal = parseInt(document.querySelector("#colonne_number_setup_global").value); colonneTot = 0; while (porteTotal > 2) { porteTotal = porteTotal / 2; numberPerColonne.push(porteTotal); colonneTot++; } numberPerColonne.push(porteTotal); colonneTot++; } else { colonneTot = parseInt(document.querySelector("#colonne_number_setup").value); document.querySelectorAll(".input_refresh").forEach(function (element) { numberPerColonne.push(parseInt(element.value)); }); } arrayPorte = arrayPorte.filter(function (e) { if (e[0] === true) return e; }) generate_perso_level(); isLineCollapsing() click = 0; let i = 0; var error = false; while (checkIfEnd()) { if (i === NOMBRETEST) { error = true; break; } resetAllTabs(false); generate_perso_level(); isLineCollapsing(); i++; } if (error) { document.location.reload(true); } } function spin(button) { button.childNodes[3].classList = "fa fa-spinner"; } function stopspin(button) { button.childNodes[3].classList = "fa fa-arrow-right"; } function buttonSpin(button, type) { spin(button); if (type === 'gen') { setTimeout(calcul(true), 20); } else setTimeout(loadPerso, 1000); } document.querySelector("#colonne_number_setup_global_gen").addEventListener('input', (event) => { if (parseInt(document.querySelector("#colonne_number_setup_global_gen").value) <= 0) document.querySelector("#colonne_number_setup_global_gen").value = 1; calcul(); }); function calcul(launch) { var number_const = parseInt(document.querySelector("#colonne_number_setup_global_gen").value); var number = number_const; var col = new Array(); let i = 0; let reste = 0; while (number > 1) { number = number / 2; if (!Number.isInteger(number) && number > 1) reste = 1; if (parseInt(number) + reste != 0) col[i] = parseInt(number) + reste; reste = 0; i++; } //armonisation : if (col.reduce((a, b) => a + b, 0) > number_const) { let arm = col.reduce((a, b) => a + b, 0) - number_const; for (let i = col.length - 1; i > col.length - 1 - arm; i--) { col[i]--; } } else if (col.reduce((a, b) => a + b, 0) < number_const) { let arm = number_const - col.reduce((a, b) => a + b, 0); for (let i = col.length - 1; i > col.length - 1 - arm; i--) { col[i]++; } } document.querySelector("#colonnes_live").innerHTML = col + " | nombre de colonne : " + col.length + " | nombre total de portes : " + number_const; var matchs = []; var fork = []; var count = []; for (let i = 0; i < col.length; i++) count[i] = 0; var error = null; var warning = null; document.querySelectorAll(".div_gen_col_fork").forEach(function (element) { element.childNodes[0].innerHTML = "Ajouter un fork pour la colonne " + element.childNodes[1].value + " qui se divise en " + element.childNodes[2].value + " "; count[(parseInt(element.childNodes[1].value) - 1)] = count[(parseInt(element.childNodes[1].value) - 1)] + parseInt(element.childNodes[2].value); matchs.push([(parseInt(element.childNodes[1].value) + 1), parseInt(element.childNodes[2].value)]); fork.push([parseInt(element.childNodes[1].value), parseInt(element.childNodes[2].value)]); if (parseInt(element.childNodes[1].value) < 1 || parseInt(element.childNodes[1].value) > col.length) { error = "Problème sur le numéro de colonne"; element.childNodes[1].style.borderColor = "red"; } else { element.childNodes[1].style.borderColor = "white"; } if (count[(parseInt(element.childNodes[1].value) - 1)] > col[parseInt(element.childNodes[1].value)] * 2 || count[(parseInt(element.childNodes[1].value) - 1)] > col[parseInt(element.childNodes[1].value) - 1]) { error = "Problème nombre de porte dans la colonne"; element.childNodes[2].style.borderColor = "red"; } else { element.childNodes[2].style.borderColor = "white"; } //calcul des erreur de sorties/entrees potentiels : if (count[(parseInt(element.childNodes[1].value) - 1)] > col[parseInt(element.childNodes[1].value)]) warning = "Problèmes possible lors de la génération "; if (element.childNodes[1].value === "" || element.childNodes[1].value === null || element.childNodes[1].value === undefined || element.childNodes[2].value === "" || element.childNodes[2].value === null || element.childNodes[2].value === undefined) error = "Element(s) vide"; }); if (error !== null) { ErrorMessage(error); stopspin(document.querySelector("#generate_button_perso")); document.querySelector("#generate_button_perso").addEventListener('mouseenter', e => { document.querySelector("#generate_button_perso").style.cursor = 'not-allowed'; }); document.querySelector("#generate_button_perso").addEventListener('mouseleave', e => { document.querySelector("#generate_button_perso").style.cursor = 'default'; }); } else { ErrorMessage(""); document.querySelector("#generate_button_perso").addEventListener('mouseenter', e => { document.querySelector("#generate_button_perso").style.cursor = 'pointer'; }); document.querySelector("#generate_button_perso").addEventListener('mouseleave', e => { document.querySelector("#generate_button_perso").style.cursor = 'default'; }); } if (warning) WarningMessage(warning) else WarningMessage(""); if (launch !== undefined && launch !== null && launch === true && error === null) { generate2(col, matchs, fork); } } function addGenForkInput() { var div = document.createElement("div"); div.classList = "div_gen_col_fork"; var span = document.createElement("span"); span.innerHTML = "Ajouter un fork pour la colonne 1 qui se divise en 2"; div.appendChild(span); var input = document.createElement("input"); input.addEventListener('input', (event) => { calcul(); }); input.value = 1; input.setAttribute("type", "number"); div.append(input); input = document.createElement("input"); input.addEventListener('input', (event) => { calcul(); }); input.value = 2; input.setAttribute("type", "number"); div.append(input); var i = document.createElement("i"); i.className = "fa fa-trash"; i.onclick = function () { this.parentElement.remove(); calcul(); } div.append(i); document.querySelector("#gen_fork_div").appendChild(div); calcul(); } function tuto(niveau) { setMode("Tuto"); isTutoriel = true; windowWidth = window.innerWidth; windowHeight = window.innerHeight; var numberColonne1 = 2; var numberColonne2 = 1; var isEtNonAllowed = false; var isOuNonAllowed = false; var isNotAllowed = false; var isOuAllowed = false; var finish = false; var firstTime = []; switch (niveauActuel) { case 1: firstTime.push("et"); break; case 2: firstTime.push("inv"); break; case 3: firstTime.push("ou"); break; case 4: firstTime.push("etnon"); break; case 5: firstTime.push("nonou"); break; default: finish = true; break; } if (!finish) { isTuto = false; createStage("play_container"); stage.add(layer); colonneTot = 3; numberPerColonne = [numberColonne1, numberColonne2, 2]; var logiqueCount = 0; if (firstTime.length > 0) { numberColonne1 = 1; numberColonne2 = 0; numberPerColonne = [numberColonne1, numberColonne2]; liveColonneNumber.push([]); timeEnd = 20; insertLogiqueColonne("logique" + logiqueCount, firstTime[0], 0); if (allowedInfoPorte) { openModal('firstTime' + firstTime[0]); timer.pause(); } } logiques.forEach(function (element) { createLinkAuto(element.name); }); initEndGame("tuto"); } else { } } function createGameOne(niveau) { windowWidth = window.innerWidth; windowHeight = window.innerHeight; var numberColonne1 = 2; var numberColonne2 = 1; var isEtNonAllowed = false; var isOuNonAllowed = false; var isNotAllowed = false; var isOuAllowed = false; double = false; var firstTime = []; switch (niveauActuel) { case 1: firstTime.push("et"); break; case 2: numberColonne1 = 2; numberColonne2 = 0; timeEnd = 15; break; case 3: numberColonne1 = 2; numberColonne2 = 1; timeEnd = 15; break; case 4: numberColonne1 = 3; numberColonne2 = 1; timeEnd = 20; break; //Fin premiere serie case 5: firstTime.push("inv"); isNotAllowed = true; timeEnd = 20; break; case 6: numberColonne1 = 2; numberColonne2 = 0; isNotAllowed = true; timeEnd = 20; break; case 7: numberColonne1 = 2; numberColonne2 = 1; isNotAllowed = true; timeEnd = 20; break; case 8: numberColonne1 = 3; numberColonne2 = 1; isNotAllowed = true; timeEnd = 20; break; case 9: numberColonne1 = 4; numberColonne2 = 2; isNotAllowed = true; double = true; timeEnd = 25; break; case 10: firstTime.push("ou"); break; case 11: isNotAllowed = true; isOuAllowed = true; numberColonne1 = 3; numberColonne2 = 1; timeEnd = 20; break; case 12: case 13: case 14: isNotAllowed = true; isOuAllowed = true; numberColonne1 = 3; numberColonne2 = 1; timeEnd = 20; break; default: isEtNonAllowed = true; isOuNonAllowed = true; timeEnd = 20; numberColonne1 = 4; numberColonne2 = 4; break; } isTuto = false; createStage("play_container"); stage.add(layer); colonneTot = 3; numberPerColonne = [numberColonne1, numberColonne2, 2]; var logiqueCount = 0; if (firstTime.length > 0) { numberColonne1 = 1; numberColonne2 = 0; numberPerColonne = [numberColonne1, numberColonne2]; liveColonneNumber.push([]); timeEnd = 20; insertLogiqueColonne("logique" + logiqueCount, firstTime[0], 0); if (allowedInfoPorte) { openModal('firstTime' + firstTime[0]); timer.pause(); } } else { for (let i = 0; i < colonneTot; i++) { liveColonneNumber.push([]); } for (let i = 0; i < colonneTot; i++) { for (let j = 0; j < numberPerColonne[i]; j++) { logiqueCount++; let type = null; let x = 1; if (isOuAllowed) x++; if (isEtNonAllowed) x++; if (isOuNonAllowed) x++; if (isNotAllowed) x++; let numberRandom = getRandomArbitrary(0, x); switch (numberRandom) { case 0: type = "et"; break; case 1: type = "inv"; break; case 2: type = "ou"; break; case 3: type = "etnon"; break; case 4: type = "nonou"; break; } insertLogiqueColonne("logique" + logiqueCount, type, i); } } } logiques.forEach(function (element) { createLinkAuto(element.name); }); initEndGame(); } function generate2(col, matchs, fork, portes) { isTutoriel = false; isTuto = false; hideDiv(document.querySelector('#TestNouveauGenerateur')); showDiv(document.querySelector('#playPage')); resetAllTabs(false); if (portes === null || portes === undefined) arrayPorte = [ [document.querySelector("#logique1").checked, "et"], [document.querySelector("#logique2").checked, "ou"], [document.querySelector("#logique3").checked, "inv"], [document.querySelector("#logique4").checked, "etnon"], [document.querySelector("#logique5").checked, "nonou"] ]; else arrayPorte = portes; arrayPorte = arrayPorte.filter(function (e) { if (e[0] === true) return e; }) var numberCol = col.length; var numberPerCol = []; numberPerColonne = []; tempMatchs = []; tempFork = []; for (let i = 0; i < col.length; i++) { numberPerCol[i] = col[i]; numberPerColonne[i] = col[i]; } for (let i = 0; i < matchs.length; i++) tempMatchs[i] = matchs[i]; for (let i = 0; i < fork.length; i++) tempFork[i] = fork[i]; tempPortes = portes; tempCol = numberPerColonne; colonneTot = col.length; var compt = new Array(); var listeSame = []; let num = 1; //attribution for (let i = numberCol; i > 0; i--) { matchs.forEach(function (element) { if (element[0] == i) { for (let j = 0; j < element[1]; j++) { compt.push([i, "same" + num]); numberPerCol[i - 1]--; listeSame.push(["same" + num, null]); } num++; } }); for (let j = 0; j < numberPerCol[i - 1]; j++) compt.push([i, "normal"]); } //attribution groupe var listeLogiqueObj = new Array(); let i = 0; let colTemp = col; compt.forEach(function (element) { if (colTemp[element[0] - 1] !== 0) { var newLogiqueObj = { id: "logique" + i, col: element[0], id1: null, id2: null, id3: null, matchId1: null, matchId2: null, type: null, } if (element[1] === "normal") { newLogiqueObj.matchId1 = null; } else { newLogiqueObj.matchId1 = element[1]; } listeLogiqueObj.push(newLogiqueObj); colTemp[element[0] - 1]--; } else { var allowed = []; listeLogiqueObj.forEach(function (logique) { if (logique.col === element[0] && logique.matchId1 !== element[1]) allowed.push(logique); }); allowed[getRandomArbitrary(0, allowed.length)].matchId2 = element[1]; } i++; }); //Attribution des types //num = liste de comptatibilité let num2 = getRandomArbitrary(0, arrayPorte.length); for (x in listeLogiqueObj) { if (listeLogiqueObj[x].matchId1 !== null) { if (getElementArray(listeLogiqueObj[x].matchId1, listeSame)[1] === null) { getElementArray(listeLogiqueObj[x].matchId1, listeSame)[1] = arrayPorte[num2][1]; if (listeLogiqueObj[x].matchId2 !== null && getElementArray(listeLogiqueObj[x].matchId2, listeSame)[1] === null) { getElementArray(listeLogiqueObj[x].matchId2, listeSame)[1] = arrayPorte[num2][1]; } num2 = getRandomArbitrary(0, arrayPorte.length); } else { listeSame[x] = getElementArray(listeLogiqueObj[x].matchId1, listeSame); } } } createStage("play_container"); stage.add(layer); timeEnd = 999; for (let i = 0; i < colonneTot; i++) { liveColonneNumber.push([]); } var col = []; listeLogiqueObj.forEach(function (logique) { if (col[logique.col - 1] === null || col[logique.col - 1] === undefined || col[logique.col - 1] === 0) col[logique.col - 1] = 1; else col[logique.col - 1]++; }); numberPerColonne = col; initTimer(); //creation logique listeLogiqueObj.forEach(function (logique) { if (getTypeFromSameMatch(listeSame, logique.matchId1) === null) logique.type = arrayPorte[getRandomArbitrary(0, arrayPorte.length)][1]; else logique.type = getTypeFromSameMatch(listeSame, logique.matchId1); insertLogiqueColonne(logique.id, logique.type, logique.col - 1); }); var logiquesGen; //creation de liens entre les portes logiques : for (let col = 1; col < colonneTot; col++) { //recuperer tout les logiques : logiquesGen = new Array(); listeLogiqueObj.forEach(function (logique) { if (logique.col === col && logique.id3 === null) logiquesGen.push(logique); }); //check si y a des fork while (getNumberFork(fork, col) > 0) { fork.forEach(function (element) { if (element[0] === col) { //selection de la sortie a forker : let logiqueFork = logiquesGen[getRandomArbitrary(0, logiquesGen.length)]; //recupération des logiques d'entree : let logiqueEntre = new Array(); let sameId = null; listeLogiqueObj.forEach(function (logique) { if (logique.col === col + 1) { if (sameId === null) { sameId = logique.matchId1; logiqueEntre.push(logique); } else if (sameId === logique.matchId1) logiqueEntre.push(logique); } }); logiqueFork.id3 = "done"; var loEntre = null; //creation du lien avec fork : for (let i = 0; i < logiqueEntre.length; i++) { createLink(findLogique(logiqueFork.id), findLogique(logiqueEntre[i].id)); loEntre = findLogique(logiqueEntre[i].id); } logiques.forEach(function (logique) { if (logique.name === logiqueFork.id) createCircle(logique.x + imageWidth + ((loEntre.x - findLogique(logiqueFork.id).x) / 100 * pourcentageBreak), findLogique(logiqueFork.id).y + imageHeight / 2, logique.id3); }); fork.splice(fork.indexOf(element), 1); } }); } } //creation des autres liens : //recupération des logiques restants : for (x in listeLogiqueObj) { if (listeLogiqueObj[x].id3 === null && listeLogiqueObj[x].col !== colonneTot) { //liste des logiques possible : let logiquesGen = new Array(); listeLogiqueObj.forEach(function (logique) { if (logique.col === listeLogiqueObj[x].col + 1) { if (logique.type === 2 && checkLogiqueLines(logique.id, "id1") === false) { logiquesGen.push(logique); } else if (logique.type !== 2) { if (checkLogiqueLines(logique.id, "id1") === false || checkLogiqueLines(logique.id, "id2") === false) { logiquesGen.push(logique); } } } }); //attribution du logique le plus proche : let logiqueFinal = null; logiquesGen.forEach(function (logique) { if (logiqueFinal === null) { logiqueFinal = logique; } else if (Math.abs(findLogique(listeLogiqueObj[x].id).y - findLogique(logique.id).y) < Math.abs(findLogique(listeLogiqueObj[x].id).y - findLogique(logiqueFinal.id).y)) { logiqueFinal = logique; } }); createLink(findLogique(listeLogiqueObj[x].id), findLogique(logiqueFinal.id)); } } initEndGame(); invertLine(); checkAllSortieLogique(); } function getNumberFork(arr, col) { let num = 0; arr.forEach(function (item) { if (item[0] === col) num++; }); return num; } function getElementArray(one, array) { retour = null; array.forEach(function (element) { if ((element[0] === one && retour === null)) { retour = element; } }); return retour; } function getTypeFromSameMatch(listeMatch, sameId) { retour = null; listeMatch.forEach(function (element) { if (element[0] === sameId) retour = element[1]; }); return retour; } //a l'epoque function generateFromBottom() { var NombrecolonneTest = 2; var numberPerColonneTest = [3, 2]; var porteBool = [ [true, "et"], [true, "ou"], [true, "inv"] ]; var logiquesTest = []; for (let i = NombrecolonneTest; i > 0; i--) { for (let j = 0; j < numberPerColonneTest[i - 1]; j++) { let numberRandom = getRandomArbitrary(0, porteBool.length); logiquesTest.push([porteBool[numberRandom][1], i, j, (j + 1) * 10]); } } createLink(logiquesTest, numberPerColonneTest[numberPerColonneTest.length - 1]); } function createLinkTest(listeLogique, colonneMax) { listeLogique.forEach(function (element) { if (element[1] !== colonneMax) { console.log(element); listeLogique.forEach(function (item) { if (item[1] !== colonneMax) { console.log(item); } }); } }); } function isValid(listeLogique, colonneMax) { listeLogique.forEach(function (element) { //derniere colonne donc fin if (element[1] == colonneMax) { console.log(element); } }); }