忍者ブログ
  • 2024.08
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 2024.10
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2024/09/19 11:57 】 |
Nation.h
Nation.h
国のクラスです。メンバ変数として国民へのポインタを持ちます。
メンバ関数にポインタに国民を割り当てたり解放したりする処理を
記述することで入出国を管理します。

#include <typeinfo>
class Nation{
 private:
  const static int MAX_PLAYER_NUM = 10; // プレイヤー数の上限
  const static int NPC_NUM = 5;         // NPC数(固定)

  const char * const name;              // 国名
  int playerNum;                        // プレイヤー数
  Player *player[MAX_PLAYER_NUM];       // プレイヤー
  Player *NPC[NPC_NUM];                 // NPC

  const int searchEmptyPlayerIndex();
  template <class T> const static bool typeIsPlayer(T arg){
    return (typeid(arg)) == (typeid(Player));
  }

 public:
  // コンストラクタ、デストラクタに配置newを使いたかったけど,使いこなせなかったので断念
 Nation(const char* const name):
  name(name), playerNum(0), player(){
    for(int i=0;i<NPC_NUM;i++)NPC[i] = new Player();
    const static bool DEBUG = true;
    if(DEBUG){
      std::cout << "-_~DEBUG Nation.h Nation()~_-" << std::endl;
      std::cout << "国名(Nation::name):" << Nation::name << std::endl;
      std::cout << std::endl;
    }
    /*
    for(int i=0;((i<MAX_NAME_SIZE)&&(name[i]!='\0'));i++){
      Nation::name[i] = name[i];
    }
    */
  }
  ~Nation(){
    for(int i=0;i<NPC_NUM;i++){
      delete NPC[i];
      NPC[i] = NULL;
    }
  }
  const char* getName(){return name;}
  Player* getPlayer(const int index){return player[index];}
  Player* getNPC(const int index){return player[index];}
  // プレイヤーをこの国に所属させる(プレイヤーは引数で指定)
  int addPlayer(Player * const parg){
    const int index = searchEmptyPlayerIndex();
    addPlayer(index, parg);
    return index;
  }
  void addPlayer(const int index, Player * const parg);

  void removePlayer(const int index);         // プレイヤーの解雇
  const int getPlayerNum(){return playerNum;} // プレイヤー数を確認
  const int getPlayerAndNPCNum(){return NPC_NUM+playerNum;} //プレイヤー数とNPCをあわせた合計

  // 戦闘相手として選択される場合などにプレイヤーかNPCを取得する関数
  // 例えばプレイヤー3人とNPC5人なら
  // プレイヤーが0,1,2でNPC5人が3,4,5,6,7の番号に対応する
  Player* getPlayerOrNPC(const int index);

  const bool allNotSurvive(); // プレイヤーとNPCが全員修理中かどうかを調べる
};

PR
【2013/03/08 03:14 】 | EBの箱 | 有り難いご意見(0)
<<Server.h | ホーム | Player.h>>
有り難いご意見
貴重なご意見の投稿














<<前ページ | ホーム | 次ページ>>