2015-08-08 57 views
-1

我有兩個類;球員類和比賽類。我希望玩家能夠使用其所有成員功能;攜帶,捕獲等,但其成員函數需要了解匹配類數據成員;董事會,對手,挑戰者,而不公開這些數據成員。無法訪問類之間的數據成員

如果你找不到一個好的解決方案或者對我所問的問題感到困惑,我仍然會對班級組織的建議感興趣。

mancala.h

#ifndef MANCALA_H 
#define MANCALA_H 

#include <string> 
#include <random> 

namespace mancala{ 

    class player{ 
     public: 
      void carry(int pit); 
      void capture(const int& pit); 
      bool madeStore(const int& pit)const; 
      bool madeCapture(const int& pit)const; 
      bool madeValidMove(const int& pit)const; 
      bool madeSideClear()const; 
      bool operator==(const player& p)const; 
     private: 
      std::string tag; 
    }; 

    class match{ 
     public: 
      match(const int& match_ID = 0) : board{4,4,4,4,4,4,0,4,4,4,4,4,4,0}{ 
       if(match_ID){ 
        ; 
       } 
       else{ 
        this->turn = randPlayer(); 
        this->status = "make move"; 
       } 
      }; 
      void begin(); 
      void end(); 
      void save(); 
      bool update(); 
     private: 
      player challenger, opponent, turn; 
      static const int seeds = 48; 
      static const int pits = 14; 
      std::string status; 
      int board[pits]; 
      void nextTurn(); 
      player randPlayer(); 
    }; 
} 

#endif//MANCALA_H 

來自實例:mancala.cpp

void player::carry(int pit) 
{ 
    if(this->madeValidMove(pit) == false){ 
     status = "invalid move"; 
    } 
    else{ 

     int hand = board[pit]; 

     board[pit] = 0; 

     for(hand; hand != 0; hand--){ 

      if(pit == 13){ 
       pit = 0; 
      } 
      else{ 
       pit++; 
      } 
      //Skips opponent store 
      if(pit == 13 && turn == challenger){ 
       pit = 0; 
      }//Skips challenger store 
      else if(pit == 6 && turn == opponent){ 
       pit++; 
      } 
      //If placing last seed check where it is being placed 
      if(hand == 1){ 
       //Store 
       if(this->madeStore(pit)){ 
        board[pit]++; 
        status = "free move"; 
       }//Capture 
       else if(this->madeCapture(pit)){ 
        this->capture(pit); 
        nextTurn(); 
       }//Normal 
       else{ 
        board[pit]++; 
        nextTurn(); 
       } 
      }//Placing seed 
      else{ 
       board[pit]++; 
      } 
     } 

     if(this->madeSideClear()){ 
      end(); 
     } 
    } 

    save(); 
} 

回答