学学写个智能的蛇

引言

我先放一张传说中的贪吃智能蛇
这里写图片描述

言归正传

首先,我写了个能够每秒自动走的蛇作为原型
这里写图片描述

首先,我简单介绍下kbhit()
这个东西利用if(kbhit()) getch()能够实现需要的读入
然后是时间控制
首先存一个start=time();
while(time()-start<=1000) if (kbhit()) getch()
即可实现一个很完美的输入

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define FOR(i,j,k) for(i=j;i<=k;++i)
int d[1000][1000];
char map[1000][1000] = { ' ' };
int fx, fy, lx, ly;
int len = 5;
int diffcuilty;
void pm();
int n, m, w, h;//W 1 ;A 2 ;S 3 ;D 4
void CreateTheBackground(int x, int y)
{
    int i, j, k;
    FOR(i, 1, w)
        FOR(j, 1, h)
    {
        map[i][j] = ' '; d[i][j] = 0;
    }
    FOR(i, 1, 5) {
        map[1][i] = '*';
        d[1][i] = 1;
    }
    fx = 1; fy = 5;
    map[1][5] = '%';
    lx = ly = 1;
    FOR(i, 0, h + 1) { map[0][i] = '#'; map[w + 1][i] = '#'; }
    FOR(i, 0, w + 1) { map[i][0] = '#'; map[i][h + 1] = '#'; }
    pm();
}
void pm()
{
    srand(time(NULL));
    int i, j;
    i = rand() % w + 1;
    j = rand() % h + 1;
    while (map[i][j] != ' ')
    {
        i = rand() % w + 1;
        j = rand() % h + 1;
    }
    map[i][j] = '$';
}
int getinp()//W 1 ;A 2 ;S 3 ;D 4
{
    int dr = 0;
    char c = 0;
    int tle = 1;
    int start = clock();
    while (clock() - start <= diffcuilty * 50)
    {
        if (kbhit()) c = getch();
        if (c != 0) { tle--; }
        if (c == 'w') dr = 1;
        if (c == 'a') dr = 2;
        if (c == 's') dr = 3;
        if (c == 'd') dr = 4;
        if (clock() - start > diffcuilty * 50) return dr;
    }


    if (c == 'w') dr = 1;
    if (c == 'a') dr = 2;
    if (c == 's') dr = 3;
    if (c == 'd') dr = 4;
    return dr;
}
void pp()
{
    int i, j;
    system("cls");
    for (i = h + 1; i >= 0; --i)
    {
        FOR(j, 0, w + 1)
            printf("%c", map[j][i]);
        printf("\n");
    }
}
void dead()
{
    int t;
    printf("Failture with the score:%d\n", len);
    printf("\n\n\nPress any key to exit.");
    getchar();
    exit(0);
}
void movement(int dr)
{
    int tx, ty, t;
    d[fx][fy] = dr;
    int bj = 0;
    map[fx][fy] = '*';

    if (dr == 1) fy++;
    if (dr == 2) fx--;
    if (dr == 3) fy--;
    if (dr == 4) fx++;
    if (map[fx][fy] == '$') { bj = 1; }
    if (map[fx][fy] == '*' || map[fx][fy] == '#') { dead(); return; }
    map[fx][fy] = '%';
    if (bj) {
        pm();
        len++;
        return;
    }

    t = d[lx][ly];
    map[lx][ly] = ' ';
    if (t == 1) ly++;
    if (t == 2) lx--;
    if (t == 3) ly--;
    if (t == 4) lx++;
}
int u = 1;
void done()//W 1 ;A 2 ;S 3 ;D 4
{
    int d = getinp();

    if (d != 0) u = d;
    movement(u);
    pp();
}
int main()
{
BACK:
    printf("Firstly, please input the width and height of the area:\n(The width and height are recommanded more than 5)\n");
ERROR:
    scanf("%d%d", &w, &h);
    if (w <= 5 || h <= 5) {
        printf("Input illegal,please input again\n");
        goto ERROR;
    }
ERROR2:
    printf("Please choose a diffcuilty of this game:\nThe diffcuilty from easy to diffcuilt show 1~16\n");
    scanf("%d", &diffcuilty);
    if (!(diffcuilty >= 1 && diffcuilty <= 16)) {
        printf("Input illegal,please input again\n");
        goto ERROR2;
    }
    diffcuilty = 17 - diffcuilty;
    printf("\n");
    system("cls");


    CreateTheBackground(w, h);
    pp();
    system("pause");
    int d = 1;
    while (1)
    {

        done();
    }
    return 0;
}

然后写了第一次作业

#include<stdio.h>
#include<time.h>
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'


