Tetris recursive does not work as i predicted

93 Views Asked by At

First of all, apologize my awful english. I hope you can understand my question. :) below function is tetris game's recommend system. It search every possible x, y positions and get a best position which gives highest score(max). It is based on data structure - Tree. I call this function with NULL pointer at first. so the first depth's prev == NULL. recommend X, Y, R / Field[HEIGHT][WIDTH] is global variable. checktomove function returns 1 when the block moving is possible. if you need more information about this code, I can upload the whole code.

Goal: At level 0: call level 1 recursively At level 1: call level 2 recursively At level 2: search the best score and return the score At level 1: search the best score returned from level2 and return the score At level 0: serach the best score returned from level1, and if the score is highest, record the position at that time on global variable. It will be used at drawing recommended blocks.

ERROR: I used tile2 for debugging, and if it works correct, then the tile2 must be '3'. But when I run the program, tile 2 is '2' and I guess this means the loop does not go into if(curr->level==0) recommend x, y, r... But what makes it stranger is, x, y, r changes (almost) all the time. How could this happen?

int recommend(Leaf_pointer prev){
    int max=0, tmp;
    int acc_score;
    int rotate, x, y, i, j;
    char originField[HEIGHT][WIDTH];
    rec=0, swip=0, tile2='2';
    Leaf_pointer curr = (Leaf_pointer)malloc(sizeof(Leaf));
    if(prev==NULL){
        curr->level = 0;
        acc_score = 0;
        for(i=0; i<HEIGHT; i++){
            for(j=0; j<WIDTH; j++){
                originField[i][j] = field[i][j];
            }
        }
    }
    else{
        curr->level = prev->level + 1;
        acc_score = prev->accumulatedScore;
        for(i=0; i<HEIGHT; i++){
            for(j=0; j<WIDTH; j++){
                originField[i][j] = prev->recField[i][j];
            }
        }
    }
    curr->curBlockID = nextBlock[curr->level];
    if(curr->curBlockID==4)
        rotate=3;
    else if(curr->curBlockID==0 || curr->curBlockID==5 || curr->curBlockID==6)
        rotate=2;
    else    
        rotate=0;
    for(rotate=0; rotate<4; rotate++){
        for(x=-1; x<WIDTH; x++){
            for(i=0; i<HEIGHT; i++){
                for(j=0; j<WIDTH; j++){
                    curr->recField[i][j] = field[i][j];
                }
            }
            if(x==WIDTH)
                break;
            y=0;
            while(CheckToMove(originField, curr->curBlockID, rotate, ++y, x));
            y--;
            curr->accumulatedScore = acc_score + AddBlockToField(curr->recField, curr->curBlockID, rotate, y, x);
            curr->accumulatedScore += DeleteLine(curr->recField);
            if(curr->level<VISIBLE_BLOCKS-1)    
                curr->accumulatedScore=recommend(curr);
            if(max < curr->accumulatedScore){
                max = curr->accumulatedScore;
                if(curr->level==0){
                    recommendY = y;
                    recommendX = x;
                    recommendR = rotate;
                    tile2='3';
                }
                rec=1;
            }
        }
    }
    free(curr);
    return max;
}
0

There are 0 best solutions below