From f2b39a9dadcf366d7b836e25ca8a56c674a2a479 Mon Sep 17 00:00:00 2001 From: Samir Badi Date: Mon, 11 Nov 2024 14:33:31 -0500 Subject: [PATCH] Peasant can finish the quiz --- Assets/Prefabs/Peasant.prefab | 7 ++++--- Assets/Scenes/Samir.unity | 17 +---------------- Assets/Scripts/Core/QuizHandler.cs | 22 ++++++++++++++++++---- Assets/Scripts/Peasant/PeasantMain.cs | 7 ++++++- Assets/Scripts/Quiz/QuestionInfo.cs | 2 ++ 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/Assets/Prefabs/Peasant.prefab b/Assets/Prefabs/Peasant.prefab index fabc51d..1ddfbd3 100644 --- a/Assets/Prefabs/Peasant.prefab +++ b/Assets/Prefabs/Peasant.prefab @@ -46,9 +46,10 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: answers: [] - initTime: 20 - minTimePerQuestion: 5 - maxTimePerQuestion: 18 + initTime: 2 + minTimePerQuestion: 0.5 + maxTimePerQuestion: 1 + successRate: 0.75 --- !u!1 &5739709613417327085 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Samir.unity b/Assets/Scenes/Samir.unity index 68d12f6..b31c4a4 100644 --- a/Assets/Scenes/Samir.unity +++ b/Assets/Scenes/Samir.unity @@ -764,21 +764,6 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 54002814} m_Modifications: - - target: {fileID: 774219910790536021, guid: 58a9a466f0501ce4d8388102500b1754, - type: 3} - propertyPath: initTime - value: 2 - objectReference: {fileID: 0} - - target: {fileID: 774219910790536021, guid: 58a9a466f0501ce4d8388102500b1754, - type: 3} - propertyPath: maxTimePerQuestion - value: 1 - objectReference: {fileID: 0} - - target: {fileID: 774219910790536021, guid: 58a9a466f0501ce4d8388102500b1754, - type: 3} - propertyPath: minTimePerQuestion - value: 0.5 - objectReference: {fileID: 0} - target: {fileID: 2684113167729998087, guid: 58a9a466f0501ce4d8388102500b1754, type: 3} propertyPath: m_Name @@ -3952,6 +3937,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: questionAmount: 20 + quizTakerAmount: 8 questionVisual: - {fileID: -959586227, guid: 19a8aecd158cac04fb8bb6de1e21ffaa, type: 3} - {fileID: -1923230478, guid: 19a8aecd158cac04fb8bb6de1e21ffaa, type: 3} @@ -3960,7 +3946,6 @@ MonoBehaviour: - {fileID: 233852084, guid: 19a8aecd158cac04fb8bb6de1e21ffaa, type: 3} seed: 0 randomGeneration: 1 - correctAnswer: [] --- !u!1 &1839156921 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Core/QuizHandler.cs b/Assets/Scripts/Core/QuizHandler.cs index 1998415..d7b9f9b 100644 --- a/Assets/Scripts/Core/QuizHandler.cs +++ b/Assets/Scripts/Core/QuizHandler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using GameOff.Peasant; using GameOff.Quiz; using UnityEngine; using Random = System.Random; @@ -12,10 +13,10 @@ namespace GameOff.Core public static QuizHandler Instance { get; private set; } [SerializeField] private int questionAmount = 20; + [SerializeField] private int quizTakerAmount = 8; [SerializeField] private List questionVisual; [SerializeField] private int seed; [SerializeField] private bool randomGeneration; - [SerializeField] private string[] correctAnswer; private readonly List ChoicePossibles = new List() { "a", "b", "c", "d", "e", "f" }; @@ -46,7 +47,6 @@ namespace GameOff.Core private void GenerateQuiz() { _questionInfos = new List(); - correctAnswer = new string[questionAmount]; for (int i = 0; i < questionAmount; i++) { @@ -58,11 +58,25 @@ namespace GameOff.Core tmp.Add(ChoicePossibles[x]); _questionInfos.Add(new QuestionInfo(i + 1, questionVisual[_random.Next(questionVisual.Count)], tmp, answer)); - correctAnswer[i] = tmp[_random.Next(amount)]; } } - public string GetAnswerAtIndex(int index) => correctAnswer[index]; + public string GetAnswerAtIndex(int index, float successRate) { + if(_random.Next(100) <= successRate * 100) + return _questionInfos[index].Answer; + return _questionInfos[index].Choices[_random.Next(_questionInfos[index].Count)]; + } + + public float GetResultPercent(string[] answer) + { + float succeed = 0; + + for (int i = 0; i < answer.Length; i++) + if (answer[i] == _questionInfos[i].Answer) + succeed++; + + return succeed / questionAmount * 100; + } public List QuestionInfos => _questionInfos; public int QuestionAmount => questionAmount; diff --git a/Assets/Scripts/Peasant/PeasantMain.cs b/Assets/Scripts/Peasant/PeasantMain.cs index ee6e771..30e0a59 100644 --- a/Assets/Scripts/Peasant/PeasantMain.cs +++ b/Assets/Scripts/Peasant/PeasantMain.cs @@ -11,6 +11,8 @@ namespace GameOff.Peasant [SerializeField] private float initTime = 20f; [SerializeField] private float minTimePerQuestion = 5f; [SerializeField] private float maxTimePerQuestion = 18f; + [Range(0, 1)] + [SerializeField] private float successRate = 0.75f; private int _indexQuestion; private float _answerTimer; @@ -29,12 +31,15 @@ namespace GameOff.Peasant if (_answerTimer <= 0) { - answers[_indexQuestion] = QuizHandler.Instance.GetAnswerAtIndex(_indexQuestion); + answers[_indexQuestion] = QuizHandler.Instance.GetAnswerAtIndex(_indexQuestion, successRate); _answerTimer = RandomQuestionTimer(); _indexQuestion++; if (_indexQuestion >= answers.Length) + { + Debug.Log($"{name}: {QuizHandler.Instance.GetResultPercent(answers)}%"); enabled = false; + } } } diff --git a/Assets/Scripts/Quiz/QuestionInfo.cs b/Assets/Scripts/Quiz/QuestionInfo.cs index 7ed35d8..57bfab6 100644 --- a/Assets/Scripts/Quiz/QuestionInfo.cs +++ b/Assets/Scripts/Quiz/QuestionInfo.cs @@ -17,5 +17,7 @@ namespace GameOff.Quiz Choices = choices; Answer = answer; } + + public int Count => Choices.Count; } } \ No newline at end of file