⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strategy.cpp

📁 足球机器人比赛程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		double enter_y;
		if(env->currentBall.pos.x<env->lastBall.pos.x)
		{
			double k=(env->currentBall.pos.y-env->lastBall.pos.y)/(env->currentBall.pos.x-env->lastBall.pos.x);
			enter_y=k*(estimate_x-env->currentBall.pos.x)+env->currentBall.pos.y;
		}
		if(enter_y>33.9320-5 && enter_y<49.6801+5)
		{
			estimate_y=enter_y;
		}
		if(estimate_y>49.6801+5)
			estimate_y=49.6801+5;
		else if(estimate_y<33.9320-5)
			estimate_y=33.9320-5;
		if(GoaliePosition(&env->home[flag[4]], estimate_x, estimate_y))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}

	if(count>=3)
	{
		for(i=1;i<=4;i++)
		{
			if(env->home[i].pos.x>=env->currentBall.pos.x)
			{
				if(env->currentBall.pos.y>=GTOPY+8)
					Position1(&env->home[i],FLEFTX,GTOPY+8);
				if(env->currentBall.pos.y<=GTOPY-8)
					Position1(&env->home[i],FLEFTX,GBOTY-8);
				if(env->currentBall.pos.y<GTOPY+8&&env->currentBall.pos.y>GTOPY-8)
				{
					if(env->home[i].pos.y>=env->currentBall.pos.y)
						Position1(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y+20);
					else
						Position1(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y-20);
				}
			}
			else
				Defence1_Left(&env->home[i],env);
		}
	}
}

void Defend1_Right(Environment *env)//防守策略(暂考虑把转换成防守的条件放在函数外面)//一般防守阵形
{
	int count=0;
	double leng[5];
	leng[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{
		if(env->home[i].pos.x<=env->currentBall.pos.x)
			count++;
	}
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}

	if(count==0 || count==1 ||count==2)
	{ 
		Defence1_Right(&env->home[flag[1]],env);
		
		if(env->home[flag[2]].pos.x<=env->currentBall.pos.x)
		{
			if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
				GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y+20);
			else
				GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y-20);
		}
		else
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+10,env->currentBall.pos.y))
			{
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}

		if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
		{
			if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x+10,env->currentBall.pos.y+10))
			{
				AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}
		else
		{
			if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x+10,env->currentBall.pos.y-10))
			{
				AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}

		double estimate_x=FRIGHTX-8;
		double estimate_y=env->currentBall.pos.y;
		double enter_y;
		if(env->currentBall.pos.x>env->lastBall.pos.x)
		{
			double k=(env->currentBall.pos.y-env->lastBall.pos.y)/(env->currentBall.pos.x-env->lastBall.pos.x);
			enter_y=k*(estimate_x-env->currentBall.pos.x)+env->currentBall.pos.y;
		}
		if(enter_y>33.9320-5 && enter_y<49.6801+5)
		{
			estimate_y=enter_y;
		}
		if(estimate_y>49.6801+5)
			estimate_y=49.6801+5;
		else if(estimate_y<33.9320-5)
			estimate_y=33.9320-5;
		if(GoaliePosition(&env->home[flag[4]], estimate_x, estimate_y))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}
	
	if(count>=3)
	{
		for(i=1;i<=4;i++)
		{
			if(env->home[i].pos.x<=env->currentBall.pos.x)
			{
				if(env->currentBall.pos.y>=GTOPY+8)
					Position1_cz(&env->home[i],FRIGHTX,GTOPY+8);
				if(env->currentBall.pos.y<=GTOPY-8)
					Position1_cz(&env->home[i],FRIGHTX,GBOTY-8);
				if(env->currentBall.pos.y<GTOPY+8&&env->currentBall.pos.y>GTOPY-8)
				{
					if(env->home[i].pos.y>=env->currentBall.pos.y)
						Position1_cz(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y+20);
					else
						Position1_cz(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y-20);
				}
			}
			else
				Defence1_Right(&env->home[i],env);
		}
	}
/*	AvoidGoalie(&env->home[flag[1]]);
	AvoidGoalie(&env->home[flag[2]]);
	AvoidGoalie(&env->home[flag[3]]);
	AvoidGoalie(&env->home[flag[4]]);*/
}

void Defend2_Left(Environment *env)
{
	double leng[5];
	leng[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}

	Defence1_Left(&env->home[flag[1]],env);
	if(env->currentBall.pos.y<=FBOT+6 || env->currentBall.pos.y>=FTOP-6)
	{
		if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x-2,env->currentBall.pos.y+6))
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
		}
		else
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x-2,env->currentBall.pos.y-6))
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
		}
	}
	else
		Defence1_Right(&env->home[flag[2]],env);
}

