怎么才能实现如下功能?
/*抢旗子 规则如下 (现在规定只能在红色显示的代码区做修改 达到提高白队的胜率的功能 (即蓝方的动作未知,白方的代码尽量做到防守攻击兼备的效果
请问 应该怎么设计? 求示范小样 )
100回合之内先抢到对方旗子的胜利。
地图大小20×20
2队的旗子和人初始位置已知固定
蓝队 2人,白队 2人。
只能上下左右直线移动,不能斜着移动。
1回合内1个人只能移动一格
同一坐标点点上不能存在2个人或者旗子。
地图左上角开始计算为(x,y)=(0,0)。
(0,0) (1,0) (2,0)... (19,0)
(0,1) (1,1) (2,1)... (19,1)
...
(0,19)(1,19)(2,19)...(19,19)
*/
#include <stdio.h>
#define WAIT for(_temp=0;_temp<100000000;_temp++)
#define CLS for(_temp=0;_temp<2;_temp++){printf("\n");}
#define TRUE 1
#define FALSE 0
#define MAX_X 20
#define MAX_Y 20
#define MAX_TURN 100
#define NONE 0
#define blue0 10
#define blue1 11
#define white0 20
#define white1 21
#define BF 30
#define WF 40
#define BF_X 3
#define BF_Y 3
#define WF_X MAX_X-4
#define WF_Y MAX_Y-4
typedef struct position{
int x;
int y;
}POS;
//大域変数
int _temp=0;
int turn=0;
int berr_flag=FALSE;
int werr_flag=FALSE;
int bend_flag=FALSE;
int wend_flag=FALSE;
int map[MAX_X][MAX_Y];
POS blue[3];
POS white[3];
POS old_blue[3];
POS old_white[3];
POS bf={BF_X,BF_Y}; //蓝旗位置
POS wf={WF_X,WF_Y}; //白旗位置
void init(void)
{
int i,j;
//初期位置
blue[1].x=BF_X+2; blue[1].y=BF_Y+2;
blue[0].x=BF_X+2; blue[0].y=BF_Y-2;
white[0].x=WF_X-2; white[0].y=WF_Y+2;
white[1].x=WF_X-2; white[1].y=WF_Y-2;
//地图初期化
for(j=0;j<MAX_Y;j++){
for(i=0;i<MAX_X;i++){
map[i][j]=NONE;
}
}
}
void move_b0(void)
{
if(blue[0].x<WF_X){blue[0].x++;}
else if(blue[0].x>WF_X){blue[0].x--;}
else if(blue[0].y<WF_Y){blue[0].y++;}
else if(blue[0].y>WF_Y){blue[0].y--;}
static int sflag=0,flag=0;
if(sflag>1){
if(flag%8==0){blue[0].y++; flag++;}
else if(flag%8==1){blue[0].y++; flag++;}
else if(flag%8==2){blue[0].x--; flag++;}
else if(flag%8==3){blue[0].x--; flag++;}
else if(flag%8==4){blue[0].y--; flag++;}
else if(flag%8==5){blue[0].y--; flag++;}
else if(flag%8==6){blue[0].x++; flag++;}
else if(flag%8==7){blue[0].x++; flag++;}
}
if(sflag==0){blue[0].x--; sflag++;}
else if(sflag==1){blue[0].y++; sflag++;}
}
void move_b1(void)
{
if(blue[1].x<WF_X){blue[1].x++;}
else if(blue[1].x>WF_X){blue[1].x--;}
else if(blue[1].y<WF_Y){blue[1].y++;}
else if(blue[1].y>WF_Y){blue[1].y--;}
}
void move_blue(void)
{
int i;
int oldx[2],oldy[2];
for(i=0;i<2;i++){
oldx[i]=blue[i].x;
oldy[i]=blue[i].y;
}
move_b0();
move_b1();
for(i=0;i<2;i++){
if(blue[i].x==white[i].x&&blue[i].y==white[i].y){
blue[i].x=oldx[i];
blue[i].y=oldy[i];
}
}
if(blue[0].x==blue[1].x&&blue[0].y==blue[1].y){
blue[0].x=oldx[0];
blue[0].y=oldy[0];
blue[1].x=oldx[1];
blue[1].y=oldy[1];
}
//限制在地图范围内移动
for(i=0;i<2;i++){
if(blue[i].x>MAX_X){blue[i].x=oldx[i];}
if(blue[i].x<0){blue[i].x=0;}
if(blue[i].y>MAX_Y){blue[i].y=oldy[i];}
if(blue[i].y<0){blue[i].y=0;}
}
}
void move_w0(void)
{
static int sflag=0;
if(sflag==0){white[0].x++; sflag++;}
else if(sflag<2){white[0].y--; sflag++;}
}
void move_w1(void)
{
static int sflag=0;
if(sflag<2){white[1].x++; sflag++;}
else if(sflag<3){white[1].y++; sflag++;}
}
void move_white()
{
int i;
int oldx[2],oldy[2];
for(i=0;i<2;i++){
oldx[i]=white[i].x;
oldy[i]=white[i].y;
}
move_w0();
move_w1();
for(i=0;i<2;i++){
if(white[i].x==blue[i].x&&white[i].y==blue[i].y){
white[i].x=oldx[i];
white[i].y=oldy[i];
}
}
//限制在地图范围内移动
for(i=0;i<2;i++){
if(white[i].x>MAX_X){white[i].x=oldx[i];}
if(white[i].x<0){white[i].x=0;}
if(white[i].y>MAX_Y){white[i].y=oldy[i];}
if(white[i].y<0){white[i].y=0;}
}
}
void make_map(void)
{
int i,j;
for(j=0;j<MAX_Y;j++){
for(i=0;i<MAX_X;i++){
map[i][j]=0;
}
}
map[blue[0].x][blue[0].y]=blue0;
map[blue[1].x][blue[1].y]=blue1;
map[white[0].x][white[0].y]=white0;
map[white[1].x][white[1].y]=white1;
map[bf.x][bf.y]=BF;
map[wf.x][wf.y]=WF;
}
void message(void)
{
int i,j;
for(i=0;i<MAX_X;i++){printf("-");} printf("\n");
for(j=0;j<MAX_Y;j++){
for(i=0;i<MAX_X;i++){
if(map[i][j]==0){printf(" ");}
if(map[i][j]==blue0){printf("B0");}
if(map[i][j]==blue1){printf("B1");}
if(map[i][j]==white0){printf("W0");}
if(map[i][j]==white1){printf("W1");}
if(map[i][j]==BF){printf("BF");}
if(map[i][j]==WF){printf("WF");}
}
printf("\n");
}
for(i=0;i<MAX_X;i++){printf("-");} printf("\n");
printf("现在的回合数:%d\n",turn);
}
void check_before(void)
{
int i;
for(i=0;i<2;i++){
old_blue[i].x=blue[i].x;
old_blue[i].y=blue[i].y;
old_white[i].x=white[i].x;
old_white[i].y=white[i].y;
}
}
void check_after(void)
{
int i,j;
//抢到旗子胜利
for(i=0;i<2;i++){
if(blue[i].x==WF_X&&blue[i].y==WF_Y){bend_flag=TRUE;}
if(white[i].x==BF_X&&white[i].y==BF_Y){wend_flag=TRUE;}
}
// 用自己人占据自己旗子的坐标(犯规)
for(i=0;i<2;i++){
if(blue[i].x==BF_X&&blue[i].y==BF_Y){berr_flag=TRUE;}
if(white[i].x==WF_X&&white[i].y==WF_Y){werr_flag=TRUE;}
}
//斜着移动(犯规)
for(i=0;i<2;i++){
if(blue[i].x-old_blue[i].x==1&&blue[i].y-old_blue[i].y==1){berr_flag=TRUE;}
else if(blue[i].x-old_blue[i].x==-1&&blue[i].y-old_blue[i].y==1){berr_flag=TRUE;}
else if(blue[i].x-old_blue[i].x==-1&&blue[i].y-old_blue[i].y==-1){berr_flag=TRUE;}
else if(blue[i].x-old_blue[i].x==1&&blue[i].y-old_blue[i].y==-1){berr_flag=TRUE;}
if(white[i].x-old_white[i].x==1&&white[i].y-old_white[i].y==1){werr_flag=TRUE;}
else if(white[i].x-old_white[i].x==-1&&white[i].y-old_white[i].y==1){werr_flag=TRUE;}
else if(white[i].x-old_white[i].x==-1&&white[i].y-old_white[i].y==-1){werr_flag=TRUE;}
else if(white[i].x-old_white[i].x==1&&white[i].y-old_white[i].y==-1){werr_flag=TRUE;}
}
//与对方敌人不能重叠在同一坐标上。
for(i=0;i<2;i++){
for(j=0;j<2;j++){
if(blue[i].x==white[j].x&&blue[i].y==white[j].y){
printf("与敌人坐标重叠, wx>%d, wy>%d, ",white[j].x,white[j].y);
printf("bx>%d, by>%d\n",blue[i].x,blue[i].y);
blue[i].x=old_blue[i].x;
blue[i].y=old_blue[i].y;
white[j].x=old_white[j].x;
white[j].y=old_white[j].y;
}
}
}
//蓝方,不能与自己人坐标重叠
if(blue[0].x==blue[1].x&&blue[0].y==blue[1].y){
printf("blue与自己人坐标重叠, bx>%d, by>%d, ",blue[0].x,blue[0].y);
blue[0].x=old_blue[0].x;
blue[0].y=old_blue[0].y;
blue[1].x=old_blue[1].x;
blue[1].y=old_blue[1].y;
}
//白方,不能与自己人坐标重叠。
if(white[0].x==white[1].x&&white[0].y==white[1].y){
printf("white与自己人坐标重叠, wx>%d, wy>%d, ",white[0].x,white[0].y);
white[0].x=old_white[0].x;
white[0].y=old_white[0].y;
white[1].x=old_white[1].x;
white[1].y=old_white[1].y;
}
//一次移动1格以上判为犯规
for(i=0;i<2;i++){
if(abs(old_blue[i].x-blue[i].x)>=2){berr_flag=TRUE;}
if(abs(old_blue[i].y-blue[i].y)>=2){berr_flag=TRUE;}
if(abs(old_white[i].x-white[i].x)>=2){werr_flag=TRUE;}
if(abs(old_white[i].y-white[i].y)>=2){werr_flag=TRUE;}
}
//限制在地图范围内
for(i=0;i<2;i++){
if(blue[i].x>MAX_X){blue[i].x=old_blue[i].x;}
if(blue[i].x<0){blue[i].x=0;}
if(blue[i].y>MAX_Y){blue[i].y=old_blue[i].y;}
if(blue[i].y<0){blue[i].y=0;}
if(white[i].x>MAX_X){white[i].x=old_white[i].x;}
if(white[i].x<0){white[i].x=0;}
if(white[i].y>MAX_Y){white[i].y=old_white[i].y;}
if(white[i].y<0){white[i].y=0;}
}
}
int main(void)
{
init();
for(turn=0;turn<MAX_TURN&&bend_flag==FALSE&&wend_flag==FALSE&&berr_flag==FALSE&&werr_flag==FALSE;turn++){
message();
check_before();
if(turn%2==0){
move_blue();
move_white();
}
else{
move_white();
move_blue();
}
/*
blue[1].x=10;
blue[1].y=10;
white[0].x=10;
white[0].y=10;
*/
check_after();
make_map();
CLS;
WAIT;
}
if(bend_flag==TRUE&&wend_flag==TRUE){printf("平:BLUE0点, WHITE0点\n");}
else if(bend_flag==TRUE&&wend_flag==FALSE){printf("蓝方胜利:BLUE%d点, WHITE%d点\n",MAX_TURN+1-turn,-(MAX_TURN+1-turn));}
else if(bend_flag==FALSE&&wend_flag==TRUE){printf("白方胜利:BLUE%d点, WHITE%d点\n",-(MAX_TURN+1-turn),MAX_TURN+1-turn);}
else if(berr_flag==TRUE&&werr_flag==TRUE){printf("双方都犯规:BLUE-10点, WHITE-10点\n");}
else if(berr_flag==TRUE){printf("蓝方犯规:BLUE%d点, WHITE%d点\n",-(MAX_TURN+1-turn),MAX_TURN+1-turn);}
else if(werr_flag==TRUE){printf("白方犯规:BLUE%d点, WHITE%d点\n",MAX_TURN+1-turn,-(MAX_TURN+1-turn));}
else if(bend_flag==FALSE&&wend_flag==FALSE){printf("回合用完未分胜负:BLUE0点, WHITE0点\n");}
return(0);
}
/* end of program */