我做了一个琐事游戏,我必须制作一个方法(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个问题,然后一旦它询问最大数量的问题,关闭然后有一个方法返回如果用户获胜,则为true;如果不是,则为false。但我完全不知道该怎么做。
编辑:所以我知道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,否则,它是假的。但是,当我这样做时,SuccessOrFail属性仍然返回初始值false。因为在开始时,没有任何设置。
我对此代码的疑问是:这比之前的代码更好吗?有没有什么方法可以修改这段代码,以便在所有问题都得到解答后才能返回SuccessOrFail?
答案 0 :(得分:1)
根据您的问题,您可以在3-5个问题之间随机询问一些问题。所有你需要做的就是问一个问题,一旦回答了布尔值。如果您拥有正确数量的布尔值,则返回获胜游戏状态。
答案 1 :(得分:0)
首先,您需要一个私有成员变量来跟踪已经提出的问题数量,如下所示:
private int questionsAsked = 0;
然后在您的btnNext_Click
处理程序中,您将增加此总数,然后检查他们是否已回答足够的问题以在需要时关闭表单:
questionsAsked++;
if (questionsAsked >= 5) this.Hide(); // this will hide the form but keep the object so you can fetch the results after.
然后最后,要从表单中获取结果,只需声明一个公共属性以返回一个布尔值来说明用户是否赢了。你没有提到赢家的定义,所以你需要添加决定这个的表达式:
public bool IsWinner {
get {
return /* true if winner */;
}
}
我不确定你的表单周围有什么应用程序框架,但你应该引用表单对象,因此可以得到如下结果:
var winner = MyTriviaForm.IsWinner;