如何在 Pascal 中使用字符串集?

发布于 2024-07-13 03:42:47 字数 183 浏览 7 评论 0原文

我正在编写一个小游戏,其中询问用户他们的种族和类别。 string[5] 有五种可能的竞争,string[9] 有四种可能的类别。

如何对 pascal 进行编程以 1. 将五个种族和四个类定义为常量,2. 检查用户输入以查看输入是否在可能的种族和类内 - 而不使用多个 IF 语句?

任何提示将不胜感激。

I am writing a little game in which the user is asked for their race and class.
There are five possible races of string[5] and four possible classes of string[9].

How do I program pascal to 1. define the five races and four classes as constants, 2. check the user input to see whether the input is within the possible races and classes - without using multiple IF statements?

Any hints will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别在捏我脸啦 2024-07-20 03:42:48

由于您的输入是严格定义的,所以我的第一个问题是,您必须使用字符串作为用户输入吗? 不能给用户选择吗? 说下降? 然后您可以将每个选择映射到一个枚举。

type
  race = (rcRace0, rcRace1, rcRace2 rcRace3, rcRace4);


case race(Input) of  //input is an integer, index of the drop down list for example
  rcRace0 : //perform processing for race 0
  rcRace1 : //perform processing for race 1
  rcRace2 : //perform processing for race 2
  rcRace3 : //perform processing for race 3
  rcRace4 : //perform processing for race 4
end;

上课也一样。

Since your input is strictly defined, my first question is, must you use strings as the user input? Can you not give the user a choice? Say a drop down? Then you can map each choice to an enumeration.

type
  race = (rcRace0, rcRace1, rcRace2 rcRace3, rcRace4);


case race(Input) of  //input is an integer, index of the drop down list for example
  rcRace0 : //perform processing for race 0
  rcRace1 : //perform processing for race 1
  rcRace2 : //perform processing for race 2
  rcRace3 : //perform processing for race 3
  rcRace4 : //perform processing for race 4
end;

Same for class.

萌逼全场 2024-07-20 03:42:48

我会推荐 Steves 解决方案作为起点,但进一步使用枚举类型和集合...

type
    TRace = (rcRace0, rcRace1, rcRace2, rcRace3, rcRace4);

    TCharacterClass = (ccClass0, ccClass1, ccClass2, ccClass3);

    TCharacterClassSet = set of TCharacterClass;

const
    validCombinations : array[TRace] of TCharacterClassSet = (
        [ccClass0, ccClass1, ccClass2, ccClass3],  // race0 can be any class
        [ccClass0, ccClass2],                      // race1 
        [ccClass0, ccClass1, ccClass2],            // race2
        [ccClass0, ccClass3],                      // race3
        [ccClass0]                                 // race4
        );

您还可以为种族名称和字符类设置常量:

const
    raceNames : array[TRace] of string = (
        'Race 0',
        'Race 1',
        'Race 2',
        'Race 3',
        'Race 4'
        );

    characterClassNames = array[TCharacterClass] of string = (
        'Class 0',
        'Class 1 ',
        'Class 2',
        'Class 3'
        );

现在,如果您使用组合框进行用户输入和将输入映射到这些枚举类型,检查组合是否有效很简单:

function ValidRaceAndClass( aRace : TRace; aClass : TCharacterClass ) : Boolean;
    begin
    result := aClass in validCombinations[ aRace ];
    end;

I would recommend Steves solution as the starting point, but go a bit further with the use of enumerated types and sets...

type
    TRace = (rcRace0, rcRace1, rcRace2, rcRace3, rcRace4);

    TCharacterClass = (ccClass0, ccClass1, ccClass2, ccClass3);

    TCharacterClassSet = set of TCharacterClass;

const
    validCombinations : array[TRace] of TCharacterClassSet = (
        [ccClass0, ccClass1, ccClass2, ccClass3],  // race0 can be any class
        [ccClass0, ccClass2],                      // race1 
        [ccClass0, ccClass1, ccClass2],            // race2
        [ccClass0, ccClass3],                      // race3
        [ccClass0]                                 // race4
        );

You could also set up constants for the race names and character classes:

const
    raceNames : array[TRace] of string = (
        'Race 0',
        'Race 1',
        'Race 2',
        'Race 3',
        'Race 4'
        );

    characterClassNames = array[TCharacterClass] of string = (
        'Class 0',
        'Class 1 ',
        'Class 2',
        'Class 3'
        );

Now, if you use comboboxes for user input and map the input to these enumerated types, the check if a combination is valid is simple:

function ValidRaceAndClass( aRace : TRace; aClass : TCharacterClass ) : Boolean;
    begin
    result := aClass in validCombinations[ aRace ];
    end;
半枫 2024-07-20 03:42:48

我发现你应该遵循史蒂夫的解决方案。 但如果您喜欢使用字符串,则可以使用 TStringList(在 Delphi/可能是 FreePascal 中)。 您可以用您的比赛填充它,然后使用 TStringList 的 IndexOf 函数评估玩家的答案。 它返回传递的字符串的索引,或者当传递的字符串不在列表中时返回-1。

不管怎样,我强烈推荐史蒂夫的解决方案:)

I find you should follow Steves solution. But if using strings is your way, you could use a TStringList (in Delphi/possibly FreePascal). You could fill it with your races and then evaluate the players answer using the IndexOf function of the TStringList. It returns the index of the passed string or -1 when the passed string isn't in the List.

Anyway i'd strongly recommend Steves solution :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文