#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 20
#define EMPTY 0
#define ME 1
#define OTHER 2
#define TRUE 1
#define FALSE 0
#define MAX_BYTE 10000
#define START "START"
#define PLACE "PLACE"
#define DONE "DONE"
#define TURN "TURN"
#define BEGIN "BEGIN"
#define END "END"
struct Position
{
int x;
int y;
};
char buffer[MAX_BYTE] = {0};
char board[BOARD_SIZE][BOARD_SIZE] = {0};
void debug(const char *str)
{
printf("DEBUG %s\n", str);
fflush(stdout);
}
/*
* YOUR CODE BEGIN
* 你的代码开始
*/
/*
* You can define your own struct and variable here
* 你可以在这里定义你自己的结构体和变量
*/
/*
* You should init your AI here
* 在这里初始化你的AI
*/
void initAI()
{
}
/*
* Game Start, you will put the first chess.
* Warning: This method will only be called when after the initialize ofi the map, it is your turn to put the chess.
* Or this method will not be called.
* You should return a valid Position variable.
* 棋局开始,首先由你来落子
* 请注意:只有在当棋局初始化后,轮到你落子时才会触发这个函数。如果在棋局初始化完毕后,轮到对手落子,则这个函数不会被触发。详见项目要求。
* 在这里,me的取值只可能是ME(1),即board(棋盘)上为ME(1)的位置表示你的棋子,board(棋盘)上为OTHER(2)的位置表示对手的棋子。
* 你需要返回一个结构体Position,在x属性和y属性填上你想要落子的位置。
*/
struct Position aiBegin(const char board[BOARD_SIZE][BOARD_SIZE], int me)
{
/*
* TODO: Write your own ai here!
* Here is a simple AI which just put chess at empty position!
* 代做:在这里写下你的AI。
* 这里有一个示例AI,它只会寻找第一个可下的位置进行落子。
*/
int important(int x,int y);
int value(int n,int x,int y);
void move(int n,int *x,int *y);
int i, j, k, max=0;
struct Position pre;
for (i = 0; i < BOARD_SIZE; i++)
{
for (j = 0; j < BOARD_SIZE; j++)
{
if (EMPTY == board[i][j])
{
k=important(i,j);
if(k>=max)
{
pre.x = i;
pre.y = j;
max=k;
}
}
}
}
return pre;
}
/*
* Game ongoing, the competitor put the chess at the position (otherX, otherY). You should put your chess.
* You should return a valid Position variable.
* 棋局进行中,对方上一步落子在(otherX, otherY)的位置,轮到你落子了。
* 在这里,me的取值只可能是ME(1),即board(棋盘)上为ME(1)的位置表示你的棋子,board(棋盘)上为OTHER(2)的位置表示对手的棋子。
* 你需要返回一个结构体Position,在x属性和y属性填上你想要落子的位置。
*/
struct Position aiTurn(const char board[BOARD_SIZE][BOARD_SIZE], int me, int otherX, int otherY)
{
/*
* TODO: Write your own ai here!
* Here is a simple AI which just put chess at empty position!
* 代做:在这里写下你的AI。
* 这里有一个示例AI,它只会寻找第一个可下的位置进行落子。
*/
int important(int x,int y);
int value(int n,int x,int y);
void move(int n,int *x,int *y);
int i, j, k, max=0;
struct Position pre;
for (i = 0; i < BOARD_SIZE; i++)
{
for (j = 0; j < BOARD_SIZE; j++)
{
if (EMPTY == board[i][j])
{
k=important(i,j);
if(k>=max)
{
pre.x = i;
pre.y = j;
max=k;
}
}
}
}
return pre;
}
int important(int x,int y)
{
int value(int n,int x,int y);
void move(int n,int *x,int *y);
int n,k=0,k1,k2,M,m;
for(n=1;n<5;n++)
{
k1=value(n,x,y);
n=n+4;
k2=value(n,x,y); /*对称方向*/
n=n-4;
M=(k1>=k2)?k1:k2;
m=(k1<=k2)?k1:k2;
k1=m;
k2=M;
if((k1==8||k1==308)&&(k2==308||k2==8)) /*22022*/
k=k+8000;
else if((k1==400||k1==10)&&(k2==400||k2==10)) /*11011*/
k=k+10000;
else if((k1==30||k1==2)&&(k2==2500||k2==305))/*22202*/
k=k+8000;
else if ((k1==5||k1==40)&&(k2==3500||k2==600))/*11101*/
k=k+10000;
else if(k1==8&&k2==305) /*222022 且两边冲*/
k=k+8000;
else if(k1==10&&k2==600) /*111011 且两边冲*/
k=k+10000;
else if(k1==30&&k2==30) /*202 且两边活*/
k=k+308;
else if(k1==40&&k2==40) /*101 且两边活*/
k=k+400;
else if(k1==30&&k2==308) /*2022 且两边活*/
k=k+2500;
else if(k1==40&&k2==400) /*1011 且两边活*/
k=k+3500;
else if((k1==8&&k2==30)||(k1==2&&k2==308))/*2022 且只有一边活*/
k=k+305;
else if((k1==10&&k2==40)||(k1==5&&k2==400))/*1011 且只有一边活*/
k=k+600;
else if(k1==5&&k2==40) /*101 且只有一边活*/
k=k+10;
else if(k1==2&&k2==30) /*202 且只有一边活*/
k=k+8;
else if(k1==2&&k2==2) /*202 且两边冲*/
k=k+0;
else if(k1==5&&k2==5) /*101 且两边冲*/
k=k+0;
else if(k1==2&&k2==8) /*2022 且两边冲*/
k=k+0;
else if(k1==5&&k2==10) /*1011 且两边冲*/
k=k+0;
else if(k1<0&&k2<0)
k=k+0;
else if(k1==-1&&k2>0)
k=k+k2+5;
else if(k1==-5&&(k2==5||k2==10||k2==600))
k=k+0;
else if(k1==-5&&(k2==2||k2==8||k2==305))
k=k+0;
else if(k1==-5)
k=k+k2/2; /*边界问题*/
else if(k1==30&&k2==40)
k=k+(k1+k2)/4;
else if(k1==40&&(k2==308||k2==2500))
k=k+(k1+k2)/4;
else if(k1==5&&(k2==30||k2==308||k2==2500))
k=k+(k1+k2)/4;
else if(k1==40&&k2==305)
k=k+5;
else if(k2==40&&(k1==2||k1==8))
k=k+(k1+k2)/4;
else if(k1==5&&(k2==2||k2==8||k2==305))
k=k+0;
else if(k2==5&&(k1==2||k1==8||k1==305)) /*自已一 0 对方*/
k=k+0;
else if(k2==400&&(k1==30||k1==308))
k=k+(k1+k2)/4;
else if(k1==400&&k2==2500)
k=k+(k1+k2)/4;
else if(k2==400&&(k1==30||k1==308))
k=k+(k1+k2)/4;
else if(k2==400&&(k1==2||k1==8||k1==305))
k=k+(k1+k2)/4;
else if(k1==10&&(k2==30||k2==308||k2==2500))
k=k+(k1+k2)/4;
else if(k2==10&&(k1==2||k1==8||k1==305))
k=k+0;
else if(k1==10&&(k2==2||k2==8||k2==305)) /*自己二 0 对方*/
k=k+0;
else if(k2==3500&&(k1==30||k1==308||k1==2500))
k=k+(k1+k2)/4;
else if(k2==3500&&(k1==2||k1==8||k1==305))
k=k+(k1+k2)/4;
else if((k1==600&&k2==2500)||(k1==308&&k2==600))
k=k+(k1+k2)/4;
else if(k2==600&&k1==30)
k=k+0;
else if(k2==600&&(k1==2||k1==8||k1==305)) /*自己三 0 对方*/
k=k+0;
else if(k1==20&&k2==40) /*1001 且两边活*/
k=k+400;
else if(k1==26&&k2==30) /*2002 且两边活*/
k=k+308;
else if((k1==5&&k2==11)||(k1==5||k2==140)||(k1==11&&k2==40)||(k1==40&&k2==140))/*10011*/
k=k+600;
else if((k1==2&&k2==160)||(k1==2||k2==19)||(k1==19&&k2==30)||(k1==30&&k2==160))/*20022*/
k=k+305;
else {
k=k+k1+k2;
}
}
return k;
}
int value(int n,int x,int y)
{
int k,f=0;
void move(int n,int *x,int *y);
move(n,&x,&y);
if(x<0||x>19||y<0||y>19)
k=-5; /*遇到边界*/
else
{
switch(board[x][y])
{
case ME: {
f=f+1;
move(n,&x,&y);
if(x<0||x>19||y<0||y>19)
{
k=5;
return k;
} /*遇到边界*/
while(board[x][y]==ME)
{
f=f+1;
move(n,&x,&y);
if(x<0||x>19||y<0||y>19)
{
if(f==1)
k=5;
if(f==2)
k=10;