我做了一个琐事游戏,我必须制作一个方法(SuccessOrFail),该方法将返回用户是否击败琐事。
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();
}
}
}
}
我的问题是如何使代码向用户询问3或5个问题,然后返回用户是否胜出?
我想知道是否可以通过某种方式使公共空隙仅使表格弹出并向用户询问3到5个问题,然后询问最大数量的问题,然后关闭该方法,如果有用户赢了,如果没有赢,则为假。但是我真的不知道该怎么做。
编辑:所以我知道一个for循环可以使代码运行多次。但是我遇到的问题是,我不知道如何制作,因此琐事游戏在返回某些内容之前会询问3到5个问题。
EditAgain:因此,我已经尝试了类似您所说(我认为)的方法,其中的代码略有不同...
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 Map map;
private Random rand = new Random();
private HashSet<int> pickedQuestion = new HashSet<int>();
private string usersAnswer;
private int numCorrectAnswers;
private int numIncorrectAnswers;
private bool successOrFail;
public TriviaForm()
{
InitializeComponent();
this.trivia = new Trivia();
this.map = new Map();
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
{
get { return this.successOrFail; } }
}
/// <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;
// The max number of questions that can be asked.
int maxQuestions = 3;
if (map.wumpus == true)
maxQuestions = 5;
// The number of questions needed to be answered correctly for the user to win.
int numNeededCorrect = maxQuestions / 2 + 1;
if (this.usersAnswer == question.CorrectAnswer.ToString())
numCorrectAnswers++;
else
numIncorrectAnswers++;
if (numCorrectAnswers + numIncorrectAnswers == maxQuestions)
{
if (numCorrectAnswers == numNeededCorrect)
this.successOrFail = true;
else
this.successOrFail = false;
numCorrectAnswers = 0;
numIncorrectAnswers = 0;
// Somehow close the form.
}
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();
}
}
}
}
因此,此代码中的主要更改只是新属性(SuccessOrFail),该属性现在通过获取successOrFail变量来返回用户是否赢了。在btnNext_Click中调用此变量,在该变量中,我使用正确/错误答案的数量来查看它们是否回答了最大数量的问题,如果回答了,并且它们具有所需的正确答案的数量,那么successOrFail变量= true,否则为false。但是,当我这样做时,SuccessOrFail属性仍然返回false的初始值。因为在开始时,没有任何设置。
我对这段代码的疑问是:这比以前的代码好吗?有什么方法可以修改此代码,以便仅在回答所有问题后才返回SuccessOrFail?
最佳答案
假设您有3到5个问题,那么您会随机问一些问题。您需要做的只是问一个问题,回答后返回一个布尔值。如果您具有正确的布尔值,请返回获胜游戏状态。