From 773fb66806036bf91b5e8821099016fb45b3e5a5 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sun, 4 Jul 2021 23:07:38 +0300 Subject: [PATCH] answer randomizer --- app/js/game.js | 7 +++++++ app/js/index.js | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/js/game.js b/app/js/game.js index 18478ec..b9b0597 100644 --- a/app/js/game.js +++ b/app/js/game.js @@ -14,3 +14,10 @@ export function nextQuest(questions, questIndex, answer) { 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]]; + } +} diff --git a/app/js/index.js b/app/js/index.js index b6ba87d..6c372ae 100644 --- a/app/js/index.js +++ b/app/js/index.js @@ -5,7 +5,7 @@ import gameData from '../gameData.json'; // game data import { getMousePos, isInside } from './buttons.js'; import { clearContext, getCenterH, getCenterV } from './draw.js'; -import { nextQuest } from './game.js'; +import { nextQuest, shuffleQuestAnswer } from './game.js'; // Engine variables ------------------------------------- let DEBUG = true; @@ -41,6 +41,7 @@ window.onload = function() { totalRightAnswers: 0, // количество правильных ответов }; game.quest = gameData[game.questIndex]; + shuffleQuestAnswer(gameData[game.questIndex].answer); // присваем всем квестам статус не выполнен gameData.forEach(element => element.status = null); @@ -85,8 +86,9 @@ window.onload = function() { // click by first answer button if (isInside(mousePos, button.answerButtons[0])) { if (nextQuest(gameData, game.questIndex, button.answerButtons[0].data)) { - game.quest = gameData[game.questIndex += 1]; + game.quest = gameData[game.questIndex += 1]; // костыль game.totalRightAnswers += 1; + shuffleQuestAnswer(gameData[game.questIndex].answer); if (DEBUG) { console.log("Wow, right answer!!"); @@ -102,6 +104,7 @@ window.onload = function() { if (nextQuest(gameData, game.questIndex, button.answerButtons[1].data)) { game.quest = gameData[game.questIndex += 1]; game.totalRightAnswers += 1; + shuffleQuestAnswer(gameData[game.questIndex].answer); if (DEBUG) { console.log("Wow, right answer!!"); @@ -117,6 +120,7 @@ window.onload = function() { if (nextQuest(gameData, game.questIndex, button.answerButtons[2].data)) { game.quest = gameData[game.questIndex += 1]; game.totalRightAnswers += 1; + shuffleQuestAnswer(gameData[game.questIndex].answer); if (DEBUG) { console.log("Wow, right answer!!"); @@ -132,6 +136,7 @@ window.onload = function() { if (nextQuest(gameData, game.questIndex, button.answerButtons[3].data)) { game.quest = gameData[game.questIndex += 1]; game.totalRightAnswers += 1; + shuffleQuestAnswer(gameData[game.questIndex].answer); if (DEBUG) { console.log("Wow, right answer!!");