| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2634 人关注过本帖, 2 人收藏
标题:[原创] [挑战] 100 行代码以内实现贪吃蛇
只看楼主 加入收藏
RockCarry
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:13
帖 子:662
专家分:58
注 册:2005-8-5
结帖率:95.65%
收藏(2)
已结贴  问题点数:1 回复次数:7 
[原创] [挑战] 100 行代码以内实现贪吃蛇
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

static void snack_feed(char *board, int bw, int bh, int ssize) {
    int rf = rand() % ((bw - 2) * (bh - 2) - ssize), i;
    for (i=0; i<bw*bh; i++) {
        if (board[i] == ' ' && !rf--) {
            board[i] = 'f'; break;
        }
    }
}

static void snack_init(char *board, int bw, int bh, int *snack, int *shead, int *stail, int *ssize) {
    int i; memset(board, ' ', bw * bh);
    for (i=0; i<bw-2; i++) board[i+1] = board[bw*bh - i - 2] = '-';
    for (i=0; i<bh-2; i++) board[bw+i*bw] = board[bw-1+bw+i*bw] = '|';
    board[0] = board[bw-1] = board[bw*(bh-1)] = board[bw*bh-1] = '+';
    board[bw/2 + bh/2*bw] = '@';
    *ssize = *shead = *stail = 1;
    snack[2] = bw/2; snack[3] = bh/2;
    snack_feed(board, bw, bh, *ssize);
}

static void snack_draw(char *board, int bw, int bh) {
    int i, j;
    system("cls"); printf("i-UP, k-DOWN, j-LEFT, l-RIGHT, q-EXIT\n\n");
    for (i=0; i<bh; i++) {
        for (j=0; j<bw; j++) printf("%c ", board[j+i*bw]);
        printf("\n");
    }
}

static int snack_move(char *board, int bw, int bh, int *snack, int *shead, int *stail, int *ssize, int d) {
    int hx = snack[*shead * 2 + 0], tx = snack[*stail * 2 + 0], ox = hx;
    int hy = snack[*shead * 2 + 1], ty = snack[*stail * 2 + 1], oy = hy;

    switch (d) {
    case 'J': case 'j': hx--; break; // left
    case 'L': case 'l': hx++; break; // right
    case 'I': case 'i': hy--; break; // up
    case 'K': case 'k': hy++; break; // down
    default : return -1;
    }

    if (board[hx + hy * bw] == '-' || board[hx + hy * bw] == '|' || (board[hx + hy * bw] == '#' && (hx != tx || hy != ty))) {
        return 'D'; // 'D' means snake die
    }

    if (board[hx + hy * bw] == 'f') {
        (*ssize)++; (*shead)++; *shead %= (bw-2)*(bh-2);
        board[ox + oy * bw] = '#'; board[hx + hy * bw] = '@';
        snack[*shead * 2 + 0] = hx; snack[*shead * 2 + 1] = hy;
        if (*ssize == (bw-2)*(bh-2)) return 'W'; // 'W' means win
        snack_feed(board, bw, bh, *ssize);
        return 'E'; // means snake eat
    }

    (*shead)++; *shead %= (bw-2)*(bh-2);
    (*stail)++; *stail %= (bw-2)*(bh-2);
    snack[*shead * 2 + 0] = hx; snack[*shead * 2 + 1] = hy;
    board[ox + oy * bw] = *ssize > 1 ? '#' : ' ';
    board[tx + ty * bw] = ' ';
    board[hx + hy * bw] = '@';
    return 'M';
}

int main(void) {
    int  width = 17, height = 11, snack[2 * (width-2) * (height-2)], shead = 0, stail = 0, ssize = 0, op = 0, reinit = 1, ret;
    char board[width * height];
    do {
        if (reinit) {
            snack_init(board, width, height, snack, &shead, &stail, &ssize);
            reinit = 0; op = 0;
        }
        ret = snack_move(board, width, height, snack, &shead, &stail, &ssize, op);
        snack_draw(board, width, height);
        switch (ret) {
        case 'D': reinit = 1; printf("\ngame over !\n\npress any key to try agin ...\n"); break;
        case 'W': reinit = 1; printf("\nyou win !\n\nYou are a clever and great snake !\n\npress any key to try agin ...\n"); break;
        }
    } while ((op = getch()) && op != 'q' && op != 'Q');
    return 0;
}


[此贴子已经被作者于2020-2-24 14:12编辑过]

搜索更多相关主题的帖子: return board case int break 
2020-02-24 13:58
RockCarry
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:13
帖 子:662
专家分:58
注 册:2005-8-5
收藏
得分:0 
snack.rar (7.45 KB)


[此贴子已经被作者于2020-2-24 14:13编辑过]

2020-02-24 14:00
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:1 
应该界定条件:
1,一个分号一行
2,除了定义变量和函数参数,执行语句不能用逗号运算符
3,可以连等,如a=b=c=0;
4,“{}”符号是否行数

不然有人一行就解决问题了

能编个毛线衣吗?
2020-02-24 14:43
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10541
专家分:42927
注 册:2014-5-20
收藏
得分:1 
回复 2楼 RockCarry
会屏闪,看着不舒服。
2020-02-24 17:32
RockCarry
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:13
帖 子:662
专家分:58
注 册:2005-8-5
收藏
得分:0 
搞成不闪,不是做不到,是要多加几十行代码。
2020-02-24 18:44
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:1 
厉害,看看

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2020-02-26 12:07
万致远醉帥
Rank: 2
等 级:论坛游民
威 望:1
帖 子:88
专家分:35
注 册:2020-3-24
收藏
得分:0 
666

我们遇到什么困难,也不要怕,微笑着面对他,消除恐惧的最好方法就是面对恐惧。
2020-03-24 14:13
maomao12345
Rank: 2
来 自:五位神奇空间
等 级:禁止访问
威 望:2
帖 子:127
专家分:64
注 册:2020-3-23
收藏
得分:0 
#include<iostream>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
using namespace std;
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout<<"按 W S A D 移动方向"<<endl;
gotoxy(N+3,2);
color(20);
cout<<"按任意键暂停"<<endl;
gotoxy(N+3,3);
color(20);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"失败!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}
我135行。

一个快乐的小小孩
2020-03-24 17:49
快速回复:[原创] [挑战] 100 行代码以内实现贪吃蛇
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017167 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved