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

时间:2021-04-03 17:53:24

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].

我正在写一个小游戏,用户被要求参加比赛和上课。字符串[5]有五种可能的种族,字符串[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?

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

Any hints will be appreciated.

任何提示将不胜感激。

3 个解决方案

#1


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.

同样的课程。

#2


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

我会建议使用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
        );

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;

#3


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.

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

Anyway i'd strongly recommend Steves solution :)

无论如何,我强烈推荐Steves解决方案:)

#1


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.

同样的课程。

#2


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

我会建议使用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
        );

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;

#3


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.

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

Anyway i'd strongly recommend Steves solution :)

无论如何,我强烈推荐Steves解决方案:)