Reputation: 129
I am trying to make a question / answer level in C# for my coursework. Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Linq;
namespace frmSplashScreen
{
public partial class frmLevel3 : Form
{
public frmLevel3()
{
InitializeComponent();
}
string currentq = null;
int questionsshown = 1; //to go up to 4
int questionno = 0;
public void randomq() //new question function... generating number, using it as index, putting it on lblQ
{
Random ran = new Random();
questionno = ran.Next(20);
label1.Text = questionno.ToString(); //debug
label2.Text = gameClass.answers[questionno];//debug
lblQ.Text = gameClass.questions[questionno];
gameClass.questions.RemoveAt(questionno);
}
private void frmLevel3_Load(object sender, EventArgs e) // Load //
{
btnNext.Hide();
using (StreamReader sr = new StreamReader("l3questions.txt")) //reading all questions from text file
{
string line;
while ((line = sr.ReadLine()) != null)
{
gameClass.questions.Add(line);
}
}
using (StreamReader sr = new StreamReader("l3answers.txt")) //reading all answers from text file
{
string line;
while ((line = sr.ReadLine()) != null)
{
gameClass.answers.Add(line);
}
}
randomq();
}
private void btnNext_Click(object sender, EventArgs e) // Next Button //
{
btnNext.Hide();
btnCheck.Show();
if (questionsshown >= 4) // Checking no. of questions shown
{
frmMainMenu menu = new frmMainMenu(); //Go to Menu
this.Hide();
menu.Show();
}
else
{
questionsshown++;
randomq(); //if less than 4 questions have been shown, show another.
}
txt1a.BackColor = Color.White; //Setting txts back to normal
txt1a.Text = null;
}
private void btnCheck_Click(object sender, EventArgs e) // Check Button //
{
btnCheck.Hide();
btnNext.Show();
if (txt1a.Text.ToLower() == gameClass.answers[questionno]) //checking question
{
gameClass.score++;
txt1a.BackColor = Color.Green;
}
else
{
txt1a.BackColor = Color.Red;
}
}
}
}
gameClass is a class that stores a question list and an answer list.
My problem is that when I go to run the level, it is picking up the correct questions / answers from the list for the first 2 times but for the next two times after that, the answer that it is trying to check is something completely random from the list, not the answer that it should take from the index number in the variable questionno.
Any help would be greatly appreciated.
Upvotes: 1
Views: 64
Reputation: 585
In your randomq function you remove your question from the file, but not the answer. When you click the button though, you check in your answer test file with a matching index number. After the first loop, your text files will go out of sync, you'll have less questions than answers and their index will not match.
public void randomq() //new question function... generating number, using it as index, putting it on lblQ
{
Random ran = new Random();
questionno = ran.Next(20);
label1.Text = questionno.ToString(); //debug
label2.Text = gameClass.answers[questionno];//debug
lblQ.Text = gameClass.questions[questionno];
//Add this
someStringAnswer = gameClass.answers[questionno];
gameClass.questions.RemoveAt(questionno);
//And this
gameClass.answers.RemoveAt(questionno);
}
After this, use someStringAnswer
to look if you have the right answer in btnCheck_Click
Upvotes: 1