简体   繁体   English

如何在Delphi中跟踪点

[英]How can I keep track of points in delphi

I'm struggling to find a way to be able to keep track of points. 我正在努力寻找一种方法来跟踪积分。 I'm coding a educational game for my high school 3rd term project and my educational game is a series of multiple choice word problems, what I want to happen is that every time the user clicks on the right answer a point should be added so that when the user reaches the end page and clicks on the 'get results' button that delphi would count up all of the correct answers the user has gotten and displays them in a pop-up message. 我正在为我的高中三年级项目编写一个教育游戏,而我的教育游戏是一系列选择题问题,我想发生的事情是,每次用户单击正确的答案时都应添加一个点,以便当用户到达最终页面并单击“获取结果”按钮时,delphi将计算用户已获得的所有正确答案,并在弹出消息中显示它们。

While solution that David Heffernan suggests in his comment is easiest to implement (before moving to next question you simply check if the user answered correctly and increase points if he did before moving to next question) I would rather go for solution recommended by paulsm4. 尽管David Heffernan在他的评论中建议的解决方案最容易实现(在移至下一个问题之前,您只需检查用户是否正确回答并在回答下一个问题之前就增加了分数),我还是希望使用paulsm4建议的解决方案。 Why? 为什么?

His solution recommends storing all the answers user made which in the end gives you ability so that you can show the user which questions did he answer correctly and which not and which would be the correct answers. 他的解决方案建议存储用户做出的所有答案,这最终将使您有能力,以便可以向用户显示他正确回答了哪些问题,哪些回答不正确以及哪些是正确答案。

Now since you are making an educational game showing the user where he screwed up and his answer should be will greatly improve value and usability of your application. 现在,由于您正在制作一个教育游戏,向用户显示他所处的位置以及答案应该会大大提高应用程序的价值和可用性。

So how do you implement this: 那么如何实现呢?

First I would recommend creating a record for storing information regarding each question like so 首先,我建议创建一条记录来存储有关每个问题的信息,如下所示

RQuestion = record
  //Contains the text which represents the question
  QuestionText: String;
  //Contains text which represents first answer
  Answer1Text: String;
  //Contains text which represents second answer
  Answer2Text: String;
  //Contains text which represents third answer
  Answer3Text: String;
  //Contains text which represents fourth answer
  Answer4Text: String;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

Then in order to store required data for all questions you can simply make an array of RQuestion typed items 然后,为了存储所有问题所需的数据,您可以简单地使一组RQuestion类型的项目

ArrQuestions: Array[0..NumOfQuestions] of RQuestion;

So now you are able to access data regarding each question like this: 因此,现在您可以访问有关每个问题的数据,如下所示:

var Questions: ArrQuestions;

...

//Text of the question
QText := Questions[QNum].QuestionText;
//Text of the first answer
A1Text := Questions[Qnum].Answer1Text;
//Text of the second answer
A2Text := Questions[Qnum].Answer2Text;
//Text of the third answer
A3Text := Questions[Qnum].Answer3Text;
//Text of the fourth answer
A4Text := Questions[Qnum].Answer4Text;
//Correct answer
CorrAnsw := Questions[Qnum].CorrectAnswer;
//Chosen answer
SelAnsw := Questions[QNum].SelectedAnswer;

Then in order to calculate the number of points your user has accumulated you simply loop through the array of questions and check if the user answered correctly to them 然后,为了计算您的用户积累的分数,您只需遍历一系列问题并检查用户是否正确回答了这些问题

TotalPoints := 0;
for Qnum := 0 to NumOfQuestions do
begin
  if Questions[QNum].CorrectAnswer = Questions[QNum].SelectedAnswer then
    TotalPoints := TotalPoints+1;
end;

Now as I said before in the end you can give your user ability to review his answers and see which ones did he chose and which ones are the correct ones. 现在,正如我在最后所说的那样,您可以使您的用户能够查看他的答案,并查看他选择了哪些答案,哪些是正确的答案。

