#include<bits/stdc++.h>
#include<windows.h>
#include <cmath>
#include <utility>
#include<conio.h>
#define M_PI 3.14159265358979323846
using namespace std;
const int dx=30,ai=1;
void circleCollision(double& x1, double& y1, double& vx1, double& vy1, double r1,
double& x2, double& y2, double& vx2, double& vy2, double r2) {
// 计算圆心距离
double dx = x2 - x1;
double dy = y2 - y1;
double dist = sqrt(dx*dx + dy*dy);
double radiusSum = r1 + r2;
// 检查碰撞
if(dist > radiusSum) return;
// 计算单位法向量
double nx = dx/dist;
double ny = dy/dist;
// 计算相对速度
double v_rel = (vx2-vx1)*nx + (vy2-vy1)*ny;
// 完全弹性碰撞响应
if(v_rel < 0) {
// 交换法向速度分量
double impulse = v_rel;
vx1 += impulse*nx;
vy1 += impulse*ny;
vx2 -= impulse*nx;
vy2 -= impulse*ny;
// 防止重叠
double overlap = radiusSum - dist;
x1 -= overlap*nx/2;
y1 -= overlap*ny/2;
x2 += overlap*nx/2;
y2 += overlap*ny/2;
}
}
void cls() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD topLeft = {0, 0};
SetConsoleCursorPosition(hConsole, topLeft);
}
#include <cmath>
#include <algorithm>
constexpr double PI = 3.14159265358979323846;
#include <cmath>
#include <algorithm>
int getAggressiveAIMovement(
double self_x, double self_y, double self_vx, double self_vy,
double target_x, double target_y, double target_vx, double target_vy)
{
if(rand()%10==0)return rand()%6;
// 战场参数配置
const double BATTLEFIELD_SIZE = 30.0;
const double BORDER_MARGIN = 13.0;
const double PREDICT_TIME = 2.0;
// 边界安全预测(最高优先级)
double future_x = self_x + self_vx * PREDICT_TIME;
double future_y = self_y + self_vy * PREDICT_TIME;
// 四向边界防御
if(future_x < BORDER_MARGIN) {
if(self_vx < 0) return 3;
if(fabs(self_vy) > 1.0) return (self_vy > 0) ? 2 : 1;
return 3;
}
if(future_x > BATTLEFIELD_SIZE - BORDER_MARGIN) {
if(self_vx > 0) return 4;
if(fabs(self_vy) > 1.0) return (self_vy > 0) ? 2 : 1;
return 4;
}
if(future_y < BORDER_MARGIN) {
if(self_vy < 0) return 2;
if(fabs(self_vx) > 1.0) return (self_vx > 0) ? 3 : 4;
return 2;
}
if(future_y > BATTLEFIELD_SIZE - BORDER_MARGIN) {
if(self_vy > 0) return 1;
if(fabs(self_vx) > 1.0) return (self_vx > 0) ? 3 : 4;
return 1;
}
// 战术参数
const double CHASE_RADIUS = 10.0;
const double RETREAT_RADIUS = 5.0;
const double CHARGE_THRESHOLD = 3.0;
const double INTERCEPT_WEIGHT = 1.8;
// 运动状态计算
double current_speed = hypot(self_vx, self_vy);
double dx = target_x - self_x;
double dy = target_y - self_y;
double distance = hypot(dx, dy);
// 三段式战术循环
if(distance < RETREAT_RADIUS && current_speed < CHARGE_THRESHOLD) {
double escape_angle = atan2(dy, dx) + M_PI;
if(fabs(escape_angle) <= M_PI/4) return 4;
if(fabs(escape_angle) >= 3*M_PI/4) return 3;
return (escape_angle > 0) ? 2 : 1;
}
if(current_speed >= CHARGE_THRESHOLD) {
if(fabs(self_vx) > fabs(self_vy)) {
return self_vx > 0 ? 3 : 4;
} else {
return self_vy > 0 ? 2 : 1;
}
}
// 智能拦截系统
double rel_vx = target_vx - self_vx;
double rel_vy = target_vy - self_vy;
double intercept_time = (dx*rel_vx + dy*rel_vy) /
(rel_vx*rel_vx + rel_vy*rel_vy + 0.01);
intercept_time = std::max(0.0, std::min(intercept_time * INTERCEPT_WEIGHT, 2.0));
double predict_x = dx + rel_vx * intercept_time;
double predict_y = dy + rel_vy * intercept_time;
// 动态攻击策略
if(distance < CHASE_RADIUS) {
double attack_angle = atan2(predict_y, predict_x);
double adaptive_threshold = M_PI/4 * (1 + current_speed/CHARGE_THRESHOLD);
if(fabs(attack_angle) <= adaptive_threshold) return 3;
if(fabs(attack_angle) >= M_PI - adaptive_threshold) return 4;
return attack_angle > 0 ? 1 : 2;
}
// 巡航加速模式
if(current_speed < CHARGE_THRESHOLD/2) {
double patrol_angle = atan2(self_y-15, self_x-15);
if(patrol_angle < -M_PI/2) return 3;
if(patrol_angle > M_PI/2) return 4;
return (self_y < 15) ? 2 : 1;
}
// 默认追击策略
return (fabs(predict_x) > fabs(predict_y)) ?
(predict_x > 0 ? 3 : 4) : (predict_y > 0 ? 2 : 1);
}
int getAggressiveAIMovement1(
double self_x, double self_y, double self_vx, double self_vy,
double target_x, double target_y, double target_vx, double target_vy)
{
if(rand()%10==0)return rand()%6;
// 战场参数配置
const double BATTLEFIELD_SIZE = 30.0;
const double BORDER_MARGIN = 3.0;
const double PREDICT_TIME = 6.0;
// 边界安全预测(最高优先级)
double future_x = self_x + self_vx * PREDICT_TIME;
double future_y = self_y + self_vy * PREDICT_TIME;
// 四向边界防御
if(future_x < BORDER_MARGIN) {
if(self_vx < 0) return 3;
if(fabs(self_vy) > 1.0) return (self_vy > 0) ? 2 : 1;
return 3;
}
if(future_x > BATTLEFIELD_SIZE - BORDER_MARGIN) {
if(self_vx > 0) return 4;
if(fabs(self_vy) > 1.0) return (self_vy > 0) ? 2 : 1;
return 4;
}
if(future_y < BORDER_MARGIN) {
if(self_vy < 0) return 2;
if(fabs(self_vx) > 1.0) return (self_vx > 0) ? 3 : 4;
return 2;
}
if(future_y > BATTLEFIELD_SIZE - BORDER_MARGIN) {
if(self_vy > 0) return 1;
if(fabs(self_vx) > 1.0) return (self_vx > 0) ? 3 : 4;
return 1;
}
// 战术参数
const double CHASE_RADIUS = 8.0;
const double RETREAT_RADIUS = 5.0;
const double CHARGE_THRESHOLD = 1.0;
const double INTERCEPT_WEIGHT = 1.0;
// 运动状态计算
double current_speed = hypot(self_vx, self_vy);
double dx = target_x - self_x;
double dy = target_y - self_y;
double distance = hypot(dx, dy);
// 三段式战术循环
if(distance < RETREAT_RADIUS && current_speed < CHARGE_THRESHOLD) {
double escape_angle = atan2(dy, dx) + M_PI;
if(fabs(escape_angle) <= M_PI/4) return 4;
if(fabs(escape_angle) >= 3*M_PI/4) return 3;
return (escape_angle > 0) ? 2 : 1;
}
if(current_speed >= CHARGE_THRESHOLD) {
if(fabs(self_vx) > fabs(self_vy)) {
return self_vx > 0 ? 3 : 4;
} else {
return self_vy > 0 ? 2 : 1;
}
}
// 智能拦截系统
double rel_vx = target_vx - self_vx;
double rel_vy = target_vy - self_vy;
double intercept_time = (dx*rel_vx + dy*rel_vy) /
(rel_vx*rel_vx + rel_vy*rel_vy + 0.01);
intercept_time = std::max(0.0, std::min(intercept_time * INTERCEPT_WEIGHT, 2.0));
double predict_x = dx + rel_vx * intercept_time;
double predict_y = dy + rel_vy * intercept_time;
// 动态攻击策略
if(distance < CHASE_RADIUS) {
double attack_angle = atan2(predict_y, predict_x);
double adaptive_threshold = M_PI/4 * (1 + current_speed/CHARGE_THRESHOLD);
if(fabs(attack_angle) <= adaptive_threshold) return 3;
if(fabs(attack_angle) >= M_PI - adaptive_threshold) return 4;
return attack_angle > 0 ? 1 : 2;
}
// 巡航加速模式
if(current_speed < CHARGE_THRESHOLD/2) {
double patrol_angle = atan2(self_y-15, self_x-15);
if(patrol_angle < -M_PI/2) return 3;
if(patrol_angle > M_PI/2) return 4;
return (self_y < 15) ? 2 : 1;
}
// 默认追击策略
return (fabs(predict_x) > fabs(predict_y)) ?
(predict_x > 0 ? 3 : 4) : (predict_y > 0 ? 2 : 1);
}
int main(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
srand(time(0));
double fx=rand()%dx+1,fy=rand()%dx+1,tx=rand()%dx+1,ty=rand()%dx+1,adfx=0,adfy=0,adtx=0,adty=0;
double mcx=0.98,mcy=0.98;
// cout<<"输入玩家1的摩擦系数"<<endl;
// cin>>mcx;
// cout<<"输入玩家2的摩擦系数"<<endl;
// cin>>mcy;
while(1){
if(ai){
int x=getAggressiveAIMovement1(fx,fy,adfx,adfy,tx,ty,adtx,adty);
// 修正方向控制逻辑
if(x==1)adfy-=0.1; // 下 (增加y坐标)
if(x==2)adfy+=0.1; // 上 (减少y坐标)
if(x==3)adfx+=0.1; // 右 (增加x坐标)
if(x==4)adfx-=0.1; // 左 (减少x坐标)
x=getAggressiveAIMovement1(tx,ty,adtx,adty,fx,fy,adfx,adfy);
if(x==1)adty-=0.1;
if(x==2)adty+=0.1;
if(x==3)adtx+=0.1;
if(x==4)adtx-=0.1;
}
if(_kbhit()){
char x=_getch();
if(x=='w')adfx-=0.1;
if(x=='a')adfy-=0.1;
if(x=='s')adfx+=0.1;
if(x=='d')adfy+=0.1;
if(x==-32){
x=_getch();
if(x==72)adtx-=0.1;
if(x==75)adty-=0.1;
if(x==80)adtx+=0.1;
if(x==77)adty+=0.1;
}
// if(x=='i')adtx-=0.1;
// if(x=='j')adty-=0.1;
// if(x=='k')adtx+=0.1;
// if(x=='l')adty+=0.1;
}
fx+=adfx,fy+=adfy,adfx*=mcx,adfy*=mcx;
tx+=adtx,ty+=adty,adtx*=mcy,adty*=mcy;
int jsfx=fx+0.5+1e-9,jsfy=fy+0.5+1e-9,jstx=tx+0.5+1e-9,jsty=ty+0.5+1e-9;
cls();
for(int i=1;i<=dx;i++){
for(int j=1;j<=dx;j++){
if(i==jsfx&&j==jsfy)cout<<"&";
else if(i==jstx&&j==jsty)cout<<"%";
else cout<<".";
}
cout<<endl;
}
if(jsfx<1||jsfy<1||jsfx>dx||jsfy>dx){
cls();
for(int i=1;i<=100;i++)cout<<"玩家二赢了";
_sleep(3000);
return 0;
}
if(jstx<1||jsty<1||jstx>dx||jsty>dx){
cls();
for(int i=1;i<=100;i++)cout<<"玩家一赢了";
_sleep(3000);
return 0;
}
// if(jsfx<1)fx+=dx;
// if(jsfy<1)fy+=dx;
// if(jsfx>dx)fx-=dx;
// if(jsfy>dx)fy-=dx;
// if(jstx<1)tx+=dx;
// if(jsty<1)ty+=dx;
// if(jstx>dx)tx-=dx;
// if(jsty>dx)ty-=dx;
circleCollision(fx,fy,adfx,adfy,1.5,tx,ty,adtx,adty,1.5);
}
return 0;
}剪贴板
剪贴板 6xmt5ew8
剪贴板信息
剪贴板信息
- 收录
- 更新