【问题标题】:What's a better way to store information than by using static ints? C++有什么比使用静态整数更好的存储信息的方法? C++
【发布时间】:2011-08-05 13:43:43
【问题描述】:

我通过将玩家的工作设置为一个数字来跟踪玩家的“工作”,如果他改变工作,则将其加一,并通过该数字是偶数还是奇数来确定他当前的工作。 (现在只有两个工作)。但是,我知道有更好的方法可以做到这一点,很快我就需要实施第三和第四个工作,所以我不能继续使用偶数/奇数检查。

这是我的代码供参考:(请注意,我只包含相关代码)

GameModeState.cpp

// If changeJob's parameter number is 1, it increments the job. If number is 2, it only  returns the current job
int GameModeState::changeJob(int number)
{
   // Default job is even (landman)
   static int job = 1;
   if (number == 1)
   {
    job = (job+1);
    return job;
   } 
   else 
   {
    return job;
   }
}

int GameModeState::getJob()
{
    int currentJob = (changeJob(2));
    return currentJob;
}

// If the player opens the "stat sheet", it changes their job
void GameModeState::_statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
    changeJob(1);
}

GameModeState.h

class GameModeState : public GameState::State
{
public:

    /// Changes the player's job if number is 1, or returns current job if number is 2
    static int changeJob(int number);

    /// Returns the current job number by calling changeJob appropriately
    static int getJob();

private:

    // Opening the player sheet will change the player's job
    void _statSheet(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output);
};

ZoneMovementState.cpp(这是我检查当前工作的地方)

#include "GameModeState.h"
#include <EnergyGraphics/ZoneParser.h>

    void ZoneMovementState::_changeZone(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
    {
        // If the number from getJob is even, the player is currently a geologist
        if (GameModeState::getJob()%2 == 0)
        {
            ZoneParser::getSingleton().load("../media/zones/geology_zone.xml", false);
        } 
        else //otherwise they are a landman
        {
            ZoneParser::getSingleton().load("../media/zones/landman_zone.xml", false);
        }
        transitionHandler->go();
    }

我认为作业的数组或枚举将是处理此问题的更好方法,但我不确定如何在我的代码中实现这一点。如果您知道更好的方法,请包括示例或至少一个正确方向的点。我将不胜感激!

【问题讨论】:

    标签: c++ arrays enums static-methods code-organization


    【解决方案1】:

    不要使用静态变量在类中保存类似的东西。请改用成员变量。

    IMO 做类似事情并使其可扩展的最简单方法是使用枚举:

    enum PlayerJob
        JOB_NONE = 0,
        JOB_GEOLOGIST,
        JOB_LANDMAN,
        ...
        NUM_JOBS // this element is optional but can be useful for range checking.
    };
    
    ...
    
    PlayerJob job = JOB_NONE;
    
    ...
    
    switch(job)
    {
        case JOB_NONE:
            break;
        case JOB_GEOLOGIST:
            ...
            break;
        ...
        default:
            error("Unhandled palyer job: %d", job);
            break;
    }
    

    我还考虑以某种方式将这些“与工作相关”的东西组织成某种数组或列表或其他任何东西,以便更容易地调用“特定于工作”的东西:

    std::map<PlayerJob,std::string> jobzones;
    jobzones.push_back(JOB_GEOLOGIST, "geozone.xml");
    
    ...
    
    transitToZone(jobzones[job]);
    

    【讨论】:

    • 这很好,但我知道枚举会更好;问题是我不知道如何将它们与我的代码结合使用。
    • 嗯,好的,您提供了一些示例。谢谢!
    【解决方案2】:

    枚举很好,您也可以考虑使用 std::stack 或类似的东西来为 GameState 使用,以便您可以推送/弹出等。

    【讨论】:

      【解决方案3】:

      你可能想看看State pattern

      【讨论】:

        猜你喜欢
        • 2013-11-19
        • 2011-01-27
        • 2012-01-02
        • 2013-02-20
        • 2010-10-26
        • 2019-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多