跳到正文

个人记录

绝地塔防 v.1.1000

OIerLCY约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 15 分钟

版本

  1. v1当前

认准LCY正版

C++
#include<bits/stdc++.h>
#include<windows.h>
#define int long long
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
using namespace std;
struct node
{
	int x,y,kind,t;
	int fx,fy,hp;
	bool vis;
};
struct edge
{
	int x,y;
}pre[30][105];
node gun[10005],monster[10005];
char a[30][105];
bool flag=false,vis[30][105];
int cd[30][105];
edge d[10];
edge road[3005];
int mo[30][105];
int len=0,money,head=0,tail=1,cct=0,tt=1;
int hp=20,cd1=50,cdm=10,cd2=150,cd3=200;
void monkey_sort()
{
	int n=rand()%100+1;
	while(n--)
	{
		for(int i=1;i<=4;i++)
		{
			for(int j=1;j<=4;j++)
			{
				if(rand()%2==1) swap(d[i],d[j]);
			}
		}
	}
	return;
}
void print(int x,int y)
{
	if(x==0&&y==0) return;
	road[++len]={x,y};
	print(pre[x][y].x,pre[x][y].y);
	a[x][y]='#';
	return;
}
bool check(int x,int y,int fx,int fy)
{
	for(int i=1;i<=4;i++)
	{
		if(x+d[i].x==fx&&y+d[i].y==fy) continue;
		if(vis[x+d[i].x][y+d[i].y]==true) return false;
	}
	return true;
}
void dfs(int x,int y,int fx,int fy)
{
	if(flag==true) return;
	if(x<1||x>25||y<1||y>100) return;
	if(vis[x][y]) return;
	if(check(x,y,fx,fy)==false) return;
	pre[x][y].x=fx,pre[x][y].y=fy;
	if(x==1&&y==1)
	{
		flag=true;
		print(x,y);
		return;
	}
	vis[x][y]=true;
	monkey_sort();
	for(int i=1;i<=4;i++)
	{
		if(x+d[i].x==fx&&y+d[i].y==fy) continue;
		dfs(x+d[i].x,y+d[i].y,x,y);
	}
}
bool IsKeyPressed(WORD key)
{
    return (GetAsyncKeyState(key) & 0x8000) != 0;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	srand(time(0));
	cout<<"如你所料,欢迎来到lcy塔防!请认准正版哦!"<<endl;
	Sleep(3000);
	system("cls");
	d[1]={1,0};
	d[2]={-1,0};
	d[3]={0,-1};
	d[4]={0,1};
	for(int i=1;i<=25;i++)
	{
		for(int j=1;j<=100;j++) a[i][j]=' ';
	}
	while(flag==false)
    {
        for(int i=1;i<=25;i++)
        {
            for(int j=1;j<=100;j++) vis[i][j]=false,pre[i][j].x=pre[i][j].y=0;
        }
        dfs(25,100,0,0);
    }
	while(hp>0)
	{
	    for(int i=1;i<=25;i++)
	    {
	        for(int j=1;j<=100;j++) mo[i][j]=0;
	    }
		cd1--,cdm--,cd2--,cd3--;
		if(cd1==0) monster[++head]={25,100,1,0,0,0,5},cd1=10;
		if(cd2==0) monster[++head]={25,100,2,0,0,0,10},cd2=17;
		if(cd3==0) monster[++head]={25,100,3,0,0,0,20},cd3=23;
		if(cdm==0) money++,cdm=10;
		for(int i=tail;i<=head;i++)
		{
		    if(monster[i].hp<=0) continue;
			int x=monster[i].x,y=monster[i].y,fx=monster[i].fx,fy=monster[i].fy;
			for(int j=1;j<=4;j++)
			{
			    if(a[x+d[j].x][y+d[j].y]=='X') monster[i].hp--;
				if(x+d[j].x==fx&&y+d[j].y==fy)
				{
					continue;
				}
				if(a[x+d[j].x][y+d[j].y]=='#') monster[i].x+=d[j].x,monster[i].y+=d[j].y,monster[i].fx=x,monster[i].fy=y;
			}
			mo[monster[i].x][monster[i].y]=i;
			if(monster[i].x==1&&monster[i].y==1)
			{
			    hp--;
			    monster[i].hp=0;
			}
		}
		for(int i=1;i<=25;i++)
		{
			for(int j=1;j<=100;j++)
			{
				if(a[i][j]=='0')
				{
					if(cd[i][j]>0) cd[i][j]--;
					else
					{
						cd[i][j]=10;
						for(int k=1;k<=len;k++)
                        {
                            if(mo[road[k].x][road[k].y]>0)
                            {
                                gun[++cct]={i,j,1,mo[road[k].x][road[k].y],0,0,0,true};
                                break;
                            }
                        }
					}
				}
				if(a[i][j]=='Q')
				{
					if(cd[i][j]>0) cd[i][j]--;
					else
					{
						cd[i][j]=5;
						for(int k=1;k<=len;k++)
                        {
                            if(mo[road[k].x][road[k].y]>0)
                            {
                                gun[++cct]={i,j,1,mo[road[k].x][road[k].y],0,0,0,true};
                                break;
                            }
                        }
					}
				}
				if(a[i][j]=='G')
				{
					if(cd[i][j]>0) cd[i][j]--;
					else
					{
						cd[i][j]=2;
						for(int k=1;k<=len;k++)
                        {
                            if(mo[road[k].x][road[k].y]>0)
                            {
                                gun[++cct]={i,j,1,mo[road[k].x][road[k].y],0,0,0,true};
                                break;
                            }
                        }
					}
				}
			}
		}
		for(int i=tt;i<=cct;i++)
		{
		    for(int j=1;j<=3;j++)
    	    {
    		    if(gun[i].vis==false) continue;
    		    if(monster[gun[i].t].hp<=0)
    		    {
    		        bool succeed=false;
					for(int k=1;k<=len;k++)
                    {
                        if(mo[road[k].x][road[k].y]>0)
                        {
                            gun[i].t=mo[road[k].x][road[k].y];
                            succeed=true;
                            break;
                        }
                    }
                    if(succeed==false)
                    {
                        gun[i].vis=false;
                        while(gun[tt].vis==false) tt++;
                    }
    		    }
    		    if(gun[i].x==monster[gun[i].t].x&&gun[i].y==monster[gun[i].t].y)
    		    {
    		        monster[gun[i].t].hp--;
    		        if(monster[gun[i].t].hp<=0) money++;
    		        gun[i].vis=false;
    		        continue;
    		    }
    			if(gun[i].x>monster[gun[i].t].x)
    			{
    			    gun[i].x--;
    		    }
    	   	    else gun[i].x++;
    	   	    if(gun[i].y>monster[gun[i].t].y)
    	   	    {
    	   	        gun[i].y--;
    	   	    }
    	   	    else gun[i].y++;
		    }
		}
		if(IsKeyPressed('1'))
		{
			if(money>=5)
			{
				int x,y;
				cout<<"请输入坐标";
				Sleep(1000);
				cin>>x>>y;
				while(a[x][y]!=' ')
				{
					cout<<"别闹了";
					cin>>x>>y;
				}
				a[x][y]='0';
				money-=5;
			}
		}
		if(IsKeyPressed('2'))
		{
			if(money>=10)
			{
				int x,y;
				cout<<"请输入坐标";
				Sleep(1000);
				cin>>x>>y;
				while(a[x][y]!=' ')
				{
					cout<<"别闹了";
					cin>>x>>y;
				}
				a[x][y]='Q';
				money-=10;
			}
		}
		if(IsKeyPressed('3'))
		{
			if(money>=20)
			{
				int x,y;
				cout<<"请输入坐标";
				Sleep(1000);
				cin>>x>>y;
				while(a[x][y]!=' ')
				{
					cout<<"别闹了";
					cin>>x>>y;
				}
				a[x][y]='G';
				money-=20;
			}
		}
		if(IsKeyPressed('4'))
		{
			if(money>=10)
			{
				int x,y;
				cout<<"请输入坐标";
				Sleep(1000);
				cin>>x>>y;
				while(a[x][y]!=' ')
				{
					cout<<"别闹了";
					cin>>x>>y;
				}
				a[x][y]='X';
				money-=10;
			}
		}
	    cout<<"按下对应数字键选择你要放置的塔防种类,然后输入合法的地址,注意等待一秒再输入"<<endl;
		cout<<"能量:"<<money<<"    "<<"基地血量:"<<hp<<endl;
		cout<<setw(10)<<"枪塔"<<setw(10)<<"快枪塔"<<setw(10)<<"机枪塔"<<setw(10)<<"尖刺塔";
		cout<<endl;
		cout<<setw(10)<<"5$"<<setw(10)<<"10$"<<setw(10)<<"20$"<<setw(10)<<"10$";
		cout<<endl;
		for(int i=1;i<=25;i++)
		{
			for(int j=1;j<=100;j++)
			{
			    if(i==j&&j==1)
			    {
			        cout<<"K";
			        continue;
			    }
				bool v=false;
				for(int k=tail;k<=head;k++)
				{
					if(monster[k].x==i&&monster[k].y==j&&monster[k].hp>0)
					{
						v=true;
						if(monster[k].kind==1)
						{
							std::cout<<RED<<"n"<<RESET;
						}
						if(monster[k].kind==2)
						{
							std::cout<<RED<<"h"<<RESET;
						}
						if(monster[k].kind==3)
						{
							std::cout<<RED<<"m"<<RESET;
						}
						break;
					}
				}
				for(int k=1;k<=cct;k++)
				{
					if(gun[k].x==i&&gun[k].y==j&&gun[k].vis==true)
					{
						v=true;
						if(gun[k].kind==1)
						{
							std::cout<<BLUE<<"o"<<RESET;
						}
						break;
					}
				}
				if(v==false)
				{
					cout<<a[i][j];
				}
			}
			cout<<endl;
		}
		Sleep(100);
		system("cls");
	}
	cout<<"你失败了";
	return 0;
}
原文