Unity Quiz Game

175 views Asked by At

my new question is about the quiz I'm making in unity, how can I make it so that if the question is answered the first time it skips two duplicates, and if it is answered the second time it skips only one duplicate, I have a scriptable object and there is my list of questions, to make it easier for you to understand, it looks like this:

What is C# ?
What is C# ?
What is C# ?
What is Java ?
What is Java ?
What is Java ?
....... Next Questions

and I have a code that skips duplicates based on the correct answers, but it doesn't work the way I need it, because it needs to work the same way for each question, not to give me it constantly skips 2 then 1 2 then 1, but for each question as I said, it is answered correctly, it skips two duplicates, if it is answered correctly from the second attempt, it skips one duplicate, I hope you understand me, this is part of the code that serves that function. Public bool Odgovor control the answers,based on the counter of correct answers it will skip 2 then 1 2 then one, and for me it should go the way I explained to you

public StatusIgre StatusIgre { get { return statusIgre; } }

    public void PokreniIgru(int index)
    {
        brojacBodova = 0;
        trenutnoVreme = ogranicenjeVremena;
        ostatakZivota = 3;
        pitanja = new List<Pitanje>();




        for (int i = 0; i < sadrzajPitanja[index].pitanja.Count; i++)
        {
            pitanja.Add(sadrzajPitanja[index].pitanja[i]);

        }

        IzaberiPitanje();
        statusIgre = StatusIgre.IgraSe;
    }


    void IzaberiPitanje()
    {
        if (pitanja.Count > 0)
        {
            if (preskociDuplikate > 0)
            {
                // Skip the specified number of questions
                for (int i = 0; i < preskociDuplikate; i++)
                {
                    pitanja.RemoveAt(0);
                }
                preskociDuplikate = 0; // Reset the skip counter
            }
            izabranoPitanje = pitanja[0];
            kvizUI.PostaviPitanje(izabranoPitanje);
            pitanja.RemoveAt(0);
        }
        else
        {
            statusIgre = StatusIgre.Sledece;
            kvizUI.KrajIgre.SetActive(true);
        }
    }

    private void Update()
    {
        if (statusIgre == StatusIgre.IgraSe)
        {
            trenutnoVreme -= Time.deltaTime;
            PodesiVreme(trenutnoVreme);
        }
    }

    private void PodesiVreme(float vrednost)
    {
        TimeSpan vreme = TimeSpan.FromSeconds(vrednost);

        kvizUI.VremeTekst.text = "Vreme:" + vreme.ToString("mm':'ss");

        if (trenutnoVreme <= 0)
        {
            statusIgre = StatusIgre.Sledece;
            kvizUI.KrajIgre.SetActive(true);
        }
    }

    public bool Odgovor(string odgovoreno)



    {
        bool tacanOdgovor = false;

        if (odgovoreno == izabranoPitanje.tacanOdgovor)
        {
            tacanOdgovor = true;
            brojacBodova += 50;
            kvizUI.BodoviTekst.text = "Bodovi:" + brojacBodova;
            // Brojac tacnih odgovora za trenutno pitanje
            izabranoPitanje.tacniOdgovori++;
            

            if (izabranoPitanje.tacniOdgovori == 1)
            {
                // Preskace sledeca dva pitanja ako je odgovor tacan iz prvog pokusaja
                preskociDuplikate = 2;
            }
            else if (izabranoPitanje.tacniOdgovori == 2)
            {
                // Preskace sledeci pitanje ako je odgovor tacan iz drugog pokusaja
                preskociDuplikate = 1;
            }
            else
            {
                // Resetuje brojac tacnih odgovora i preskace duplikate
                izabranoPitanje.tacniOdgovori = 0;
                preskociDuplikate = 0;
                
            }

        }
        else
        {
            ostatakZivota--;
            kvizUI.OduzmiZivot(ostatakZivota);

            if (ostatakZivota <= 0)
            {

                statusIgre = StatusIgre.Sledece;
                kvizUI.KrajIgre.SetActive(true);
            }

            izabranoPitanje.tacniOdgovori = 0;
            preskociDuplikate = 0;
        }

        if (preskociDuplikate > 0)
        {
            for (int i = 0; i < preskociDuplikate; i++)
            {
                IzaberiPitanje();
            }
        }
        else
        {
            IzaberiPitanje();
        }



        return tacanOdgovor;
    }

I hope I managed to explain to you what I need.I know that the code should be in English, but I started my work in Serbian, because it is for a graduate thesis and I wanted it to be done entirely in Serbian.

1

There are 1 answers

0
Kozydot On
  1. Store the number of correct attempts for each question in a separate data structure, such as a dictionary.
  2. Update the number of correct attempts for each question accordingly.
  3. Modify the IzaberiPitanje() function to skip duplicates based on the number of correct attempts for each question.

First, create a dictionary to store the number of correct attempts for each question:

private Dictionary<Pitanje, int> correctAttempts;
Initialize the dictionary in the PokreniIgru function:

correctAttempts = new Dictionary<Pitanje, int>();
for (int i = 0; i < sadrzajPitanja[index].pitanja.Count; i++)
{
    pitanja.Add(sadrzajPitanja[index].pitanja[i]);
    correctAttempts[sadrzajPitanja[index].pitanja[i]] = 0;
}

Update the number of correct attempts in the Odgovor function (or wherever you're checking for correct answers):

public void Odgovor(bool isCorrect, Pitanje currentQuestion)
{
    if (isCorrect)
    {
        correctAttempts[currentQuestion]++;
    }
    // ... (Other logic for handling correct/incorrect answers)
}

Finally, modify the IzaberiPitanje() function to skip duplicates based on the number of correct attempts for each question:

public void IzaberiPitanje()
{
    Pitanje selectedQuestion = null;

    for (int i = 0; i < pitanja.Count; i++)
    {
        int skipCount = correctAttempts[pitanja[i]] == 1 ? 1 : 2;

        if (i + skipCount < pitanja.Count && pitanja[i].Equals(pitanja[i + skipCount]))
        {
            i += skipCount;
        }
        else
        {
            selectedQuestion = pitanja[i];
            break;
        }
    }

    if (selectedQuestion != null)
    {
        // ... (Set the selected question and update the UI)
    }
    else
    {
        // ... (Handle the case when there are no more questions)
    }
}