为什么我的指针不起作用?

时间:2014-03-24 13:35:32

标签: c pointers struct

我正在尝试编写旧的画家游戏,在那里你在正方形上画画,当周边完成时,正方形会改变颜色,使用ncurses。我写了一个简单的关卡编辑器,让我用星号绘制正方形。我有一个块结构和它们的数组,可以通过蛮力扫描屏幕上的星号和设置块到那些位置来创建块。所以我意识到每个块都连接到2-4个其他块,所以我可以指向下一个up / dn / lt / rt块并按照指针进行操作。这样可以节省时间,因为我不需要检查边界条件或者玩家留在线上,我可以简单地检查ptr是否为空,如果是,则不让玩家向那个方向移动。所以我可以保存块数组并将其加载到画家游戏中,它可以正确显示。我有一个ptr到当前块cur_blk我已经设置为指向数组元素5,这是有效的。如果我gdb,我可以打印出* cur_blk的值,它显示everthing是正确的,但指针不起作用。我认为这个问题出现在关卡编辑器中,但我被卡住了。

这是一些代码。这是块结构:

struct block{
int id;
int row, col;
int alive;

struct block *next_up, *next_dn, *next_lt, *next_rt;
struct square *my_squares;  

};

这是块数组设置:

struct block *bp;
int cnt = 0;

for(bp = &the_blocks[0]; bp < &the_blocks[1024]; bp++){
    bp->id = cnt;   
    bp->row = 0;
    bp->col = 0;
    bp->alive = FALSE;

    bp->next_up = NULL;
    bp->next_dn = NULL;
    bp->next_lt = NULL;
    bp->next_rt = NULL;

    bp->my_squares = (struct square *)malloc(sizeof(struct square)*4);
    cnt++;
}

这里是为块提供正确的co-ords并将它们链接在一起的代码 - 链接代码在for(d=0;d<blk_cnt;d++)循环中。它重新扫描比较co-ords的数组,这样如果行少一个并且col相同,则将其设置为the_blocks[c].next_up。我很确定这是错误的地方,但我不知道它是什么。

char cc, dn_ch, rt_ch; // cur/next chars
    int row, col;

    //create blocks

    for(row=0;row<24;row++){
        for(col=0;col<61;col++){
            cc = mvinch(row,col);

            if(cc == '*'){
                the_blocks[blk_cnt].row = row;
                the_blocks[blk_cnt].col = col;
                the_blocks[blk_cnt].alive = TRUE;
                blk_cnt++;
            }

            if(row<24) ;
                dn_ch = mvinch(row+1, col);

            if(col<60) ;
                rt_ch = mvinch(row,col+1);

            if(cc == '*' && dn_ch == '*' && rt_ch == '*')
                make_square(row,col);
        }
    }

    //make into squares and link blocks

    int c,d,e, scnt;

    for(c=0;c<blk_cnt;c++){
        int row = the_blocks[c].row;
        int col = the_blocks[c].col;

        for(e=0;e<sq_cnt;e++){
            // for each square, check top/base then left side and rigth side.
            // use tlx, tly etc
            scnt = 0;

            int tlx = the_squares[e].tlx;
            int tly = the_squares[e].tly;
            int blx = the_squares[e].blx;
            int bly = the_squares[e].bly;
            int trx = the_squares[e].trx;
            int try = the_squares[e].try;
            int brx = the_squares[e].brx;
            int bry = the_squares[e].bry;

            if(row == tly && col >=tlx && col <= trx){
                the_blocks[c].my_squares[scnt] = the_squares[e];
                scnt++;
            }
            if(row == bly && col >=blx && col <= brx){
                the_blocks[c].my_squares[scnt] = the_squares[e];
                scnt++;
            }
            if(col == tlx && row >=tly && row <= bly){
                the_blocks[c].my_squares[scnt] = the_squares[e];
                scnt++;
            }
            if(col == trx && row >=try && row <= bry){
                the_blocks[c].my_squares[scnt] = the_squares[e];
                scnt++;
            }
        }

        for(d=0;d<blk_cnt;d++){
            if((the_blocks[d].row == row-1) && (the_blocks[d].col == col)){
                //the_blocks[c].next_up = (struct block *)malloc(sizeof(struct block));
                the_blocks[c].next_up = &the_blocks[d];
            }
            if((the_blocks[d].row == row+1) && (the_blocks[d].col == col)){
                //the_blocks[c].next_dn = (struct block *)malloc(sizeof(struct block));
                the_blocks[c].next_dn = &the_blocks[d];
            }
            if(( the_blocks[d].col == col-1) && (the_blocks[d].row == row)){
                //the_blocks[c].next_lt = (struct block   )malloc(sizeof(struct block));
                the_blocks[c].next_lt = &the_blocks[d];
            }
            if((the_blocks[d].col == col+1) && (the_blocks[d].row == row)){
                //the_blocks[c].next_rt = (struct block *)malloc(sizeof(struct block));
                the_blocks[c].next_rt = &the_blocks[d];
            }
        }
    }

以下是画家游戏中设置cur_blk指针的代码

cur_blk = &the_blocks[5];
move(cur_blk->row, cur_blk->col);
addch('@');
refresh();  

这是移动代码,我认为我可以按照指示进行操作:

switch(dir){
    case UP    : if(cur_blk->next_up != NULL){
                    cur_blk = cur_blk->next_up;
                    moved = TRUE;
                    break;
    }
    case DOWN  : if(cur_blk->next_dn != NULL){
                    cur_blk = cur_blk->next_dn;
                    moved = TRUE;
                    break;
    }
    case LEFT  : if(cur_blk->next_lt != NULL){
                    cur_blk = cur_blk->next_lt;
                    moved = TRUE;
                    break;
    }
    case RIGHT : if(cur_blk->next_rt != NULL){
                    cur_blk = cur_blk->next_rt;
                    moved = TRUE;
                    break;
    }
}

我很困难,这是不真实的。在此先感谢您的帮助。如果需要,我可以发布更多代码。关卡编辑器的工作区别于连接位!

0 个答案:

没有答案