c版贪吃蛇半成品
程序代码:
#include <stdio.h>
#include <stdlib.h>//system函数的头文件
#include <conio.h> //getch函数的头文件
#include <time.h>//为了兼容性
//map[y][x]
#define TRUE 1
#define FALSE -1 //定义返回值
int create(char *address, char *person);
int move(char *address, char *move);
int showmap(char *address, char *nowperson);
int number(char *nowfood);
int main()
{
char map[19][29];//创建地图 ps:以0结尾
create(&map[0][0], &map[9][5]);//传递地图的首地址,传递代表人所在位置的地址
showmap(&map[0][0], &map[9][5]);
return 0;
}
int showmap(char *address, char *nowperson)//展示地图
{
char *a=address;
int b=0,back=0;//b为换行提供依据,back接收返回值,初始化防止错误
while (a != (address+19*28))//超出地图就停止展示
{
if ( b == 28)
{
printf("\n");
b=0;
}
if ( *a == 0)
{
printf(" ");//空白填充
a++;
}else{
if ( *a == 2)
{
printf("*");//*填充墙
a++;
}else{
if ( *a == 3)
{
printf("*");
a++;
}else{
if ( *a == 4)
{
printf("$");//$填充
a++;
}else{
printf("@");//@填充人
a++;
}
}
}
}
b++;
}
back = move(address, nowperson); //返回值
if (back == -1)//传递地图的首地址,传递代表人所在位置的地址
system("cls"),printf("\n越界!!!!\n");
return 0;
}
int create(char *address, char *person)//完成地图的初始化
{
int b=0,c=0;//c:Y b:X
char *a=address;//把地图首地址赋予给*a
while (a != (address+19*28))
{
if ( b < 28 && (c == 18 || c == 0) )//先判断是否到达(假定)墙的位置
{
*a=3;//为了美观X方向的墙赋予 3
a++;
}else{
if ( b == 28)
{
*a=2;//为了方便越界判断赋予 墙 (2)
b=0;
a++;
c++;
}else{
if (a != person)//不等于代表人所在位置的地址 赋予 0
{
*a=0;
a++;
}
else//等于代表人所在位置的地址就赋予1
{
*a=1;
a++;
}
}
}
b++;
}
return 0;
}
int move(char *address, char *move)//判断人的移动是否越界
{
char *nowperson=move;//把人所在位置的地址 赋予给 *nowperson
switch( getch() )
{
case '8':
case 'w':
nowperson -=28; //(移动后) ps:先检测移动后是否越界
if ( address<nowperson && nowperson<(address+29*19) && (*nowperson != 2 && *nowperson!= 3) )
{ *(nowperson+28)=0;//移动后清除原位置人
*nowperson=1;
number(address);
system("cls");//清除地图重新展示
showmap(address, nowperson);
}else{
return FALSE;//不满足条件就返回FALSE
}
break;
case 's':
case '5':
nowperson +=28;
if (address<nowperson && nowperson<(address+29*19) && (*nowperson != 2 && *nowperson!= 3) )
{
*(nowperson-28)=0;
*nowperson=1;
number(address);
system("cls");
showmap(address, nowperson);
}else{
return FALSE;
}
break;
case 'a':
case '4':
nowperson--;
if ( address<nowperson && nowperson<(address+29*19) && (*nowperson != 2 && *nowperson!= 3))
{
*(nowperson+1)=0;
*nowperson=1;
number(address);
system("cls");
showmap(address, nowperson);
}else{
return FALSE;
}
break;
case '6':
case 'd':
nowperson++;
if ( address<nowperson && nowperson<(address+29*19) && (*nowperson != 2 && *nowperson!= 3))
{
*(nowperson-1)=0;
*nowperson=1;
number(address);
system("cls");
showmap(address, nowperson);
}else{
return FALSE;
}
break;
}
return 0;
}
int number(char *nowfood)//对地图进行编号,以便随机产生一个食物
{
char *a=nowfood;//把食物的地址赋予 给 a
int random,food=-1;//接收随机数,food 初始化为 -1
srand(time(NULL));//置随机种子
while ( a != (nowfood+19*29)) //遍历整个地图 包括墙
{
if (a < (nowfood+19*29) && *a == 4 )//未完成遍历整个地图 并且 存在食物
{
return TRUE;
}else{
a++;
}
}
/*完成遍历整个地图,未发现食物。产生新食物*/
while (food != TRUE)//检测是否产生食物
{
a=nowfood;//重新初始化地图首地址
random = rand()%552; //551-1+1+1=552
if ( *(a + random) == 0)
{
*(a + random) = 4;//在地图随机产生个食物,并赋予 4
food = TRUE;
}
else
food = FALSE;
}
return 0;
}
本来想用指针的形式编写个贪吃蛇的结果,吃东西成长弄不来了。而且指针也不太会了。













[此贴子已经被作者于2016-10-15 20:17编辑过]




