powielanie kodu - refaktoryzacja funkcji

0

Cześć,

w jaki sposób mógłbym zmienić funkcję poniżej żeby nie powielać kodu ?

bool Matrix::update(uint8_t row, uint8_t col)
{
    Cell *curr;
    bool changed = false;
    auto &root = _matrix[row][col];
    u8_set temp;

    for(uint8_t r = 0; r < MATRIX_ROWS; ++r)
    {
        if(r == row)
            continue;

        curr = &_matrix[r][col];
        if(curr->get_value() > 0)
        {
            changed = root.cannot_have(curr->get_value()) ? true : changed;
            if(changed) return true;
            temp = {0};
        }
        else
        {
            if(temp.size() < curr->size())
                temp = curr->get_possible_values();
        }
    }

   if(root.get_possible_values().size() > temp.size())
        changed = root.cannot_have(temp) ? true : changed;

   temp = {0};

    for(uint8_t c = 0; c < MATRIX_COLS; ++c)
    {
        if(c == col)
            continue;
        curr = &_matrix[row][c];
        if(curr->get_value() > 0)
        {
            changed = root.cannot_have(curr->get_value()) ? true : changed;
            if(changed) return true;
            temp = {0};
        }
        else
        {
            if(temp.size() < curr->get_possible_values().size())
                temp = curr->get_possible_values();
        }
    }

   if(root.get_possible_values().size() > temp.size())
       changed = root.cannot_have(temp) ? true : changed;

    return changed;
}
1

Myślę że najlepiej jest oddzielić logikę sprawdzania od iteracji kolumn i wierszy w następujący sposób:


bool Matrix::update( uint8_t row, uint8_t col )
{  
    for(uint8_t r = 0; r < MATRIX_ROWS; ++r)
    {
        if(r == row)  continue;
        if( update( _matrix[row][col] , r , col ) ) return true;      
    }
 
    for(uint8_t c = 0; c < MATRIX_COLS; ++c)
    {
        if(c == col)  continue;
        if( update( _matrix[row][col] , row , c ) ) return true;
    } 

    return false;
}

bool Matrix::update( const Cell& cell , uint8_t row, uint8_t col )
{
    // tutaj przenieś logikę sprawdzania
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1