void Defend2_Right(Environment *env)//底线防守阵形
{
	double leng[5];
	leng[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}
	
	Defence1_Right(&env->home[flag[1]],env);
	if(env->currentBall.pos.y<=FBOT+6 || env->currentBall.pos.y>=FTOP-6)
	{
		if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+2,env->currentBall.pos.y+6))
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
		}
		else
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+2,env->currentBall.pos.y-6))
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
		}
	}
	else
		Defence1_Right(&env->home[flag[2]],env);

	if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
	{
		if(GoaliePosition(&env->home[flag[3]],FLEFTX+10,GBOTY))
			AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
		if(GoaliePosition(&env->home[flag[4]],FLEFTX+15,GTOPY+6))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}
	else
	{
		if(GoaliePosition(&env->home[flag[3]],FLEFTX+10,GTOPY))
			AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
		if(GoaliePosition(&env->home[flag[4]],FLEFTX+15,GBOTY-6))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}
	
	if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
	{
		if(GoaliePosition(&env->home[flag[3]],FRIGHTX-10,GBOTY))
			AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
		if(GoaliePosition(&env->home[flag[4]],FRIGHTX-15,GTOPY+6))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}
	else
	{
		if(GoaliePosition(&env->home[flag[3]],FRIGHTX-10,GTOPY))
			AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
		if(GoaliePosition(&env->home[flag[4]],FRIGHTX-15,GBOTY-6))
			AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
	}
/*	AvoidGoalie(&env->home[flag[1]]);
	AvoidGoalie(&env->home[flag[2]]);
	AvoidGoalie(&env->home[flag[3]]);
	AvoidGoalie(&env->home[flag[4]]);*/
}

void Defend3_Left(Environment *env)
{
	double Y[5];
	Y[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{  
        Y[i]=env->home[i].pos.y;
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(Y[i]>=Y[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}

	if(GoaliePosition(&env->home[flag[1]],FLEFTX+10,GBOTY-10))
		AngleOfPosition(&env->home[flag[1]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[2]],FLEFTX+9,GBOTY+2))
		AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[3]],FLEFTX+9,GTOPY-2))
		AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[4]],FLEFTX+10,GTOPY+10))
		AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
}

void Defend3_Right(Environment *env)//球在禁区时的防守策略
{
	double Y[5];
	Y[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{  
        Y[i]=env->home[i].pos.y;
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(Y[i]>=Y[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}

	if(GoaliePosition(&env->home[flag[1]],FRIGHTX-10,GBOTY-10))
		AngleOfPosition(&env->home[flag[1]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[2]],FRIGHTX-9,GBOTY+2))
		AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[3]],FRIGHTX-9,GTOPY-2))
		AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
	if(GoaliePosition(&env->home[flag[4]],FRIGHTX-10,GTOPY+10))
		AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y);
/*	AvoidGoalie(&env->home[flag[1]]);
	AvoidGoalie(&env->home[flag[2]]);
	AvoidGoalie(&env->home[flag[3]]);
	AvoidGoalie(&env->home[flag[4]]);*/
}

