How to make code run a certain amount of times before returning something?
Posted
by
user3564967
on Stack Overflow
See other posts from Stack Overflow
or by user3564967
Published on 2014-06-10T09:02:09Z
Indexed on
2014/06/10
9:24 UTC
Read the original article
Hit count: 222
I made a trivia game and I have to make a method (SuccessOrFail) that will return whether the user beat the trivia or not.
namespace D4
{
/// <summary>
/// Displays the trivia and returns whether the user succeeded or not, number of questions asked, and a free piece of trivia.
/// </summary>
public partial class TriviaForm : Form
{
private Trivia trivia;
private Question question;
private Random rand = new Random();
private HashSet<int> pickedQuestion = new HashSet<int>();
private string usersAnswer;
private int numCorrectAnswers;
private int numIncorrectAnswers;
public TriviaForm()
{
InitializeComponent();
this.trivia = new Trivia();
QuestionRandomizer();
QuestionOutputter();
}
/// <summary>
/// This method will return true if succeeded or false if not.
/// </summary>
/// <returns>Whether the user got the trivia right or not</returns>
public bool SuccessOrFail(bool wumpus)
{
bool successOrFail = false;
int maxQuestions = 3;
if (wumpus == true)
maxQuestions = 5;
int numNeededCorrect = maxQuestions / 2 + 1;
if (this.usersAnswer == question.CorrectAnswer.ToString())
numCorrectAnswers++;
else
numIncorrectAnswers++;
if (numCorrectAnswers + numIncorrectAnswers == maxQuestions)
{
if (numCorrectAnswers == numNeededCorrect)
successOrFail = true;
else
successOrFail = false;
numCorrectAnswers = 0;
numIncorrectAnswers = 0;
return successOrFail;
}
else
return false;
}
/// <summary>
/// This method will output a free answer to the player.
/// </summary>
public string FreeTrivia()
{
return question.Freetrivia;
}
// This method tells the player whether they were correct or not.
private void CorrectOrNot()
{
if (this.usersAnswer == question.CorrectAnswer.ToString())
MessageBox.Show("Correct");
else
MessageBox.Show("Incorrect");
}
// Displays the questions and answers on the form.
private void QuestionOutputter()
{
this.txtQuestion.Text = question.QuestionText;
this.txtBox0.Text = question.Answers[0];
this.txtBox1.Text = question.Answers[1];
this.txtBox2.Text = question.Answers[2];
this.txtBox3.Text = question.Answers[3];
}
// Clears the TextBoxes and displays a new random question.
private void btnNext_Click(object sender, EventArgs e)
{
this.usersAnswer = txtAnswer.Text;
CorrectOrNot();
this.txtQuestion.Clear();
this.txtBox0.Clear();
this.txtBox1.Clear();
this.txtBox2.Clear();
this.txtBox3.Clear();
this.txtAnswer.Clear();
this.txtAnswer.Focus();
QuestionRandomizer();
QuestionOutputter();
this.txtsuc.Text = SuccessOrFail(false).ToString();
}
// Choose a random number and assign the corresponding data to question, refreshes the list if all questions used.
private void QuestionRandomizer()
{
if (pickedQuestion.Count < trivia.AllQuestions.Count)
{
int random;
do
{
random = rand.Next(trivia.AllQuestions.Count);
} while (pickedQuestion.Contains(random));
pickedQuestion.Add(random);
this.question = trivia.AllQuestions.ToArray()[random];
if (pickedQuestion.Count == trivia.AllQuestions.ToArray().Length)
pickedQuestion.Clear();
}
}
}
}
My question is how to make it so that the code asks the user 3 or 5 questions and then returns whether the user won or not?
I was wondering if somehow I could make a public void that would just make the form pop up and ask the user 3 to 5 questions and then once it asks the maximum number of questions, to close and then have a method that returns true if the user won, or false if they didn't. But I literally have no idea how to do that.
Edit: So I know a for loop can make code run more than once. But the problem I'm having is, is that I don't know how to make it so that the trivia game asks 3 to 5 questions BEFORE returning something.
© Stack Overflow or respective owner