int life = 1;
int snakey[100] = { 5,4,3,2,1 };
int snakex[100] = { 1,1,1,1,1 };
int d[1000][1000];
char map[12][12] = { "************",//da biao
"*XXXXH     *",
"* ******** *",
"*          *",
"*   ****   *",
"*   ****   *",
"*   ****   *",
"*          *",
"* ******** *",
"*          *",
"************" };
int fx, fy, lx, ly;
int len = 5;
int diffcuilty;
void pm();
int n, m, w, h;//W 1 ;A 2 ;S 3 ;D 4
void CreateTheBackground()
{
    int i, j, k;
    w = 10; h = 10;
    FOR(i, 1, w)
        FOR(j, 1, h)
    {
        if (map[i][j] != '*') {
            map[i][j] = ' ';
            d[i][j] = 0;
        }
    }
    FOR(i, 1, 4) {
        map[i][10] = 'X';
        d[i][10] = 4;
    }
    fx = 5; fy = 10;
    d[5][10] = 4;
    map[5][10] = 'H';
    lx = 1; ly = 10;
    FOR(i, 0, h + 1) { map[0][i] = '*'; map[w + 1][i] = '*'; }
    FOR(i, 0, w + 1) { map[i][0] = '*'; map[i][h + 1] = '*'; }
    pm();
}
void pm()
{
    srand(time(NULL));
    int i, j;
    i = rand() % w + 1;
    j = rand() % h + 1;
    while (map[i][j] != ' ')
    {
        i = rand() % w + 1;
        j = rand() % h + 1;
    }
    map[i][j] = '$';
}
int getinp()//W 1 ;A 2 ;S 3 ;D 4
{
    char c;
    int dr = 0;
    c = getch();
    if (c == 'W') dr = 1;
    if (c == 'A') dr = 2;
    if (c == 'S') dr = 3;
    if (c == 'D') dr = 4;
    return dr;
}
void pp()
{
    int i, j;
    for (i = h + 1; i >= 0; --i)
    {
        FOR(j, 0, w + 1)
            printf("%c", map[j][i]);
        printf("\n");
    }
}
void dead()
{
    int t;
    printf("Game Over!!!");
    //getchar();

}
void movement(int dr)
{
    int tx, ty, t;
    d[fx][fy] = dr;
    int bj = 0;
    map[fx][fy] = 'X';

    if (dr == 1) fy++;
    if (dr == 2) fx--;
    if (dr == 3) fy--;
    if (dr == 4) fx++;
    if (map[fx][fy] == '$') { bj = 1; }
    if (map[fx][fy] == 'X' || map[fx][fy] == '*') { life = 0; return; }
    map[fx][fy] = 'H';
    if (bj) {
        pm();//fang zhi jian qian
        len++;
        return;
    }

    t = d[lx][ly];
    map[lx][ly] = ' ';
    if (t == 1) ly++;
    if (t == 2) lx--;
    if (t == 3) ly--;
    if (t == 4) lx++;
}
int u = 1;
void done()//W 1 ;A 2 ;S 3 ;D 4
{
    int d = getinp();

    if (d != 0) u = d;
    movement(u);

    pp();
}
int main()
{
    CreateTheBackground();//chuang jian bei jing 
    pp();//shu chu
    int ch;
    while (life)
    {
        ch = getinp();//shu ru
        switch (ch)//gen ju shu ru de fang xiang yi dong
        {
        case 1:movement(1); break;
        case 2:movement(2); break;
        case 3:movement(3); break;
        case 4:movement(4); break;
        }
        if (life) pp();

    }
    dead();//shu chu si wang 
    return 0;
}

简单讲述下我写贪吃蛇的思路:

  • 生成背景
  • 建立指针,包括头尾和身体,用一个数组d存身体方向
  • 利用指针完成一系列操作:

前进:头指针的方向指向位移方向存于d,之后移动头指针,尾指针按照d的方向移动
吃:不移动尾指针

  • 对各种边界情况进行判断

带智能的蛇

#include<stdio.h>
#include<time.h>
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'
#define mal 0x7fffffff