void Attack_Left(Environment *env)
{
	Robot *robot1, *robot2, *robot3, *robot4;
	int i,flag1,flag2,flag3,flag4;
    double leng[5],Y[5],MIN;
	leng[0]=0;
	Y[0]=0;
    for(i=1;i<5;i++)
	{
		leng[i]=leng(14,(FBOT+FTOP)/2,env->home[i].pos.x,env->home[i].pos.y);
        Y[i]=env->home[i].pos.y;
	}

    MIN=leng[1];
	flag4=1;
	for(i=2;i<5;i++)
	{
		if(leng[i]<MIN)
		{
			MIN=leng[i];
			flag4=i;
		}
	}
	robot4=&env->home[flag4];
    
    if(flag4==1) {flag1=2;flag2=3;flag3=4;}
	if(flag4==2) {flag1=1;flag2=3;flag3=4;}
	if(flag4==3) {flag1=1;flag2=2;flag3=4;}
	if(flag4==4) {flag1=1;flag2=2;flag3=3;}
    robot1=&env->home[flag1];
	robot2=&env->home[flag2];
	robot3=&env->home[flag3];
	
	if(env->currentBall.pos.x<=(FRIGHTX+FLEFTX)/2 && env->currentBall.pos.x>=14
		&& robot1->pos.x>env->currentBall.pos.x
		&& robot2->pos.x>env->currentBall.pos.x 
		&& robot3->pos.x>env->currentBall.pos.x)
	{
		Position1(robot4,env->currentBall.pos.x,env->currentBall.pos.y);
	}
	else
	{
		double estimate_x=15;
		double estimate_y=env->currentBall.pos.y;
		double enter_y;
		if(env->currentBall.pos.x>env->lastBall.pos.x)
		{
			double k=(env->currentBall.pos.y-env->lastBall.pos.y)/(env->currentBall.pos.x-env->lastBall.pos.x);
			enter_y=k*(estimate_x-env->currentBall.pos.x)+env->currentBall.pos.y;
		}
		if(enter_y>33.9320-10 && enter_y<49.6801+10)
		{
			estimate_y=enter_y;
		}
		if(estimate_y>49.6801+10)
			estimate_y=49.6801+10;
		else if(estimate_y<33.9320-10)
			estimate_y=33.9320-10;
		
		if(GoaliePosition(robot4, estimate_x, estimate_y))
		{
			AngleOfPosition(robot4,env->currentBall.pos.x,env->currentBall.pos.y);
		}
	}
	/*
	if(Y[flag1]<=Y[flag2] && Y[flag1]<=Y[flag3])
	{
		robot1=&env->home[flag1];
		if(Y[flag2]<=Y[flag3])
		{
			robot2=&env->home[flag2];
			robot3=&env->home[flag3];
		}
		else
		{
			robot2=&env->home[flag3];
			robot3=&env->home[flag2];
		}
	}
	if(Y[flag2]<Y[flag1] && Y[flag2]<=Y[flag3])
	{
		robot1=&env->home[flag2];
		if(Y[flag1]<=Y[flag3])
		{
			robot2=&env->home[flag1];
			robot3=&env->home[flag3];
		}
		else
		{
			robot2=&env->home[flag3];
			robot3=&env->home[flag1];
		}
	}
	if(Y[flag3]<Y[flag1] && Y[flag3]<Y[flag2])
	{
		robot1=&env->home[3];
		if(Y[flag1]<=Y[flag2])
		{
			robot2=&env->home[flag1];
			robot3=&env->home[flag2];
		}
		else
		{
			robot2=&env->home[flag2];
			robot3=&env->home[flag1];
		}
	}
    */
	double leng1=leng(env->currentBall.pos.x,env->currentBall.pos.y,robot1->pos.x,robot1->pos.y);
	double leng2=leng(env->currentBall.pos.x,env->currentBall.pos.y,robot2->pos.x,robot2->pos.y);
	double leng3=leng(env->currentBall.pos.x,env->currentBall.pos.y,robot3->pos.x,robot3->pos.y);
    
	if(env->currentBall.pos.x>FLEFTX+5 && env->currentBall.pos.x<FRIGHTX-5)
	{
		if(env->currentBall.pos.y>FBOT+5 && env->currentBall.pos.y<FTOP-5)
		{
			if(leng1<=leng2 && leng1<=leng3)
			{
				Shoot1_Left(robot1,env);
				GoaliePosition(robot2,env->currentBall.pos.x-5,env->currentBall.pos.y-10);
				GoaliePosition(robot3,env->currentBall.pos.x,env->currentBall.pos.y-20);
			}
			if(leng2<leng1 && leng2<=leng3)
			{
				Shoot1_Left(robot2,env);
				GoaliePosition(robot1,env->currentBall.pos.x+5,env->currentBall.pos.y-10);
				GoaliePosition(robot3,env->currentBall.pos.x-3,env->currentBall.pos.y+10);
			}
			if(leng3<leng1 && leng3<leng2)
			{
				Shoot1_Left(robot3,env);
				GoaliePosition(robot1,env->currentBall.pos.x-3,env->currentBall.pos.y+20);
				GoaliePosition(robot2,env->currentBall.pos.x+5,env->currentBall.pos.y+10);
			}
		}
		
		else if(env->currentBall.pos.y>=FTOP-5)
		{
			if(leng1<=leng2 && leng1<=leng3)
			{
				Shoot1_Left(robot1,env);
				GoaliePosition(robot2,env->currentBall.pos.x-5,env->currentBall.pos.y-10);
				GoaliePosition(robot3,env->currentBall.pos.x+2,env->currentBall.pos.y-20);
			}
			if(leng2<leng1 && leng2<=leng3)
			{
				Shoot1_Left(robot2,env);
				GoaliePosition(robot1,env->currentBall.pos.x+2,env->currentBall.pos.y-10);
				GoaliePosition(robot3,env->currentBall.pos.x+5,env->currentBall.pos.y-20);
			}
			if(leng3<leng1 && leng3<leng2)
			{
				Shoot1_Left(robot3,env);
				GoaliePosition(robot1,env->currentBall.pos.x-4,env->currentBall.pos.y-10);
				GoaliePosition(robot2,env->currentBall.pos.x+3,env->currentBall.pos.y-20);
			}
		}
		
		else
		{
			if(leng1<=leng2 && leng1<=leng3)
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -