2016-08-01 59 views
-4

我正在爲一個離線遊戲寫一個作弊,並且有一個叫做Player的類,負責在另一個進程中獲取和設置值。然而,我對這個類的設計非常差,因爲使用了類播放器看起來非常凌亂和醜陋,非常難以閱讀和維護更好的設計模式來讀取其他進程內存?

// declare variables here to read 
if (Player.getthis() && Player.getthat() && Player.getthat() ... and so on) 
//do stuff 

class Player { 
... 
public: 
    ... 
    // either of these calls can fail, so I return TRUE on success and FALSE on failure 
    BOOL GetHealth(float& health); 
    BOOL SetHealth(float& health); 
    ... 
}; 

所以我的問題是,什麼是這樣做的更好的辦法? 另外:我不一定需要在內存中讀取Player的每個值,每次只能讀取幾個值。這就是爲什麼我沒有一個單一的方法,如BOOL UpdatePlayer(),這將讀到的一切,並更新播放器

+2

而不是'bool'關鍵字? (與'TRUE' /'FALSE'和'true' /'false'一樣) – Borgleader

+0

我假設作弊是針對winapi應用程序的。 – drescherjm

+0

看來您的代碼目前正常工作,並且您正在尋求改進它。一般來說,這些問題對於這個網站來說太過分了,但是你可能會在[CodeReview.SE](// codereview.stackexchange.com/tour)找到更好的運氣。請記住閱讀[他們的要求](// codereview.stackexchange.com/help/on-topic),因爲它們比本網站更嚴格。 – FrankerZ

回答

1

這是我會怎麼做:你爲什麼要使用`BOOL`

class Player { 
public: 
    class AccessException : public std::exception { 
     friend class Player; 
    public: 
     virtual const char *what() const noexcept { 
      return "Error getting property with key " + key; 
     } 

    private: 
     AccessException(const std::string &key) 
      : key(key) 
     {} 

     std::string key; 
    }; 

    float GetHealth() { 
     if (is_error) { 
      throw AccessException("health"); 
     } 

     return health; 
    } 

    float GetPosX() { 
     if (is_error) { 
      throw AccessException("posX"); 
     } 

     return posX; 
    } 
}; 

void do_stuff() { 
    try { 
     float health = player.GetHealth(); 
     float posX = player.GetPosX(); 
     // Use health and posX... 
    } catch (const AccessException &ex) { 
     std::cerr << ex.what() << std::endl; 
    } 
} 
+0

你爲什麼添加例外? –

+1

@KolyolyHorvath因爲這實際上正是他們創造的。 – Joel

+0

爲什麼?我的意思是......在OP的問題中沒有任何跡象表明他需要他們。 –