#include<stdio.h>
#include<windows.h>
#define width 39
#define height 20

typedef struct head{
    int x;
    int y;
    int number;
    struct body *p;
} H;

typedef struct body{
    int x;
    int y;
    struct body *p;
} B;

typedef struct temp{
    int x;
    int y;
} T;

typedef struct food{
    int x;
    int y;
   
}F;

void mapping(int map[][width]);
void move(H head[],F food,int l,char choice,int i);
int game_over();
void gotoxy(int x, int y,char *c);
void snake(int l,H head[]);
void make_food();

int length=5;

int main(){
    int map[height][width]={
                    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
    };
   
    char choice=0;

    mapping(map);
    H snake_head[100];
   
    int x=18;
    int y=10;
    int i=1;
   
    snake(length,snake_head);
    F food;
   
    make_food();
   
    while(1){
       
        move(snake_head,food,length,choice,i);
        if(food.x==snake_head[0].x&&food.y==snake_head[0].y){
            make_food();
            length++;
        }
        if(snake_head[0].x==width-1||snake_head[0].x==0||snake_head[0].y==height-1||snake_head[0].y==0)
            break;
    }
    game_over();
}

void mapping(int map[][width]){
    int x,y;
    for(y=0;y<20;y++){
        for(x=0;x<39;x++){
            if(map[y][x]==1){
                printf("■");
            }
            if(map[y][x]==0){
                printf("  ");
            }
        }
        printf("\n");
    }
}

void move(H head[],F food,int l,char choice,int i){

        choice=getch();
            
        switch(choice){
            case 'w':
                while(head[0].y!=0){
                    for(i=l;i>0;i--){
                        head[i].x=head[i-1].x;
                        head[i].y=head[i-1].y;
                        gotoxy(head[i-1].x,head[i-1].y,"□");
                        if(i==l)
                        gotoxy(head[i-1].x,head[i-1].y," ");
                    }
                    head[0].y--;
                    gotoxy(head[0].x,head[0].y,"■");
                    if(food.x==head[0].x&&food.y==head[0].y){
                        make_food();
                        length++;
                    }
                    if(kbhit())
                        break;
                   
                    Sleep(50);
                }
                break;
            case 'a':
                while(head[0].x!=0){
                    for(i=l;i>0;i--){
                        head[i].x=head[i-1].x;
                        head[i].y=head[i-1].y;
                        gotoxy(head[i-1].x,head[i-1].y,"□");
                        if(i==l)
                        gotoxy(head[i-1].x,head[i-1].y," ");
                    }
                    head[0].x--;
                    gotoxy(head[0].x,head[0].y,"■");
                    if(food.x==head[0].x&&food.y==head[0].y){
                        make_food();
                        length++;
                    }
                   
                    if(kbhit())
                        break;
                       
                    Sleep(50);
                }
                break;
            case 's':
                while(head[0].y!=height-1){
                    for(i=l;i>0;i--){
                        head[i].x=head[i-1].x;
                        head[i].y=head[i-1].y;
                        gotoxy(head[i-1].x,head[i-1].y,"□");
                        if(i==l)
                        gotoxy(head[i-1].x,head[i-1].y," ");
                    }
                    head[0].y++;
                    gotoxy(head[0].x,head[0].y,"■");
                    if(food.x==head[0].x&&food.y==head[0].y){
                        make_food();
                        length++;
                    }
                   
                    if(kbhit())
                        break;
                   
                    Sleep(50);
                }
                break;
            case 'd':
                while(head[0].x!=width-1){
                    for(i=l;i>0;i--){
                        head[i].x=head[i-1].x;
                        head[i].y=head[i-1].y;
                        gotoxy(head[i-1].x,head[i-1].y,"□");
                        if(i==l)
                        gotoxy(head[i-1].x,head[i-1].y," ");
                    }
                    head[0].x++;
                    gotoxy(head[0].x,head[0].y,"■");
                   
                    if(food.x==head[0].x&&food.y==head[0].y){
                        make_food();
                        length++;
                    }
                   
                    if(kbhit())
                        break;
                       
                    Sleep(50);
                }
                break;
        }
}


int game_over(){
    system("cls");
    printf("GAME OVER");
    return 0;
}

void gotoxy(int x, int y,char *c){
    COORD Pos = { 2*x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
    printf("%s",c);
}

void snake(int l,H head[]){
    int i=1;
    head[0].x=width/2-1;
    head[0].y=height/2;

    for(;i<l;i++){
        head[i].x=head[0].x-i;
        head[i].y=head[0].y;
        head[i].number=1;
        if(head[i].number==1)
            gotoxy(head[i].x,head[i].y,"□");
    }
    gotoxy(head[0].x,head[0].y,"■");
}

void make_food(){
    F food;
    srand(time(NULL));
    food.x=(rand()%(width-1))-1;
    food.y=(rand()%(height-1))-1;
   
    gotoxy(food.x,food.y,"★");
   
}


먹이 먹어도 길이 늘어나지도 않고 먹이 생기지도 않음... 어찌해야함??