C ++ /如何重构此函数

时间:2011-10-14 20:10:25

标签: c++ refactoring

我有一个存储为NxM矩阵的地图。在这张地图中,我想添加一个mazed路径,同时存储路径的方向和类型(从左到右的线,从上到下的角,等等......),用于图形渲染问题。

路径是通过对函数的过度调用来构造的,该函数添加了水平或垂直路径(下面发布的代码)。这个函数有非常相似的代码片段,我认为可以重构为更一般的情况,但我看不到它。你知道我该怎么办?

void Map::addTraject(int initial_x, int initial_y, int final_x, int final_y){
    //Check verticality of the path
    bool vertical;
    if (initial_x == final_x)
        vertical = false;
    else if (initial_y == final_y)
        vertical = true;
    else 
        throw WrongCoordinatesException("Traject cannot be diagonal");

    //Get the last road added to the path
    Road* last;
    if (route.size() == 0) {
        last = NULL;
    }
    else {
        last = route.at(route.size()-1);
        assert(initial_x == last->x && initial_y == last->y);
    }

    if (last != NULL){
        //Last road never can be a corner
        switch(last->type) 
        {
        case LEFT_TOP:
        case LEFT_DOWN:
        case RIGHT_TOP:
        case RIGHT_DOWN:
        case TOP_LEFT:
        case TOP_RIGHT:
        case DOWN_LEFT: 
        case DOWN_RIGHT: 
            assert(false);
        }
    }

    if (vertical)
    {
        if (initial_x < final_x) //LEFT-RIGHT path
        { 
            //Change last road to a corner if needed
            if (last != NULL)
            {
                switch(last->type) 
                {
                case LEFT_RIGHT: break;
                case RIGHT_LEFT: throw WrongCoordinatesException("Path cannot be overlapped");
                case TOP_DOWN: last->type = TOP_RIGHT; break;
                case DOWN_TOP: last->type = DOWN_RIGHT; break;
                }
                initial_x++; //Start in the position at the right
            } 
            for (int i = initial_x; i <= final_x; i++){
                route.push_back(new Road(i, initial_y, LEFT_RIGHT));
            }
        }else {
            //RIGHT-LEFT path
            //Change last road to a corner if needed
            if (last != NULL)
            {
                switch(last->type) 
                {
                case LEFT_RIGHT: throw WrongCoordinatesException("Path cannot be overlapped");
                case RIGHT_LEFT: break;
                case TOP_DOWN: last->type = TOP_LEFT; break;
                case DOWN_TOP: last->type = DOWN_LEFT; break;
                }
                initial_x--; //Start in the position at the left
            } 
            for (int i = initial_x; i >= final_x; i--){
                route.push_back(new Road(i, initial_y, RIGHT_LEFT));
            }
        }
    }
    else {
        if (initial_y < final_y){
            //TOP-DOWN path
            //Change last road to a corner if needed
            if (last != NULL)
            {
                switch(last->type) 
                {
                case LEFT_RIGHT: last->type = LEFT_DOWN; break;
                case RIGHT_LEFT: last->type = RIGHT_DOWN; break;
                case TOP_DOWN: break;
                case DOWN_TOP: throw WrongCoordinatesException("Path cannot be overlapped");
                }
                initial_y++; //Start in the position at the bottom
            } 
            for (int i = initial_y; i <= final_y; i++){
                route.push_back(new Road(initial_x, i, TOP_DOWN));
            }
        } else {
            //DOWN-TOP path
            //Change last road to a corner if needed
            if (last != NULL)
            {
                switch(last->type) 
                {
                case LEFT_RIGHT: last->type = LEFT_TOP; break;
                case RIGHT_LEFT: last->type = RIGHT_TOP; break;
                case TOP_DOWN: break;
                case DOWN_TOP: throw WrongCoordinatesException("Path cannot be overlapped");
                }
                initial_y--;
            } 
            for (int i = initial_y - 1; i >= final_y; i--){
                route.push_back(new Road(initial_x, i, DOWN_TOP));
            }
        }
    }
}

0 个答案:

没有答案