创建具有不同游戏模式和设置的游戏,如何处理所有不同的设置?
Creating game with different game modes and settings, how to handle all the different settings?
我正在创建一个游戏,您可以在其中与计算机或人类对战,每个游戏都有多个(高级)设置,添加新设置后,可能的游戏种类会迅速增加。
我想编写干净的代码,我面临的问题是我需要为所有可能的 optional/mutual 独占选项编写大量 "unnecessary" 检查 .不必要的,因为当游戏是用特定设置创建的时,它可能已经知道它的选项,因此哪些检查是多余的。我看不出有什么理由不断检查,例如如果游戏只有计算机、人类或两者的混合。
我知道 Builder-pattern 可以解决将许多参数传递给构造函数的问题,其中许多参数是可选的。我可以在创建游戏时使用它。这解决了难以构建游戏的问题。然而,这仍然需要在游戏期间进行大量检查 运行 查看哪些设置已设置,哪些未设置。
有什么线索可以让我调查吗?
代码示例(仅供参考)
Mode mode;
Vocabulary mVocabulary;
Player[] mPlayers;
GameLog mGameLog;
String mCurrentString;
int mMinimalWordLength = 3;
int mIndexActivePlayer;
int mNumberOfActivePlayers;
boolean mComputers; //[Optional]
int mNumberOfComputers; //[Required if mComputer is true]
Difficulty mDifficulty; //[Required if mComputer is true]
//One of these optionals are required
boolean mBlueTooth; //[Optional]
boolean online; //[Optional]
boolean sameDevice; //[Optional]
//Here I could use the builder pattern to set all the options
public Game(Vocabulary vocabulary, int numberOfPlayers, int minimalWordLength, Player[] players) {
this.mVocabulary = vocabulary;
this.mMinimalWordLength = minimalWordLength;
this.numberOfActivePlayers = numberOfPlayers;
this.mPlayers = players;
}
}
我正在创建一个游戏,它需要 mVocabulary
个单词、玩家列表 mPlayers
和一些可选设置。每个游戏都是为计算机或人类或两者的混合而创建的。每场比赛都是 online
,通过 blue-toot
或 sameDevice
。
基于这些选项,在整个代码中会发生不同的事情,因此您可以在此代码后面的每个方法中编写几个 if else if 语句。但是我想防止这种情况。
如果您将程序视为一棵决策树,您在程序的特定时间点处在其中,那么您就知道一旦以特定方式创建游戏,您应该看和不看的地方。 因为你在树的特定分支中。例如,你创建了一个只有人类的游戏,如果你知道只有人类玩家,那么做一个奇怪的事情就是在每一轮检查玩家是否是人类,因为这首先是你创建游戏的方式.
我希望这能让我的观点更清楚。虽然没有写完整的代码示例。
不确定这是否是最好的方法,但我通过 subclassing 和实现 'hook' 方法避免了太多检查(实际上我从 Rails
钩子中借用了这个想法) .
例如,假设您有 ParentGame
class 和 BlueToothGame
、OnlineGame
和 SameDeviceGame
children。您将在 ParentGame
中实现所有常见逻辑,例如。-
public void mainLoop() {
startOfMainLoop();
// Some stuff
checkWinLoseCondition();
}
其中 startOfMainLoop
和 checkWinLoseCondition()
可以是要在 children classes 上实现的抽象方法。通过这种方式,您封装了通用逻辑,并为每个 'kind/configuration of game' 实现了特定的行为,而不会因主要游戏逻辑的大量检查而弄乱您的代码。
虽然只是一个想法,当然最好的方法取决于您当前的情况:)
我正在创建一个游戏,您可以在其中与计算机或人类对战,每个游戏都有多个(高级)设置,添加新设置后,可能的游戏种类会迅速增加。
我想编写干净的代码,我面临的问题是我需要为所有可能的 optional/mutual 独占选项编写大量 "unnecessary" 检查 .不必要的,因为当游戏是用特定设置创建的时,它可能已经知道它的选项,因此哪些检查是多余的。我看不出有什么理由不断检查,例如如果游戏只有计算机、人类或两者的混合。
我知道 Builder-pattern 可以解决将许多参数传递给构造函数的问题,其中许多参数是可选的。我可以在创建游戏时使用它。这解决了难以构建游戏的问题。然而,这仍然需要在游戏期间进行大量检查 运行 查看哪些设置已设置,哪些未设置。
有什么线索可以让我调查吗?
代码示例(仅供参考)
Mode mode;
Vocabulary mVocabulary;
Player[] mPlayers;
GameLog mGameLog;
String mCurrentString;
int mMinimalWordLength = 3;
int mIndexActivePlayer;
int mNumberOfActivePlayers;
boolean mComputers; //[Optional]
int mNumberOfComputers; //[Required if mComputer is true]
Difficulty mDifficulty; //[Required if mComputer is true]
//One of these optionals are required
boolean mBlueTooth; //[Optional]
boolean online; //[Optional]
boolean sameDevice; //[Optional]
//Here I could use the builder pattern to set all the options
public Game(Vocabulary vocabulary, int numberOfPlayers, int minimalWordLength, Player[] players) {
this.mVocabulary = vocabulary;
this.mMinimalWordLength = minimalWordLength;
this.numberOfActivePlayers = numberOfPlayers;
this.mPlayers = players;
}
}
我正在创建一个游戏,它需要 mVocabulary
个单词、玩家列表 mPlayers
和一些可选设置。每个游戏都是为计算机或人类或两者的混合而创建的。每场比赛都是 online
,通过 blue-toot
或 sameDevice
。
基于这些选项,在整个代码中会发生不同的事情,因此您可以在此代码后面的每个方法中编写几个 if else if 语句。但是我想防止这种情况。
如果您将程序视为一棵决策树,您在程序的特定时间点处在其中,那么您就知道一旦以特定方式创建游戏,您应该看和不看的地方。 因为你在树的特定分支中。例如,你创建了一个只有人类的游戏,如果你知道只有人类玩家,那么做一个奇怪的事情就是在每一轮检查玩家是否是人类,因为这首先是你创建游戏的方式.
我希望这能让我的观点更清楚。虽然没有写完整的代码示例。
不确定这是否是最好的方法,但我通过 subclassing 和实现 'hook' 方法避免了太多检查(实际上我从 Rails
钩子中借用了这个想法) .
例如,假设您有 ParentGame
class 和 BlueToothGame
、OnlineGame
和 SameDeviceGame
children。您将在 ParentGame
中实现所有常见逻辑,例如。-
public void mainLoop() {
startOfMainLoop();
// Some stuff
checkWinLoseCondition();
}
其中 startOfMainLoop
和 checkWinLoseCondition()
可以是要在 children classes 上实现的抽象方法。通过这种方式,您封装了通用逻辑,并为每个 'kind/configuration of game' 实现了特定的行为,而不会因主要游戏逻辑的大量检查而弄乱您的代码。
虽然只是一个想法,当然最好的方法取决于您当前的情况:)