This way you improve the learning ability of your users. 这样,您可以提高用户的学习能力。


Bonus content 奖励内容

When you are using records and arrays of records in your program you gain the ability to quickly load the information in these records from a file by using of typed files. 在程序中使用记录和记录数组时,您可以使用类型化的文件从文件中快速加载这些记录中的信息。

//Assign file handle
AssignFile(QFile,'D:\Questions.dat');
//Open file in read only mode
Reset(QFile);
//Set the desired question umber that you want to read
QNume := 10;
//Move file position to the specific question
//Exact position in bytes is automatically calculated
//since we are using typed files
Seek(QFile,QNum);
//Read the data from file into specific variable which can
//Also be a record inside an array
Read(QFile,Questions[QNum]);
//Close the file handle when you are done
CloseFile(QFile);

However you should note that in order to do so your record types needs to be of fixed size. 但是,您应该注意,为此,您的记录类型必须为固定大小。 This means that every field/variable inside your record needs to have fixed size. 这意味着记录中的每个字段/变量都必须具有固定的大小。

Unfortunately in case of strings this is not so since in Delphi strings are actually referenced types and their size actually depends on the text that is stored in such string. 不幸的是,在字符串的情况下不是这样,因为在Delphi中,字符串实际上是引用的类型,并且其大小实际上取决于存储在此类字符串中的文本。

So if you would want to gain the ability to load such records directly from a typed file you would have to change all of the strings into short strings 因此,如果您想获得直接从键入文件中加载此类记录的功能,则必须将所有字符串更改为短字符串

RQuestion = record
  //Contains the text which represents the question
  QuestionText: ShortString;
  //Contains text which represents first answer
  Answer1Text: ShortString;
  //Contains text which represents second answer
  Answer2Text: ShortString;
  //Contains text which represents third answer
  Answer3Text: ShortString;
  //Contains text which represents fourth answer
  Answer4Text: ShortString;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

Now the only problem is that ShortStrings only support ANSI based characters. 现在唯一的问题是ShortStrings仅支持基于ANSI的字符。 So you may lose the ability to use some of the NON US characters in text. 因此,您可能无法在文本中使用某些NON US字符。

In case if you need to have UNICODE strings you would have to use a bit different approach. 如果需要UNICODE字符串,则必须使用其他方法。 In such approach you won't be storing the text inside the records but instead separately in a StringList . 用这种方法,您不会将文本存储在记录内,而是分别存储在StringList中。 And your records would simply store the index information about specific StringList entry that contains text specific to your question/answer 您的记录将仅存储有关特定StringList条目的索引信息,其中包含特定于您的问题/答案的文本

So the question record would now look like this 所以问题记录现在看起来像这样

RQuestion = record
  //Contains StringList index at which the question text is stored
  QuestionText: Integer;
  //Contains StringList index at which the first answer text is stored
  Answer1Text: Integer;
  //Contains StringList index at which the second answer text is stored
  Answer2Text: Integer;
  //Contains StringList index at which the third answer text is stored
  Answer3Text: Integer;
  //Contains StringList index at which the fourth answer text is stored
  Answer4Text: Integer;
  //Simple integer value to store which is the correct answer
  CorrectAnswer: Integer;
  //Simple integer value to store which answer was chosen by the user
  SelectedAnswer: Integer;
end;

So now you have fixed sized records which can be easily loaded or saved from typed files. 因此,现在您有了固定大小的记录,可以轻松地从键入的文件中加载或保存这些记录。 And you can easily load or save questions and answers texts into or from StringList by using String Lists LoadFromfile or SaveToFile methods. 而且,您可以使用字符串列表LoadFromfile或SaveToFile方法轻松地在StringList中加载或保存问题和答案文本。

And the best thing about this last approach is that you can even have multiple text files where each text file contains text written in different language which gives you easy way to design multiligual tests with your application. 最后一种方法的最好之处在于,您甚至可以拥有多个文本文件,其中每个文本文件都包含用不同语言编写的文本,这使您可以轻松地通过应用程序设计多语言测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM