功能異常簡單的貪喫蛇(隊列實現)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define u 0
#define r 1
#define l 2
#define d 3
#define fail 0
#define success 1
#define maxsize 100
int Size=0;
int difficulty=0;
int dx[4]={-1,0,0,1};
int dy[4]={0,1,-1,0};
int map[maxsize][maxsize];
typedef struct body{
    int x;
    int y;
}body;
typedef struct snake{
    int size,n,head,tail,dir;
    body que[maxsize*maxsize+1];
}snake;
typedef struct food{
    int x;
    int y;
}food;
food apple;
snake s;
void GoToXY(int x, int y)
{
       COORD pos = {x,y};                 
       HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);    
       SetConsoleCursorPosition(hOut, pos);      
}
int printxy(int x,int y,char c)
{
    GoToXY(y,x);
    printf("%c",c);
    return 0;
}
int initmap()
{
    int i,j;
    for (i=1;i<Size-1;i++)
        for (j=1;j<Size-1;j++)
            map[i][j]=0;
    for (j=0;j<Size;j++)
    {
        map[0][j]=1;
        printxy(0,j,'#');
        map[Size-1][j]=1;
        printxy(Size-1,j,'#');
    }
    for (i=0;i<Size;i++)
    {
        map[i][0]=1;
        printxy(i,0,'#');
        map[i][Size-1]=1;
        printxy(i,Size-1,'#');
    }
    return 0;
}
int dropapple()
{
    int x=apple.x;
    int y=apple.y;
    int nx=rand()%(Size-2)+1;
    int ny=rand()%(Size-2)+1;
    while (map[nx][ny])
    {
        nx=rand()%(Size-2)+1;
        ny=rand()%(Size-2)+1;
    }
    apple.x=nx;
    apple.y=ny;
    printxy(apple.x,apple.y,'@');
    return 0;
}
int initgame()
{
    s.size=Size*Size+1;
    s.n=1;
    s.head=0;
    s.tail=0;
    s.que[0].x=Size/2;
    s.que[0].y=Size/2;
    s.dir=u;
    map[Size/2][Size/2]=1;
    time_t t;
    srand((unsigned)time(&t));
    int i,j;
    return 0;
}
int enque(int dir)
{
    int x,y;
    x=s.que[s.tail].x+dx[dir];
    y=s.que[s.tail].y+dy[dir];
    if (map[x][y])
    { 
        printxy(x,y,'!'); 
        return 1;
    } 
    s.tail=s.tail==s.size-1?0:s.tail+1;
    s.que[s.tail].x=x;
    s.que[s.tail].y=y;
    map[x][y]=1;
    printxy(x,y,'*');
    return 0;
}
int deque()
{
    int x,y;
    x=s.que[s.head].x;
    y=s.que[s.head].y;
    s.head=s.head==s.size-1?0:s.head+1;
    map[x][y]=0;
    printxy(x,y,' ');
    return 0;
}
int moves()
{
    int crash;
    crash=enque(s.dir);
    if (s.que[s.head].x==apple.x && s.que[s.head].y==apple.y)
    {
        if (crash)
            return fail;
        else
            return success;
    }
    else
    {
        deque();
        if (crash)
            return fail;
        else
            return -1; 
    }
}
int playgame()
{
    dropapple();
    while (1)
    {
        if (kbhit())
        {
            char c=getch();
            int dir;
            switch(c){
            case 'w':dir=u;break;
            case 'a':dir=l;break;
            case 's':dir=d;break;
            case 'd':dir=r;break;
            case 'q':return 0;
            default :dir=-1;
            }
            if (dir>=0 && dir!=3-s.dir)
                s.dir=dir;
        }
        switch (moves(s)){
        case fail:
            GoToXY(0,Size);
            return 0;
        case success:
            dropapple();
            break;
        default :break;
        }
        sleep(1/(double)difficulty);
    }
}
int question()
{
    while (Size<20 || Size>40)
    {
        printf("please input map size(20~40):");
        scanf("%d",&Size);
    }
    while (difficulty<10 || difficulty>1000)
    {
        printf("\nplease input diffculty(10~1000,100 is recommended):");
        scanf("%d",&difficulty);
    }
    system("cls");
}
int main(void)
{
    question();
    initmap();
    initgame();
    playgame();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章