贪食蛇(文本模式)

类别:编程语言 点击:0 评论:0 推荐:

贪食蛇(文本模式)

/**********************************************/
/* 程序名称:贪食蛇1.1(文本模式)            */
/* 程序作者:Knocker                          */
/* 完成日期:2004.10.20                       */
/* 编译环境:Windows me + Win-TC 1.9.1        */
/* Email   : [email protected]                */
/* 程序说明:有两种死法,一:蛇首被困闷死      */
/*                     二:咬到自己尾巴       */
/*           方向键控制蛇行方向               */
/* 修改说明:2004.10.21优化了print_mop()      */
/**********************************************/
#include <stdio.h>
#include <dos.h>

#define maxX  76
#define maxY  22

void exitt();
void moveadd(int*A,int*x,int*y);
void findend(int x,int y);
void rand_have();
void print_mop();

int X=0,Y=1,X1=0,Y1=0,a=2 ;
/*X,Y 蛇头所在数组下标,a表示蛇的运动方向,
                  0,1,2,3,分别标记蛇的运动方向上,右,下,左*/
int endX0=5,endY0=5;
int t=1 ;/*食物产生控制旗标*/
char mop[maxY][maxX]={0};
int main()
{
    char c ;
    union REGS regs;

    regs.h.ah=1;
    regs.h.ch=13;
    regs.h.cl=0;
    int86(0x10,&regs,&regs);

    mop[0][0]=-1 ;    /*-1标记蛇尾*/
    mop[0][1]=4 ;
    rand_have();
    while(1)
    {
        while(bioskey(1)==0)
        {   X1=X,Y1=Y;
            moveadd(&a,&X,&Y);
            print_mop();
            if(t==0)rand_have(),t=1 ;
            delay(3500);
        }
        c=bioskey(0)>>8 ;
        if(c==75)a=3 ;
        if(c==77)a=1 ;
        if(c==72)a=0 ;
        if(c==80)a=2 ;
    }
    
}
void moveadd(int*A,int*X,int*Y)
{
    int tmep;

    if(*A==0)
    if(*X-1>=0&&(!mop[*X-1][*Y]||mop[*X-1][*Y]==10||mop[*X-1][*Y]==-1))
    {
        if(mop[*X-1][*Y]==-1)exitt();
        if(mop[*X-1][*Y]!=10)findend(*X,*Y);
        else t=0 ;
        mop[*X-1][*Y]=(*A+2)%4+1 ;
        *X=*X-1 ;
    }
    else *A=rand()%2?3:1;

    if(*A==2)
    if(*X+1<maxY&&(!mop[*X+1][*Y]||mop[*X+1][*Y]==10||mop[*X+1][*Y]==-1))
    {
        if(mop[*X+1][*Y]==-1)exitt();
        if(mop[*X+1][*Y]!=10)findend(*X,*Y);
        else t=0 ;
        mop[*X+1][*Y]=(*A+2)%4+1 ;
        *X=*X+1 ;
    }
    else *A=rand()%2?1:3;

    if(*A==1)
    if(*Y+1<maxX&&(!mop[*X][*Y+1]||mop[*X][*Y+1]==10||mop[*X][*Y+1==-1]))
    {
        if(mop[*X][*Y+1]==-1)exitt();
        if(mop[*X][*Y+1]!=10)findend(*X,*Y);
        else t=0 ;
        mop[*X][*Y+1]=(*A+2)%4+1 ;
        *Y=*Y+1 ;
    }
    else*A=rand()%2?0:2 ;

    if(*A==3)
    if(*Y-1>=0&&(!mop[*X][*Y-1]||mop[*X][*Y-1]==10||mop[*X][*Y-1]==-1))
    {
        if(mop[*X][*Y-1]==-1)exitt();
        if(mop[*X][*Y-1]!=10)findend(*X,*Y);
        else t=0 ;
        mop[*X][*Y-1]=(*A+2)%4+1 ;
        *Y=*Y-1 ;
    }
    else*A=rand()%2?2:0 ;
}
void findend(int x,int y)
{
    switch(mop[x][y]-1)
    {
        case 0 :
        if(mop[x-1][y]==-1)
        {
            mop[x-1][y]=0 ;
            endX0=x-1;
            endY0=y;
            mop[x][y]=-1 ;
        }
        else findend(x-1,y);
        break ;
        case 1 :
        if(mop[x][y+1]==-1)
        {
            mop[x][y+1]=0 ;
            endX0=x;
            endY0=y+1;
            mop[x][y]=-1 ;
        }
        else findend(x,y+1);
        break ;
        case 2 :
        if(mop[x+1][y]==-1)
        {
            mop[x+1][y]=0 ;
            endX0=x+1;
            endY0=y;
            mop[x][y]=-1 ;
        }
        else findend(x+1,y);
        break ;
        case 3 :
        if(mop[x][y-1]==-1)
        {
            mop[x][y-1]=0 ;
            endX0=x;
            endY0=y-1;
            mop[x][y]=-1 ;
        }
        else findend(x,y-1);
        break ;
    }
}

void rand_have()
{
    int x,y ;
    
    qq :
    x=rand()%maxY ;
    y=rand()%maxX ;
    if(mop[x][y]==0)
    {
        mop[x][y]=10 ;
        gotoxy(y+1,x+2);
        printf("X");
    }
    else goto qq ;
}
void print_mop()
{

    gotoxy(endY0+1,endX0+2);
    printf(" ");
    gotoxy(Y+1,X+2);
    printf("O");
    gotoxy(Y1+1,X1+2);
    printf("o");
}
void exitt()
{
    gotoxy(10,10);
    printf("The game is over!");
    getch();
    exit();
}

本文地址:http://com.8s8s.com/it/it25593.htm