int life = 1;
int snakey[100] = { 5,4,3,2,1 };
int snakex[100] = { 1,1,1,1,1 };
int d[1000][1000];
char map[12][12] = { "************",//da biao
"*XXXXH     *",
"*          *",
"*          *",
"*          *",
"*          *",
"*          *",
"*          *",
"*          *",
"*          *",
"************" };
int fx, fy, lx, ly;
int len = 5;
int mx,my;
int diffcuilty;
void pm();
int n, m, w, h;//W 1 ;A 2 ;S 3 ;D 4
char cp[12][12];
int f[12][12];
int qx[1000],qy[1000];
int qboo[12][12];
int dxx[5]={0,0,-1,0,1};
int dyy[5]={0,1,0,-1,0};
void bfs(int lx,int ly)//yi (lx,ly) kuo zhang chu bfs ju li (man ha dun ju li)
{
    int ux=lx,uy=ly;int tx,ty;
    int i,j,k;
    int be=0,en=0;
    FOR(i,1,10)
        FOR(j,1,10)
            {
                qboo[i][j]=1;
                f[i][j]=mal;
            }
    qx[0]=lx;qy[0]=ly;qboo[lx][ly]=0;
    f[lx][ly]=0;
    while(be<=en)
    {
        k=be;be++;
        ux=qx[k];
        uy=qy[k];
        qboo[ux][uy]=0;
        FOR(i,1,4)
        {
            tx=dxx[i]+ux;
            ty=dyy[i]+uy;
            if ((cp[tx][ty]==' '||cp[tx][ty]=='H')&&qboo[tx][ty])
                {
                    f[tx][ty]=f[ux][uy]+1;
                    en++;
                    qx[en]=tx;
                    qy[en]=ty;
                }
        }
    }
}
void cop()//jiang mu qian zhuang tai fu zhi gei cp[][];
{
    int i,j;
    FOR(i,0,11)
        FOR(j,0,11)
            cp[i][j]=map[i][j];
}
int ddd()//qiu fang xiang jie
{
    cop();int i,j,k=10000;int l;
    bfs(mx,my);
    if (f[fx][fy]!=mal)//ruo she tou ke yi di da
        {

            FOR(i,1,4)
                if (f[fx+dxx[i]][fy+dyy[i]]<k)
                    {
                        k=f[fx+dxx[i]][fy+dyy[i]];
                        j=i;
                    }
            return j;
        }
    else
    {
        bfs(lx,ly);
        k=-1;
        FOR(i,1,4)
                if (f[fx+dxx[i]][fy+dyy[i]]>k&&f[fx+dxx[i]][fy+dyy[i]]!=mal)
                    {
                        k=f[fx+dxx[i]][fy+dyy[i]];
                        j=i;
                    }
            return j;
    }
}


void CreateTheBackground()
{
    int i, j, k;
    w = 10; h = 10;
    FOR(i, 1, w)
        FOR(j, 1, h)
    {
        if (map[i][j] != '*') {
            map[i][j] = ' ';
            d[i][j] = 0;
        }
    }
    FOR(i, 1, 4) {
        map[i][10] = 'X';
        d[i][10] = 4;
    }
    fx = 5; fy = 10;
    d[5][10] = 4;
    map[5][10] = 'H';
    lx = 1; ly = 10;
    FOR(i, 0, h + 1) { map[0][i] = '*'; map[w + 1][i] = '*'; }
    FOR(i, 0, w + 1) { map[i][0] = '*'; map[i][h + 1] = '*'; }
    pm();
}
void pm()
{
    srand(time(NULL));
    int i, j;
    i = rand() % w + 1;
    j = rand() % h + 1;
    while (map[i][j] != ' ')
    {
        i = rand() % w + 1;
        j = rand() % h + 1;
    }
    mx=i;
    my=j;
    map[i][j] = '$';
}
int getinp()//W 1 ;A 2 ;S 3 ;D 4
{
    int start = clock();
    int dr;
    while (clock() - start <= 1000);
    dr=ddd();
    return dr;
}
void pp()
{
    int i, j;
    system("cls");
    for (i = h + 1; i >= 0; --i)
    {
        FOR(j, 0, w + 1)
            printf("%c", map[j][i]);
        printf("\n");
    }
}
void dead()
{
    int t;
    printf("Game Over!!!");
    //getchar();

}
void movement(int dr)
{
    int tx, ty, t;
    d[fx][fy] = dr;
    int bj = 0;
    map[fx][fy] = 'X';

    if (dr == 1) fy++;
    if (dr == 2) fx--;
    if (dr == 3) fy--;
    if (dr == 4) fx++;
    if (map[fx][fy] == '$') { bj = 1; }
    if (map[fx][fy] == 'X' || map[fx][fy] == '*') { life = 0; return; }
    map[fx][fy] = 'H';
    if (bj) {
        pm();//fang zhi jian qian
        len++;
        return;
    }

    t = d[lx][ly];
    map[lx][ly] = ' ';
    if (t == 1) ly++;
    if (t == 2) lx--;
    if (t == 3) ly--;
    if (t == 4) lx++;
}
int u = 1;
void done()//W 1 ;A 2 ;S 3 ;D 4
{
    int d = getinp();

    if (d != 0) u = d;
    movement(u);

    pp();
}
int main()
{
    CreateTheBackground();//chuang jian bei jing 
    pp();//shu chu
    int ch;
    int las=4;
    while (life)
    {
        ch = getinp();//shu ru
        if (!(ch>=1&&ch<=4) ) ch=las;
        las=ch;
        switch (ch)//gen ju shu ru de fang xiang yi dong
        {
        case 1:movement(1); break;
        case 2:movement(2); break;
        case 3:movement(3); break;
        case 4:movement(4); break;
        }
        if (life) pp();

    }
    dead();//shu chu si wang 
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值