【发布时间】:2021-03-16 05:16:18
【问题描述】:
我正在编写一个名为Jotto 的简单游戏。玩家试图猜测一个秘密的五个字母单词;每个字母都是独一无二的。在每个 5 个字母的猜测单词之后,您会被告知猜测中有多少个字母也在秘密单词中。这是我第一次尝试使用 Delphi 进行跨平台开发。我在 Delphi 中开发过 Windows 应用程序,并使用 Flutter 为 Android 开发了一些应用程序。
所有代码在 Windows(32 位)上按预期执行,但在 Android(64 位,SDK 25.2.5)上不正常。不会引发异常,但计算出的每轮正确猜测数是不正确且不可预测的(即,下面total 的计算结果是什么是关闭的)。通过调试器运行代码有时也会显示局部变量不正确。
以下是相关功能:
function TJotto.OccurrencesOfChar(const aWord, aChar: string): integer;
var
i: integer;
begin
result := 0;
for i := 1 to Length(aWord) do
if aWord[i] = aChar then
inc(result);
end;
和
function TJotto.MakeGuess(aGuessWord: string): string;
var
i: integer;
total: integer;
wordToDisplay: string;
begin
total := 0; // number of matches
wordToDisplay := aGuessWord;
// save copy of guess before deleting duplicate letters
// because guess will be displayed
// did user solve puzzle?
if aGuessWord = FSecretWord then
Exit('You did it! The word was ' + aGuessWord);
// make sure all letters in aGuessWord are different
// otherwise a guess like 'vexed' will say an E is present in FSecretWord twice
for i := 5 downto 1 do
if OccurrencesOfChar(aGuessWord, aGuessWord[i]) > 1 then
Delete(aGuessWord, i, 1);
// go through each letter in aGuessWord to see if it's in FSecretWord
// keep a running total number of matches
for i := 1 to Length(aGuessWord) do
total := total + OccurrencesOfChar(FSecretWord, aGuessWord[i]);
result := wordToDisplay + #9 + total.ToString;
end;
【问题讨论】:
-
字符串中的字符索引?你说:通过调试器运行代码有时也会显示局部变量不正确。究竟是什么方式?
标签: android delphi delphi-10.3-rio