如何将这两个功能合并为一个实现?

时间:2020-05-27 20:32:29

标签: c++

我有此类的依赖项:

class Player
{
public:
    struct Unit{
        struct Pos{
            uint_fast16_t x;
            uint_fast16_t y;
        }position;
        uint_fast8_t ship : 1 ;
        uint_fast8_t miss : 1 ;
        uint_fast8_t hit : 1 ;
        uint_fast8_t destroyed : 1;
        uint_fast8_t r: 4;
    };
std::array<Unit,100> battle_field = { state }; //GetSingleUnitState indirectly gets data from this array

const uint_fast8_t Player::makeShot(  const uint_fast16_t size, const uint_fast16_t x, const uint_fast16_t y  )const;
const uint_fast8_t Player::isShip( const uint_fast16_t size, const uint_fast16_t x, const uint_fast16_t y ) const;

const Player::Unit Player::GetSingleUnitState(const uint_fast16_t &Absciss ,const uint_fast16_t &Ordinate) const{
   return this->first_player_battle_field_.battle_field[ ( (Ordinate * 10 ) + Absciss ) ];
};

};

还有很多其他数据,但与我的问题无关。

下面,我粘贴了声明方法中提到的两者的实现,它们可以正常工作,但它们几乎相同(仅在unit的字段check中有所不同),而且我不知道如何将它们组合为1具有通用参数的方法,因为它始终取决于uint_fast8_t tmp,num ;变量,并且在调用之前我无法提供所有需要的“布尔值”。

const uint_fast8_t Player::isShip( const uint_fast16_t size, const uint_fast16_t x, const uint_fast16_t y ) const{
    if(  ( (x > 0) &&(y > 0)&&( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y -1).ship  != 0 ))  ||
          ( (y > 0) &&(10 > x)&&  (Player::GetPlayerInstance().GetSingleUnitState(x +1 ,y -1).ship  != 0 )) ||
          ( (10 >y)&&(10 > x)&& (Player::GetPlayerInstance().GetSingleUnitState(x +1 ,y +1).ship  != 0 ))  ||
          ( (x > 0) &&(10 > y)&& ( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y +1).ship  != 0 ))||
          ( (x > 0) && ( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y).ship  != 0 ))||
          ( (y > 0 )&&( Player::GetPlayerInstance().GetSingleUnitState(x ,y -1 ).ship != 0 ))
          )
        return false;
    if( ( y > 0 ) &&( Player::GetPlayerInstance().GetSingleUnitState(x ,y -1 ).ship != 0 ) ) return false;
    if( Player::GetPlayerInstance().GetSingleUnitState(x ,y).ship == 0 )return false;
    uint_fast8_t tmp = x,num = 0;
    while( ( Player::GetPlayerInstance().GetSingleUnitState(tmp,y).ship != 0)  &&(  tmp < 10 ) ){
        tmp++,num++;
    }
    if( num == size ){
        if( Player::GetPlayerInstance().GetSingleUnitState(x ,y+1).ship != 0 ) return false;
        return true;
    }
    tmp = y;
    num = 0;
    while( ( Player::GetPlayerInstance().GetSingleUnitState(x ,tmp).ship != 0 ) &&( tmp < 10 ) ) {
        tmp++;
        num++;
    }
    if( num == size ){
        if( Player::GetPlayerInstance().GetSingleUnitState(x+1 ,y).ship != 0 ) return false;
        return true;
    }
    return false;
}

bool Player::makeShot(  const uint_fast16_t size, const uint_fast16_t x, const uint_fast16_t y  )const
{
    if(  ( (x > 0) &&(y > 0)&&( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y -1).hit  != 0 ))  ||
          ( (y > 0) &&(10 > x)&&  (Player::GetPlayerInstance().GetSingleUnitState(x +1 ,y -1).hit  != 0 )) ||
          ( (10 >y)&&(10 > x)&& (Player::GetPlayerInstance().GetSingleUnitState(x +1 ,y +1).hit  != 0 ))  ||
          ( (x > 0) &&(10 > y)&& ( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y +1).hit  != 0 ))||
          ( (x > 0) && ( Player::GetPlayerInstance().GetSingleUnitState(x -1 ,y).hit  != 0 ))||
          ( (y > 0 )&&( Player::GetPlayerInstance().GetSingleUnitState(x ,y -1 ).hit != 0 ))
          )
        return false;
    if( ( y > 0 ) &&( Player::GetPlayerInstance().GetSingleUnitState(x ,y -1 ).hit != 0 ) ) return false;
    if( Player::GetPlayerInstance().GetSingleUnitState(x ,y).hit == 0 )return false;
    uint_fast8_t tmp = x,num = 0;
    while( ( Player::GetPlayerInstance().GetSingleUnitState(tmp,y).hit != 0)  &&(  tmp < 10 ) ){
        tmp++,num++;
    }
    if( num == size ){
        if( Player::GetPlayerInstance().GetSingleUnitState(x ,y+1).hit != 0 ) return false;
        return true;
    }
    tmp = y;
    num = 0;
    while( ( Player::GetPlayerInstance().GetSingleUnitState(x ,tmp).hit != 0 ) &&( tmp < 10 ) ) {
        tmp++;
        num++;
    }
    if( num == size ){
        if( Player::GetPlayerInstance().GetSingleUnitState(x+1 ,y).hit != 0 ) return false;
        return true;
    }
    return false;

}

1 个答案:

答案 0 :(得分:0)

您可以使用模板基函数来更改您要访问的成员:


#include <iostream>

struct Unit {
    int ship = 1;
    int hit = 2;
};


template <auto member>
void test() {
   Unit unit;
   std::cout << unit.*member << std::endl;
}

int main() {

    test<&Unit::ship>();
    test<&Unit::hit>();

    return 0;
}
相关问题