请准备devc++或其他c++编程软件
运行程序后会有提示
有不懂的可以发评论或关注私聊
有其他意见可以在评论区提出,一定改进。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x,int y)
{
coord.X=y;
coord.Y=x;
SetConsoleCursorPosition(hout,coord);
};
void hide()
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(hout, &cursor_info);
}
double random(double start, double end)
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
int m,n;
char t;
struct node
{
int x,y;
}sn[1000];
int sn_l,d;
node f;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void pw()
{
cout << " ";
for (int i=1;i<=n;i++)
cout << "-";
cout << endl;
for (int j=0;j<=m-1;j++)
{
cout << "|";
for (int i=1;i<=n;i++) cout << " ";
cout << "|" << endl;
}
cout << " ";
for (int i=1;i<=n;i++)
cout << "-";
}
void ps()
{
locate(sn[0].x,sn[0].y);
cout << t;
for (int i=1;i<=sn_l-1;i++)
{
locate(sn[i].x,sn[i].y);
cout << "*";
}
}
bool ic()
{
if (sn[0].x==0 || sn[0].y==0 || sn[0].x==m+1 || sn[0].y==n+1) return false;
for (int i=1;i<=sn_l-1;i++)
{
if (sn[0].x==sn[i].x && sn[0].y==sn[i].y) return 0;
}
return 1;
}
bool pf()
{
srand((unsigned)time(0));
bool e;
while (1)
{
e=true;
int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
f.x=i;f.y=j;
for (int k=0;k<=sn_l-1;k++)
{
if (sn[k].x==f.x && sn[k].y==f.y)
{
e=false;break;
}
}
if (e) break;
}
locate(f.x,f.y);
cout << "$";
return true;
}
bool go_ahead()
{
node temp;
bool e=false;
temp=sn[sn_l-1];
for (int i=sn_l-1;i>=1;i--)
sn[i]=sn[i-1];
sn[0].x+=direct[d][0];
sn[0].y+=direct[d][1];
locate(sn[1].x,sn[1].y);
cout << "*";
if (sn[0].x==f.x && sn[0].y==f.y)
{
sn_l++;
e=true;
sn[sn_l-1]=temp;
}
if (!e)
{
locate(temp.x,temp.y);
cout << " ";
}
else
pf();
locate(sn[0].x,sn[0].y);
cout << t;
if (!ic())
{
system("cls");
cout << "输了哦" << endl << "钱 " << sn_l << endl;
return 0;
}
return 1;
}
int main()
{
cout <<"请将窗口调大,以免发生错位"<<endl;
cout <<"先选择难度,请在1-15中输入1个数,越大越难"<<endl;
cout <<"输入一个头"<<endl;
cout <<"以方向键控制方向"<<endl;
cout <<"不可以撞墙哦!"<<endl;
m=25;
n=40;
if (m<10 || n<10 || m>25 || n>40)
{
cout << "ERROR" << endl;
system("pause");
return 0;
}
int h;
cin >>h;
cin >>t;
if (h<=0 || h>100)
{
cout << "ERROR" << endl;
system("pause");
return 0;
}
sn_l=5;
clock_t a,b;
char ch;
double hl;
for (int i=0;i<=4;i++)
{
sn[i].x=1;
sn[i].y=5-i;
}
d=3;
system("cls");
hide();
pw();
pf();
ps();
locate(m+2,0);
cout << "现在的钱 ";
while (1)
{
hl=(double)sn_l/(double) (m*n);
a=clock();
while (1)
{
b=clock();
if (b-a>=(int)(400-30*h)*(1-sqrt(hl))) break;
}
if (kbhit())
{
ch=getch();
if (ch==-32)
{
ch=getch();
switch(ch)
{
case 72:
if (d==2 || d==3)
d=0;
break;
case 80:
if (d==2 || d==3)
d=1;
break;
case 75:
if (d==0 || d==1)
d=2;
break;
case 77:
if (d==0 || d==1)
d=3;
break;
}
}
}
if (!go_ahead()) break;
locate(m+2,12);
cout << sn_l;
}
system("pause");
return 0;
}
谢谢观看