文章链接:无聊の小游戏:数码追猎
特色:智能一步一BFS寻路AI,随机生成障碍物,特殊技能(时停),地图自定义大小(记得开大地图时运行界面全屏),以及——将近四百行的又臭又长的代码。
制作时间短,日后也许会追加内容()
这里奉上代码
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
int dx[4]={-1,1,0,0},dy[4]={0,0,1,-1};
const int inf=1e9+5;
int mp[200][200];
int block[200][200];
char trans[5000];
int x,y,ex,ey,px,py;
int skill1time=3;
int siz=10;
int checkvis[200][200];
int checkcnt=0;
int distnow;
struct node{
int x;
int y;
int dis;
};
void cls()
{
system("cls");
}
void gotoxy(int mx,int my)
{
COORD pos={mx,my};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
return;
}
bool outof(int cx,int cy)
{
return (cx>0&&cy>0&&cx<=siz&&cy<=siz&&block[cx][cy]==0);
}
bool outof2(int cx,int cy)
{
return (cx>=x-1&&cy>=y-1&&cx<=x+1&&cy<=y+1);
}
void checkroad(int nx,int ny)
{
if(checkvis[nx][ny]==1||outof(nx,ny)==0)
return;
if(nx==px&&ny==py){
checkcnt++;
return;
}
checkvis[nx][ny]=1;
checkroad(nx+1,ny);
checkroad(nx,ny+1);
checkroad(nx-1,ny);
checkroad(nx,ny+1);
}
int roadcheck2(int nx,int ny)
{
queue<node> q;
q.push((node){nx,ny,0});
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==x&&now.y==y)
{
return now.dis;
}
for(int i=0;i<4;i++)
{
int vx=now.x+dx[i],vy=now.y+dy[i];
if(checkvis[vx][vy]!=1&&outof(vx,vy)==1){
checkvis[vx][vy]=1;
q.push((node){vx,vy,now.dis+1});
}
}
}
return inf;
}
int enemycheck(int nex,int ney)
{
memset(checkvis,0,sizeof(checkvis));
return roadcheck2(nex,ney);
}
char getkey()
{
char key;
while(1)
{
key=0;
if(_kbhit()){
key=getch();
break;
}
}
if('A'<=key&&key<='Z')
key=key-('A'-'a');
return key;
}
char settrans()
{
trans[0]=' ';
trans[1]='O';
trans[2]='X';
trans[3]='V';
trans[4]='|';
}
void ptmap()
{
for(int i=1;i<=siz;i++){
for(int j=1;j<=siz;j++)
printf("%c ",trans[mp[i][j]]);
printf("\n");
}
cout<<endl;
cout<<"You are O, your enemy is X, you need to get V"<<endl;
cout<<"Use WASD to move"<<endl;
cout<<"Here are some skills you can use:"<<endl;
cout<<"1.Press E: Time Freeze. ("<<skill1time<<")"<<endl;
}
void setmap()
{
memset(mp,0,sizeof(mp));
for(int i=1;i<=siz;i++){
for(int j=1;j<=siz;j++)
{
if(block[i][j]==1){
mp[i][j]=4;
continue;
}
if(x==i&&y==j)
mp[i][j]=1;
if(ex==i&&ey==j)
mp[i][j]=2;
if(px==i&&py==j)
mp[i][j]=3;
}
}
}
void enemymove(){
int dist1=enemycheck(ex,ey);
int dist2=inf;
node choicemove=(node){ex,ey};
for(int i=0;i<4;i++)
{
int vx=ex+dx[i],vy=ey+dy[i];
if(outof(vx,vy)==0)
continue;
if(choicemove.x==ex&&choicemove.y==ey)
choicemove=(node){vx,vy,0};
dist2=enemycheck(vx,vy);
if(dist2<dist1)
choicemove=(node){vx,vy,0};
}
ex=choicemove.x;
ey=choicemove.y;
if(x==ex&&y==ey)
{
return;
}
}
void puttres()
{
int anum=0;
while(1){
srand(time(NULL));
ex=(abs(rand()+anum)%siz)+1;
srand(time(NULL));
ey=(abs(rand()+anum)%siz)+1;
if(enemycheck(ex,ey)>=siz)
return;
anum++;
}
}
void putblock()
{
int ranum=3;
while(1){
memset(checkvis,0,sizeof(checkvis));
checkcnt=0;
ranum++;
for(int i=1;i<=siz;i++)
{
for(int j=1;j<=siz;j++)
{
srand(time(NULL));
if(((i+j)/i+j/ranum+i*j+rand())%ranum==0)
block[i][j]=1;
else
block[i][j]=0;
}
}
checkroad(1,1);
if(checkcnt>=3)
return;
}
block[px][py]=0;
block[1][1]=0;
block[ex][ey]=0;
}
void skill1()
{
cls();
int vx=x,vy=y;
while(1){
gotoxy(0,0);
for(int i=1;i<=siz;i++){
for(int j=1;j<=siz;j++)
{
if(vx==i&&vy==j)
printf("O ");
else if(i<=x+1&&j<=y+1&&i>=x-1&&j>=y-1&&!(i==ex&&j==ey)&&!(i==px&&j==py)&&block[i][j]==0)
printf(" ");
if((vx==i&&vy==j)||(i<=x+1&&j<=y+1&&i>=x-1&&j>=y-1&&!(i==ex&&j==ey)&&!(i==px&&j==py)&&block[i][j]==0))
continue;
if(mp[i][j]==0)
{
printf(". ");
continue;
}
printf("%c ",trans[mp[i][j]]);
}
printf("\n");
}
cout<<endl;
cout<<"Time Freeze! You can move freely within a range of 3 * 3. "<<endl;
cout<<"You won't fail during this period of time."<<endl;
cout<<"Press E to end this state."<<endl;
int fl=1;
while(fl){//移动侦测
char nk=getkey();
if(nk=='w'&&(outof(x-1,vy))&&(outof2(vx-1,vy))){
fl=0;vx--;
}
if(nk=='s'&&(outof(vx+1,vy))&&(outof2(vx+1,vy))){
fl=0;vx++;
}
if(nk=='a'&&(outof(vx,vy-1))&&(outof2(vx,vy-1))){
fl=0;vy--;
}
if(nk=='d'&&(outof(vx,vy+1))&&(outof2(vx,vy+1))){
fl=0;vy++;
}
if(nk=='e'){
x=vx;y=vy;
cls();
return;
}
}
}
}
void choosesiz()
{
int nowchoice=1;
while(1)
{
gotoxy(0,0);
cout<<"Please choose the size of the game map."<<endl;
for(int i=1;i<=4;i++){
if(i==nowchoice)
cout<<"-> ";
else
cout<<" ";
cout<<i*10<<" * "<<i*10<<endl;
}
if(5==nowchoice)
cout<<"-> ";
else
cout<<" ";
cout<<"Enter the size yourself."<<endl;
cout<<"(Press S,W to choose, press E to confirm)"<<endl;
char nchr=getkey();
if(nchr=='s'&&nowchoice<5)
nowchoice++;
if(nchr=='w'&&nowchoice>1)
nowchoice--;
if(nchr=='e')
{
if(nowchoice==5)
{
cls();
cout<<"Plese input the size:";
cin>>siz;
cls();
return;
}
else
{
siz=nowchoice*10;
return;
}
}
}
}
int main()
{
loop_start:
choosesiz();
skill1time=siz/4+1;
cls();
cout<<"Loading map......"<<endl;
settrans();
py=siz,px=siz;
x=1,y=1;
puttres();
putblock();
int step=0;
cls();
while(1)
{
gotoxy(0,0);
setmap();
ptmap();
bool fl=1;
while(fl){//移动侦测
char nk=getkey();
if(nk=='w'&&(outof(x-1,y))){
fl=0;x--;
}
if(nk=='s'&&(outof(x+1,y))){
fl=0;x++;
}
if(nk=='a'&&(outof(x,y-1))){
fl=0;y--;
}
if(nk=='d'&&(outof(x,y+1))){
fl=0;y++;
}
if(nk=='e'&&skill1time>0){
skill1time--;
skill1();
goto loop;
}
}
step++;
srand(time(NULL));
if(step%(rand()%2+1)==0)
{
srand(time(NULL));
for(int i=1;i<=(step+rand())%3+1;i++)
enemymove();
}
loop:
if(x==px&&y==py)
{
char keyn=' ';
while(keyn!='q'&&keyn!='r'){
cls();
cout<<"YOU WIN!"<<endl;
cout<<"press \"q\"to quit"<<endl;
cout<<"press \"r\"to restart"<<endl;
keyn=getkey();
}
cls();
if(keyn=='r')
goto loop_start;
return 0;
}
if(x==ex&&y==ey)
{
char keyn=' ';
while(keyn!='q'&&keyn!='r'){
cls();
cout<<"YOU FAIL!"<<endl;
cout<<"press \"q\"to quit"<<endl;
cout<<"press \"r\"to restart"<<endl;
keyn=getkey();
}
cls();
if(keyn=='r')
goto loop_start;
return 0;
}
}
}