Search code examples
c#if-statementxnastreamreaderstring-parsing

XNA quiz game answer checker


right now I'm creating a true-false quiz game. the question, image for the question, and answer are stored in text file. so the main program only display the question & images directed from that text file. and when player input the answer, by clicking True or False button, the program will check it with the answer for that question in the text file.

the text file is something like this :

    question 1, TRUE, image1
    question 2, FALSE, image2
    question 3, FALSE, image3
    question 4, TRUE, image4
    question 5, TRUE, image5
    question 6, TRUE, image6
    question 7, FALSE, image7
    question 8, TRUE, image8
    question 9, FALSE, image9
    question 10, FALSE, image10

and this is how I read the datas from text file :

        StreamReader sr = new StreamReader("data.txt");
        string line;

       while ((line = sr.ReadLine()) != null)
        {
            string[] parts = line.Split(',');
            Soal datasoalnya = new Soal();
            datasoalnya.soal = parts[0];

            if (parts[1] == "TRUE")
            {
                datasoalnya.jawaban = true;
            }
            else if (parts[1] == "FALSE")
            {
                datasoalnya.jawaban = false;
            }

            datasoalnya.tempatImage = parts[2];
            soalList.Add(datasoalnya);

        }

and this is how I check the answer:

        if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
            (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
        {
            ((Game1)Game).SoundBank.PlayCue("1_btn_click02");

            if (datasoalnya.jawaban = true)
            {
                score += 100;                   
                noSoal++;
                if (noSoal == 11)
                {
                    mouseDiBenar = true;
                    noSoal = 1;
                    score = 0;
                }

            }

            else if (datasoalnya.jawaban = false)
            {
                noSoal++;
                if (noSoal == 11)
                {
                    mouseDiBenar = true;
                    noSoal = 1;
                }

            }

        }

        else if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
            (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
        {
            ((Game1)Game).SoundBank.PlayCue("1_btn_click02");
            if (datasoalnya.jawaban = true)
            {                    
                noSoal++;
                if (noSoal == 11)
                {
                    mouseDiBenar = true;
                    noSoal = 1;
                    score = 0;
                }

            }

            else if (datasoalnya.jawaban = false)
            {
                score += 100;
                noSoal++;
                if (noSoal == 11)
                {
                    mouseDiBenar = true;
                    noSoal = 1;
                }

            }
        }

the problem is whenever player input the answer, the program not checked the condition of that input, wheter it true or false. like whenever player click correct button, the score always multiplied by 100 wheter it true or false in the text file.

how I can make it so the program can check the answer...?


Solution

  • There's an issue in your answer checking too probably, but you are definately having leading/trailing spacing issues when you are reading in the file. You should Trim() your split parts up. So this is probably only half of the answer that you need.

    You're parts array currently looks like this.

    parts[0] = "question 1"
    parts[1] = " TRUE"
    parts[2] = " image1"
    

    Swapping your code to this will help that.

    StreamReader sr = new StreamReader("data.txt");
    string line;
    
    while ((line = sr.ReadLine()) != null)
    {
      string[] parts = line.Split(',');
      Soal datasoalnya = new Soal();
      datasoalnya.soal = parts[0].Trim();
    
      string correctAnswer = parts[1].Trim()
      if (correctAnswer  == "TRUE")
      {
        datasoalnya.jawaban = true;
      }
      else if (correctAnswer  == "FALSE")
      {
        datasoalnya.jawaban = false;
      }
    
      datasoalnya.tempatImage = parts[2].Trim();
      soalList.Add(datasoalnya);
    }
    

    As a side note, I would recommend using bool.TryParse() whenever you're converting a string to a bool. It will make your code cleaner and less error approne. This is what it would look like with a try parse.

    StreamReader sr = new StreamReader("data.txt");
    string line;
    
    while ((line = sr.ReadLine()) != null)
    {
      string[] parts = line.Split(',');
      Soal datasoalnya = new Soal();
      datasoalnya.soal = parts[0].Trim();
    
      bool isCorrectAnswer;
      if(bool.TryParse(parts[1], out isCorrectAnswer)
      {
        datasoalnya.jawaban = isCorrect;
      }
      else
      {
        // unable to parse, add logic to recover or error out
      }
    
      datasoalnya.tempatImage = parts[2].Trim();
      soalList.Add(datasoalnya);
    }