码迷,mamicode.com
首页 > 其他好文 > 详细

迷宫游戏

时间:2017-02-18 01:05:24      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:har   while   des   desc   putchar   char   utc   return   blog   

大一寒假作业!写了第一个小游戏!

汉字变成了乱码……

//maze_test By lizitong
#include<cstdio>
#include<ctime>
#include<conio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
char a[111][111];
int coin;
int n=5,m;
int px[10011],py[10011],e;
int xs[11][11],ys[11][11],step,steplim;
char pr[111][111];
int Abs(int x)
{
	return x<0 ? (-x) : x;
}
void lose()//?D??꧰ü 
{
	system("cls");
	printf("\n\n\n\n\n\n\n\n\n\n");
	printf("\t\tYou Lose!\n");
	printf("\n\n\n\n\n\n\n\n\n\n");
	while(!kbhit());
	getch();
	exit(0);
}
void print(int x,int y)//?üD?μ?í? 
{
	if(step>steplim)
	  lose();
	for(int i=x-2,I=1;i<=x+2;++i,++I)
	  for(int j=y-2,J=1;j<=y+2;++j,++J)
	    if(a[i][j]==‘#‘ || a[i][j]==‘*‘ || a[i][j]==‘ ‘)
	      {
	      	for(int k=xs[I][J];k<=xs[I][J]+2;++k)
	      	  for(int l=ys[I][J];l<=ys[I][J]+2;++l)
	      	    pr[k][l]=a[i][j];
	      }
	    else if(a[i][j]==‘G‘)
	      {
	      	for(int k=xs[I][J];k<=xs[I][J]+2;++k)
	      	  for(int l=ys[I][J];l<=ys[I][J]+2;++l)
	      	    pr[k][l]=‘ ‘;
	      	pr[xs[I][J]+1][ys[I][J]+1]=‘G‘;
	      }
	    else if(a[i][j]==‘o‘)
	      {
	      	for(int k=xs[I][J];k<=xs[I][J]+2;++k)
	      	  for(int l=ys[I][J];l<=ys[I][J]+2;++l)
	      	    pr[k][l]=‘ ‘;
	      	pr[xs[I][J]+1][ys[I][J]+1]=‘o‘;
	      }
	    else if(i<0 || i>n+1 || j<0 || j>n+1)
	      {
	      	for(int k=xs[I][J];k<=xs[I][J]+2;++k)
	      	  for(int l=ys[I][J];l<=ys[I][J]+2;++l)
	      	    pr[k][l]=‘ ‘;
	      }
	for(int i=1;i<=20;++i)
	  {
	  	for(int j=1;j<=20;++j)
	  	  putchar(pr[i][j]),putchar(‘ ‘);
	  	puts("");
	  }
	printf("\n\n");
	printf("\t2?êy?T??£o%d\t??μ??e±òêy£o%d\t??μ?2?êy£o%d\n",steplim,coin,step++);
}
int main()
{
	//ê?3?ó??·?μ?÷ 
	printf("\n\n\n\n\n");
	printf("\t?aê?ò?????1?ó??·£?\n");
	printf("\t?°#?±′ú±í??±ú£??°*?±′ú±í??????1?μ?±???£??aá???μ?·???2??ü2è줣?\n");
	printf("\t?°o?±′ú±í??μ?????£?ó?·??ò?ü??????μ?ò??ˉ£?\n");
	printf("\tμ±??×?μ?3??ú(?′±???μ?è±?ú′|)ê±£????í??ê¤á?£???????1???óDò???3??ú£?\n");
	printf("\t??Dèòa?ú1??¨μ?2?êyò??ú×?3???1?£???????μ????èμè????′ó£?1??¨μ?2?êy??éù£?\n");
	printf("\t???éò??ú??1?à???μ??e±ò£?ò??°G?±±íê?£?\n");
	printf("\t??×???μ?μ?·????ù?Y??1??á11?¢ê£óà2?êy?¢??????μ????èμè???¢\n\t??μ?μ??e±òêy??DD????£?\n");
	printf("\n\n");
	printf("\t\t×÷??£oà?×óí¨£?μ?×ó????′ó?§ ?????ú???§ó?1¤3ì?§?o\n\n\t\t2017?ê2??");
	printf("\n\n\n\t°′è?òa?ü?ìD?\n");
	printf("\n\n\n\n");
	while(!kbhit());
	getch();
	system("cls");
	printf("\n\n\n\n\n\n\n\n\n");
	printf("\t??ê?è???1?′óD?(???T?a5£?é??T?a30)\n\n\tD?óú5?á??è???1?′óD??a5?á5£?′óóú30?á??è???1?′óD??a30?á30\n\n\t°′????3μè·è?\n\n\t");
	scanf("%d",&n);
	if(n<5) n=5;
	if(n>30) n=30;
	system("cls");
	printf("\n\n\n\n\n\n\n\n\n");
	printf("\t??ê?è????èμè??(???T?a1£?é??T?a3)\n\n\tD?óú1?á??è??a???è1£?′óóú3?á??è??a???è3\n\n\t°′????3μè·è?\n\n\t");
	scanf("%d",&m);
	if(m<1) m=1;
	if(m>3) m=3;
	int xishu[4]={0,1,3,9};
	system("cls");
	printf("\n\n\n\n\n\n\n\n\n\n\n");
	printf("\t°′è?òa?ü?aê?ó??·£???1?′óD?£o%d?á%d ???èμè??£o%d\n\n\t",n,n,m);
	while(!kbhit());
	getch();
	system("cls");
	
	//???úéú3é??1?μ?í? 
	for(int i=1;i<=n;++i)
	  for(int j=1;j<=n;++j)
	    a[i][j]=‘#‘;
	for(int i=0;i<=n+1;++i)
	  a[0][i]=a[n+1][i]=‘*‘;
	for(int i=1;i<=n;++i)
	  a[i][0]=a[i][n+1]=‘*‘;
	px[++e]=1;
	py[e]=1;
	px[++e]=n;
	py[e]=n;
	srand(time(0));
	int x=1,y=1;
	while(x<=n&&y<=n)//?è11?ìò?ì?′ó×óé?μ?óò??μ?í¨?· 
	  {
	  	a[x][y]=‘ ‘;
	  	if(x==n) ++y;
	  	else if(y==n) ++x;
	  	else{
	  	bool dir=rand()%2;
	  	if(dir)
	  	  ++x;
	  	else
	  	  ++y;
	  	}
	  }
	//???úéú3é????×üêy 
	int space_sum=n*2-1;
	int chushu=rand()%5+1;
	while(chushu<2)
	  chushu=rand()%5+1;
	chushu=2;
	int lim=n*n/chushu;
	if(lim<space_sum)
	  lim=space_sum;
	//?ù?Y???èéú3é2?êy?T?? 
	int xishu2[4];
	if(n<10)
	  xishu2[3]=3,xishu2[2]=5,xishu2[1]=8;
	else if(n<20)
	  xishu2[3]=5,xishu2[2]=7,xishu2[1]=11;
	else
	  xishu2[3]=7,xishu2[2]=9,xishu2[1]=14;
	if(m==1)
	  steplim=(n*2-1)*xishu2[m]/2-(lim-space_sum)/5;
	else if(m==2)
	  steplim=(n*2-1)*xishu2[m]/2-(lim-space_sum)/4;
	else
	  steplim=(n*2-1)*xishu2[m]/2-(lim-space_sum)/3;
	while(space_sum<lim)//???ú??DDíú?ò 
	  {
	  	int x=rand()%n+1,y=rand()%n+1;
	  	while(a[x][y]==‘#‘)
	  	  {
	  	  	x=rand()%n+1;
	  	  	y=rand()%n+1;
	  	  }
	  	int op=rand()%4;
	  	x=x+dx[op];
	  	y=y+dy[op];
	  	if(a[x][y]==‘#‘){
	  	while(x>=1 && x<=n && y>=1 && y<=n)
	  	  {
	  	  	if(a[x][y]==‘ ‘)
	  	  	  break;
	  	  	int rnd=rand()%(n/2); 
	  	  	if(!rnd)
	  	  	  break;
	  	  	a[x][y]=‘ ‘;
	  	  	if(x==1 || x==n || y==1 || y==n)
	  	  	  {
	  	  	  	px[++e]=x;
	  	  	  	py[e]=y;
	  	  	  }
	  	  	++space_sum;
	  	  	if(space_sum==lim)
	  	  	  break;
	  	  	x=x+dx[op];
	  		y=y+dy[op];
	  	  }
	  	}
	  }
	
	//???úéú3éó2±ò 
	int coins=rand()%(lim/5)+1;
	int sidescnt=0;
	for(int i=1;i<=coins;++i)
	  {
	  	x=rand()%n+1; y=rand()%n+1;
		while(a[x][y]!=‘ ‘ || ((x==1 || x==n ||y==1 || y==n) && sidescnt==e-1))
	  	  x=rand()%n+1,y=rand()%n+1;
	  	a[x][y]=‘G‘;
	  	if(x==1 || x==n || y==1 || y==n)
	  	  ++sidescnt;
	  }
	
	//???ú11?ì?eμ???μ? 
	int id=rand()%e+1;
	int id2=rand()%e+1;
	while(id2==id || Abs(px[id]-px[id2])+Abs(py[id]-py[id2])<=n/2 || a[px[id]][py[id]]!=‘ ‘)
	  {
	  	id=rand()%e+1;
	  	id2=rand()%e+1;
	  }
	
	int nowx=px[id],nowy=py[id],aimx,aimy;
	
	if(px[id2]==1) a[0][py[id2]]=‘ ‘,aimx=0,aimy=py[id2];
	else if(px[id2]==n) a[n+1][py[id2]]=‘ ‘,aimx=n+1,aimy=py[id2];
	else if(py[id2]==1) a[px[id2]][0]=‘ ‘,aimx=px[id2],aimy=0;
	else a[px[id2]][n+1]=‘ ‘,aimx=px[id2],aimy=n+1;
//	for(int i=0;i<=n+1;++i)
//	  {
//	  	for(int j=0;j<=n+1;++j)
//	  	  putchar(a[i][j]);
//	  	puts("");
//	  }
//	  
//	puts("");puts("");puts("");
	
	for(int i=1;i<=20;++i)
	  for(int j=1;j<=20;++j)
	    pr[i][j]=‘ ‘;
	for(int i=1,I=1;i<=5;++i,I+=4)
	  for(int j=1,J=1;j<=5;++j,J+=4)
	    {
	      xs[i][j]=I;
	      ys[i][j]=J;
	    }
	
	a[nowx][nowy]=‘o‘;
	
	print(nowx,nowy);
	
	while(1)
	  {
	  	//·??ò?ü????£?′ó?ü?ì??DD?áè? 
	  	int ttx,tty;
	  	while(!kbhit());
	  	getch(); char ch=getch();
	  	if(ch==72) ttx=nowx-1,tty=nowy;
	  	else if(ch==80) ttx=nowx+1,tty=nowy;
	  	else if(ch==75) ttx=nowx,tty=nowy-1;
	  	else if(ch==77) ttx=nowx,tty=nowy+1;
	  	else
	  	  continue;
	  	if((a[ttx][tty]!=‘ ‘ && a[ttx][tty]!=‘G‘) || ttx<0 || ttx>n+1 || tty<0 || tty>n+1)
	  	  continue;
	  	system("cls");
	  	a[nowx][nowy]=‘ ‘;
	  	nowx=ttx; nowy=tty;
	  	if(a[nowx][nowy]==‘G‘)
	  	  ++coin;
	  	a[nowx][nowy]=‘o‘;
	  	print(nowx,nowy);
	  	if(nowx==aimx && nowy==aimy)//?D??μ?′???μ? 
	  	  {
	  	  	if(step-1>steplim)
	  	  	  lose();
	  	  	//ê?3?×???μ?·??é?? 
	  	  	system("cls");
	  	  	printf("\n\n\n\n\n\n\n\n");
	  	  	printf("\t\tYou Win!\n\n");
	  	  	printf("\t\t??ó?á?%d2?×?3???1?\n\n\t\t??μ?μ?á?%d???e±ò\n\n\t\t??μ?μ?·?ê?%d£?1§?2??£?\n",step-1,coin,(steplim-step+1+coin*5+Abs(px[id]-px[id2])+Abs(px[id]+px[id2]))*100*xishu[m]*n/5);
	  	  	printf("\n\n\n\n\n\n");
	  	  	while(!kbhit());
	  	  	getch();
	  	  	break;
	  	  }
	  }
	return 0;
}

迷宫游戏

标签:har   while   des   desc   putchar   char   utc   return   blog   

原文地址:http://www.cnblogs.com/autsky-jadek/p/6412078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!