【发布时间】:2019-01-18 17:11:04
【问题描述】:
所以我正在尝试为我的蛇游戏打印地图。这是代码:
#define WIDTH 20
#define HEIGHT 20
struct coordinate {
int x;
int y;
};
typedef struct coordinate coordinate;
coordinate map[HEIGHT][WIDTH];
void init_map(){ // Function initializes the map with the corresponding coordinates
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
map[i][j].y = i;
map[i][j].x = j;
}
}
} /* init_map */
// Function initializes the first snake with the corresponding coordinates
void init_snake1(coordinate snake1[], int snake1_length){
snake1[0].x = WIDTH/2;
snake1[0].y = HEIGHT/2;
snake1[1].x = snake1[0].x;
snake1[1].y = snake1[0].y+1;
} /* init_snake1 */
void print_map(coordinate snake1[], int snake1_length){
for(int i = 0; i < HEIGHT; i ++){
for(int j = 0; j < WIDTH; j++){
if(map[i][j].x == 0 && map[i][j].y == 0){
printf("#");
}else if(map[i][j].x == WIDTH-1 && map[i][j].y == HEIGHT-1){
printf("#");
}else if(map[i][j].y == 0 || map[i][j].y == HEIGHT-1){
printf("#");
}else if(map[i][j].x == 0 || map[i][j].x == WIDTH-1){
printf("#");
}else if(map[i][j].x > 0 && map[i][j].x < WIDTH-1 && map[i][j].y > 0 || map[i][j].y < HEIGHT-1){
for(int k = 0; k < snake1_length; k++){
if(map[i][j].x == snake1[k].x && map[i][j].y == snake1[k].y){
printf("x");
}else{
printf(" ");
}
}
}
}
printf("\n");
}
}/* print_map */
我的问题是,当打印地图时,似乎在地图内打印了许多空格,因此当顶部或底部边框结束时,右边框不会开始。除了蛇尾也移动了,只有蛇头似乎在正确的位置。为了更好地理解我在这里提供的问题Console Output
【问题讨论】:
-
您根本不需要
map- 只需使用i和j计算出您要打印的y 和x 坐标。 -
最后一个
if语句看起来很可疑 - 你正在混合||和&&并且可能需要一些额外的括号(gcc 报告这是对我的警告)
标签: c for-loop output console-application