0

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...?

3 Answers 3

1

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);
}
1
  • and how I put the bool.TryParse() in my code...? still not really understand it...
    – aolas
    Nov 19, 2011 at 1:31
1
if (datasoalnya.jawaban = true)
...
else if (datasoalnya.jawaban = false)

You might want to change that = to ==, which is also suggested as a warning by the compiler and underlined green in the IDE.

3
  • Yup, that's totally it. I was just about to post it. Take CodeCaster's answer and my answer and you've got your fix. Nov 18, 2011 at 14:01
  • I also already try that. when I use "=", the program only do the true condition of datasoal.jawaban, and not the false condition. but when I use "==", it reversed, where the false condition which is processed by the program
    – aolas
    Nov 19, 2011 at 1:15
  • If you're still having issues with this... you need to go read an intro to programming book. Nov 21, 2011 at 14:28
0

Try this:

bool? button = null;
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
        button = true;
else
if (
   (mouseState.LeftButton == ButtonState.Pressed && 
    prevMouseState.LeftButton == ButtonState.Released) &&
    (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
        button = false;
if (button != null)
{
    ((Game1)Game).SoundBank.PlayCue("1_btn_click02");
    if (datasoalnya.jawaban == button.Value) score += 100;
    noSoal++;
    if (noSoal == 11)
    {
        mouseDiBenar = true;
        noSoal = 1;
        if (datasoalnya.jawaban == button.Value) score = 0;
    }
}
1
  • I already tried this. and it still not work. it still not check the input with the answer data in the text file...
    – aolas
    Nov 18, 2011 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.