answer randomizer

This commit is contained in:
Alexander Popov 2021-07-04 23:07:38 +03:00
parent aac38ecd7f
commit 773fb66806
2 changed files with 14 additions and 2 deletions

View File

@ -14,3 +14,10 @@ export function nextQuest(questions, questIndex, answer) {
return false; return false;
} }
} }
export function shuffleQuestAnswer(answers) {
for (let i = answers.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[answers[i], answers[j]] = [answers[j], answers[i]];
}
}

View File

@ -5,7 +5,7 @@ import gameData from '../gameData.json'; // game data
import { getMousePos, isInside } from './buttons.js'; import { getMousePos, isInside } from './buttons.js';
import { clearContext, getCenterH, getCenterV } from './draw.js'; import { clearContext, getCenterH, getCenterV } from './draw.js';
import { nextQuest } from './game.js'; import { nextQuest, shuffleQuestAnswer } from './game.js';
// Engine variables ------------------------------------- // Engine variables -------------------------------------
let DEBUG = true; let DEBUG = true;
@ -41,6 +41,7 @@ window.onload = function() {
totalRightAnswers: 0, // количество правильных ответов totalRightAnswers: 0, // количество правильных ответов
}; };
game.quest = gameData[game.questIndex]; game.quest = gameData[game.questIndex];
shuffleQuestAnswer(gameData[game.questIndex].answer);
// присваем всем квестам статус не выполнен // присваем всем квестам статус не выполнен
gameData.forEach(element => element.status = null); gameData.forEach(element => element.status = null);
@ -85,8 +86,9 @@ window.onload = function() {
// click by first answer button // click by first answer button
if (isInside(mousePos, button.answerButtons[0])) { if (isInside(mousePos, button.answerButtons[0])) {
if (nextQuest(gameData, game.questIndex, button.answerButtons[0].data)) { if (nextQuest(gameData, game.questIndex, button.answerButtons[0].data)) {
game.quest = gameData[game.questIndex += 1]; game.quest = gameData[game.questIndex += 1]; // костыль
game.totalRightAnswers += 1; game.totalRightAnswers += 1;
shuffleQuestAnswer(gameData[game.questIndex].answer);
if (DEBUG) { if (DEBUG) {
console.log("Wow, right answer!!"); console.log("Wow, right answer!!");
@ -102,6 +104,7 @@ window.onload = function() {
if (nextQuest(gameData, game.questIndex, button.answerButtons[1].data)) { if (nextQuest(gameData, game.questIndex, button.answerButtons[1].data)) {
game.quest = gameData[game.questIndex += 1]; game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1; game.totalRightAnswers += 1;
shuffleQuestAnswer(gameData[game.questIndex].answer);
if (DEBUG) { if (DEBUG) {
console.log("Wow, right answer!!"); console.log("Wow, right answer!!");
@ -117,6 +120,7 @@ window.onload = function() {
if (nextQuest(gameData, game.questIndex, button.answerButtons[2].data)) { if (nextQuest(gameData, game.questIndex, button.answerButtons[2].data)) {
game.quest = gameData[game.questIndex += 1]; game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1; game.totalRightAnswers += 1;
shuffleQuestAnswer(gameData[game.questIndex].answer);
if (DEBUG) { if (DEBUG) {
console.log("Wow, right answer!!"); console.log("Wow, right answer!!");
@ -132,6 +136,7 @@ window.onload = function() {
if (nextQuest(gameData, game.questIndex, button.answerButtons[3].data)) { if (nextQuest(gameData, game.questIndex, button.answerButtons[3].data)) {
game.quest = gameData[game.questIndex += 1]; game.quest = gameData[game.questIndex += 1];
game.totalRightAnswers += 1; game.totalRightAnswers += 1;
shuffleQuestAnswer(gameData[game.questIndex].answer);
if (DEBUG) { if (DEBUG) {
console.log("Wow, right answer!!"); console.log("Wow, right answer!!");