So I'm trying to print the map for my snake game. Here is the code:
#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 */
My problem is that when the map is printed it seems like to many blankspaces are printed within the map so that the right border is not beginning when the top or bottom border ends. As well as that the snakes tail is also shifted, only the snakes head seems to be in the right place. For better understanding of the problem I supply here the Console Output