Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Quizz Game, Rendre les Questions Aléatoires

Discussion in 'Scripting' started by Fabz56, Sep 2, 2023.

  1. Fabz56

    Fabz56

    Joined:
    Aug 27, 2023
    Posts:
    3
    Je débute sur Unity, j'ai suivi un cours et essaie de me familiariser avec tout cela.
    Le Problème que j'essaie de régler est le fait de rendre les questions aléatoires sur un quizz.
    Les Questions restent dans l'ordre de leurs numéro.
    Merci d'avance.
    Code (CSharp):
    1. using Perangonline.PhotonQuis;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class QuestionController : MonoBehaviour {
    7.  
    8.     public static QuestionController instance;
    9.     public List<Question> _questions;
    10.     private List<List<Answer>> _answers;
    11.    
    12.  
    13.     [HideInInspector]
    14.     public int questionIndex =0;
    15.  
    16.     public int QuestionIndex
    17.     {
    18.         get
    19.         {
    20.             return questionIndex;
    21.         }
    22.  
    23.         set
    24.         {
    25.             questionIndex = value;
    26.            
    27.         }
    28.        
    29.     }
    30.  
    31.     private void Awake()
    32.     {
    33.         if (instance == null)
    34.             instance = this;
    35.     }
    36.  
    37.     public void LoadQuestions()
    38.     {
    39.         _questions = new List<Question>();
    40.         _answers = new List<List<Answer>>();
    41.        
    42.  
    43.         List<Answer> answersList = new List<Answer>();
    44.  
    45.         _questions.Add(new Question(0, "Quel est le deuxième plus grand continent ?", false));
    46.         answersList.Add(new Answer(1, "Asie", false));
    47.         answersList.Add(new Answer(2, "Europe", false));
    48.         answersList.Add(new Answer(3, "Afrique", true));
    49.         answersList.Add(new Answer(4, "Australie", false));
    50.         _answers.Add(answersList);
    51.  
    52.  
    53.         _questions.Add(new Question(22, "Quel animal est le plus gros ?", false));
    54.         answersList = new List<Answer>();
    55.         answersList.Add(new Answer(1, "Calmar Géant", false));
    56.         answersList.Add(new Answer(2, "Baleine Bleu", true));
    57.         answersList.Add(new Answer(3, "Elephant d'Afrique", false));
    58.         answersList.Add(new Answer(4, "Girafe", false));
    59.         _answers.Add(answersList);
    60.  
    61.      
    62.     }
    63.     public void Next()
    64.     {
    65.         if( QuestionIndex < _questions.Count  )
    66.         {
    67.             GameUIController.instance.ShowQuestion(_questions[QuestionIndex], _answers[QuestionIndex]);
    68.             GameUIController.instance.EnableBuzzer(true);
    69.             GameUIController.instance.EnableAnswers(false);
    70.  
    71.             QuestionIndex++;
    72.         }
    73.         else
    74.         {
    75.             GameUIController.instance.ShowResult();
    76.             GameUIController.instance.EnableBuzzer(false);
    77.             GameUIController.instance.EnableAnswers(false);
    78.         }
    79.  
    80.  
    81.     }
    82.  
    83.  
    84.     public void Next(int index)
    85.     {
    86.  
    87.         if (index < _questions.Count )
    88.         {
    89.             GameUIController.instance.ShowQuestion(_questions[index], _answers[index]);
    90.             GameUIController.instance.EnableBuzzer(true);
    91.             GameUIController.instance.EnableAnswers(false);
    92.         }
    93.         else
    94.         {
    95.             GameUIController.instance.ShowResult();
    96.             GameUIController.instance.EnableBuzzer(false);
    97.             GameUIController.instance.EnableAnswers(false);
    98.         }
    99.  
    100.            
    101.        
    102.  
    103.     }
    104. }
    105.  
    106.      
    107.        
    108.    
    109.  
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    You could just shuffle the list:
    Code (CSharp):
    1. public List<float> toShuffle = new List<float>();
    2. public List<float> shuffled = new List<float>();
    3.  
    4. // ShuffleList(toShuffle);
    5.  
    6. public void ShuffleList(List<float> list)
    7. {
    8.     List<float> temp = new List<float>();
    9.     temp.AddRange(list);
    10.  
    11.     for (int i = 0; i < list.Count; i++)
    12.     {
    13.         int index = Random.Range(0, temp.Count - 1);
    14.         shuffled.Add(temp[index]);
    15.         temp.RemoveAt(index);
    16.     }
    17. }
    or just shuffle the calls, as you can make a list of integers, that contain all the indexes in the other list, and anytime you use a question, remove that from the int list(when getting a random number). That way the original list is never tampered with, just the way you handle it is.
     
  3. Fabz56

    Fabz56

    Joined:
    Aug 27, 2023
    Posts:
    3
    I tried that but it still doesn't work, maybe I misplaced it.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,714
    This is incorrect:

    The integer Random.Range() accepts 0 to N in order to produce results from 0 to N-1

    Your shuffle will never shuffle the last element. The last element will always be returned last.

    Try it yourself:

    Code (csharp):
    1.     void Update()
    2.     {
    3.         toShuffle = new List<float>();
    4.         shuffled = new List<float>();
    5.  
    6.         for (int i = 0; i < 10; i++)
    7.         {
    8.             toShuffle.Add( i);
    9.         }
    10.  
    11.         ShuffleList( toShuffle);
    12.  
    13.         string s = "";
    14.         for (int i = 0; i < 10; i++)
    15.         {
    16.             float j = shuffled[i];
    17.             s = s + j.ToString() + ", ";
    18.         }
    19.         Debug.Log( s);
    20.     }
    And watch how 9 is always the last number. :)

    For boilerplate stuff like this you should keep a handy reference, such as this:

    https://gist.github.com/kurtdekker/6f9c33ad653d0feac22838225ba21c6c
     
    Bunny83 and Anthiese like this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Right, as random for int is exclusive. However when using "not count minus one" that I remember it had issues, either with shuffling decently or index out of bounds, can't remember.

    But to be fair that would only run if there was still an index of 0, which that random number would return. So there's technically no error with it, it just looks wrong. As that method that I've posted was made for one of my projects that needed a "list shuffle", and worked fine for all my cases.

    Sadly I am unaware if a "Unity Shuffle" method ever was invented, as all my attempts to search online for one showed you had to make your own still, or use one made by another fellow user.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,714
    I disagree. Every single time the highest number will be the last number returned.


    endswith9.png

    Every... single... time. Try it yourself. The code is posted above.

    Fix it trivially by removing the "- 1" on your second argument to Random.Range()

    Output once the change I mentioned above is done:

    Screen Shot 2023-09-10 at 8.17.55 AM.png
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I stand corrected, I can't remember for the life of me why I kept that(with that) in my notes, thanks for the heads up!
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,714
    You're welcome! And don't feel bad! Random stuff is SUPER hard to get right. Check this:

     
    wideeyenow_unity likes this.
  9. Fabz56

    Fabz56

    Joined:
    Aug 27, 2023
    Posts:
    3
    So I managed to make the questions random, but now another problem arises. I don't know how to prevent them from repeating themselves.


    Code (CSharp):
    1.  public int questionIndex = 0;
    2.  
    3.     public int QuestionIndex
    4.     {
    5.         get
    6.         {
    7.          
    8.             return questionIndex;
    9.         }
    10.  
    11.         set
    12.         {
    13.             questionIndex =  Random.Range(0,_questions.Count);
    14.            
    15.         }
    16.            
    17.     }
     
  10. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Code (CSharp):
    1. public List<float> toShuffle = new List<float>();
    2.     public List<float> shuffled = new List<float>();
    3.  
    4.     float UseDecrementList()
    5.     {
    6.         int count = shuffled.Count;
    7.         if (count == 0)
    8.         {
    9.             ShuffleList(toShuffle);
    10.             count = shuffled.Count;
    11.         }
    12.         int pull = Random.Range(0, count);
    13.         float temp = shuffled[pull];
    14.         shuffled.RemoveAt(pull);
    15.         return temp;
    16.     }
    17.  
    18.     public void ShuffleList(List<float> list)
    19.     {
    20.         List<float> temp = new List<float>();
    21.         temp.AddRange(list);
    22.  
    23.         for (int i = 0; i < list.Count; i++)
    24.         {
    25.             int index = Random.Range(0, temp.Count);
    26.             shuffled.Add(temp[index]);
    27.             temp.RemoveAt(index);
    28.         }
    29.     }