基于MinMaxSearch的翻转棋AI C++

Java Project 翻转棋

# include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int max_depth = 8;
int my_color,opp_color;
int mp[8][8];
struct MOVE {int x,y;};
MOVE FINAL;
const int w[8][8]={{500,-25,10,5,5,10,-25,500},{-25,-45,1,1,1,1,-45,-25},{10,1,3,2,2,3,1,10},{5,1,2,1,1,2,1,5},{5,1,2,1,1,2,1,5},{10,1,3,2,2,3,1,10},{-25,-45,1,1,1,1,-45,-25},{500,-25,10,5,5,10,-25,500}};
int mapWeightSum(int color) {
	int res = 0;
	for (int i=0;i<8;i++)
		for (int j=0;j<8;j++) 
			if (mp[i][j]==color) {
				res+=w[i][j];
			} else if (mp[i][j]==-color) {
				res-=w[i][j];
			}
	return res;		
}
vector<MOVE>possiMove;
int mapMoves(int color) {
	possiMove.clear();
	for (int i=0;i<8;i++)
		for (int j=0;j<8;j++) if (mp[i][j]==0) {
			int p; bool flag=false;
			p=j+1; while (p<8&&mp[i][p]==-color) p++;
			if (p<8&&mp[i][p]==color&&p!=j+1) flag=true;
			p=j-1; while (p>=0&&mp[i][p]==-color) p--;
			if (p>=0&&mp[i][p]==color&&p!=j-1) flag=true;
			p=i+1; while (p<8&&mp[p][j]==-color) p++;
			if (p<8&&mp[p][j]==color&&p!=i+1) flag=true;
			p=i-1; while (p>=0&&mp[p][j]==-color) p--;
			if (p>=0&&mp[p][j]==color&&p!=i-1) flag=true;
			p=1; while (i+p<8&&j+p<8&&mp[i+p][j+p]==-color) p++;
			if (i+p<8&&j+p<8&&mp[i+p][j+p]==color&&p!=1) flag=true;
			p=1; while (i-p>=0&&j-p>=0&&mp[i-p][j-p]==-color) p++;
			if (i-p>=0&&j-p>=0&&mp[i-p][j-p]==color&&p!=1) flag=true;
			p=1; while (i+p<8&&j-p>=0&&mp[i+p][j-p]==-color) p++;
			if (i+p<8&&j-p>=0&&mp[i+p][j-p]==color&&p!=1) flag=true;
			p=1; while (i-p>=0&&j+p<8&&mp[i-p][j+p]==-color) p++;
			if (i-p>=0&&j+p<8&&mp[i-p][j+p]==color&&p!=1) flag=true;
			if (flag) possiMove.push_back((MOVE){i,j});
		}
	return possiMove.size();	
}
void draw(int i,int j,int color) {
	int p;
	p=j+1; while (p<8&&mp[i][p]==-color) p++;
	if (p<8&&mp[i][p]==color&&p!=j+1) {
		p=j+1; while (p<8&&mp[i][p]==-color) mp[i][p++]=color;
	}
	p=j-1; while (p>=0&&mp[i][p]==-color) p--;
	if (p>=0&&mp[i][p]==color&&p!=j-1) {
		p=j-1; while (p>=0&&mp[i][p]==-color) mp[i][p--]=color;
	}
	p=i+1; while (p<8&&mp[p][j]==-color) p++;
	if (p<8&&mp[p][j]==color&&p!=i+1) {
		p=i+1; while (p<8&&mp[p][j]==-color) mp[p++][j]=color;
	}
	p=i-1; while (p>=0&&mp[p][j]==-color) p--;
	if (p>=0&&mp[p][j]==color&&p!=i-1) {
		p=i-1; while (p>=0&&mp[p][j]==-color) mp[p--][j]=color;
	}
	p=1; while (i+p<8&&j+p<8&&mp[i+p][j+p]==-color) p++;
	if (i+p<8&&j+p<8&&mp[i+p][j+p]==color&&p!=1) {
		p=1; while (i+p<8&&j+p<8&&mp[i+p][j+p]==-color) {
			mp[i+p][j+p]=color; p++;
		}
	}
	p=1; while (i-p>=0&&j-p>=0&&mp[i-p][j-p]==-color) p++;
	if (i-p>=0&&j-p>=0&&mp[i-p][j-p]==color&&p!=1) {
		p=1; while (i-p>=0&&j-p>=0&&mp[i-p][j-p]==-color) {
			mp[i-p][j-p]=color; p++;
		}
	}
	p=1; while (i+p<8&&j-p>=0&&mp[i+p][j-p]==-color) p++;
	if (i+p<8&&j-p>=0&&mp[i+p][j-p]==color&&p!=1) {
		p=1; while (i+p<8&&j-p>=0&&mp[i+p][j-p]==-color) {
			mp[i+p][j-p]=color; p++;
		}
	}
	p=1; while (i-p>=0&&j+p<8&&mp[i-p][j+p]==-color) p++;
	if (i-p>=0&&j+p<8&&mp[i-p][j+p]==color&&p!=1) {
		p=1; while (i-p>=0&&j+p<8&&mp[i-p][j+p]==-color) {
			mp[i-p][j+p]=color; p++;
		}
	}
	mp[i][j]=color;
}
int mapStables (int color) {
	int cind1[4]={0,0,7,7};
	int cind2[4]={0,7,7,0};
	int inc1[4]={0,1,0,-1};
	int inc2[4]={1,0,-1,0};
	int stop[4]={0,0,0,0};
	int res = 0;
	bool stable[8][8]; memset(stable,false,sizeof(stable));
	for (int i=0;i<4;i++) if (mp[cind1[i]][cind2[i]]==color) {
		stable[cind1[i]][cind2[i]]=true; stop[i]=1; res++;
		for (int j=1;j<7;j++) 
			 if (mp[cind1[i]+inc1[i]*j][cind2[i]+inc2[i]*j] != color) {
			 	break;
			 } else {
			 	stable[cind1[i]+inc1[i]*j][cind2[i]+inc2[i]*j]=true; stop[i]=j+1;
			 	res++;
			 }
	}
	for (int i=0;i<4;i++) if (mp[cind1[i]][cind2[i]]==color) {
		for (int j=1;j<7-stop[i-1];j++) {
			if (mp[cind1[i]-inc1[i-1]*j][cind2[i]-inc2[i-1]*j] != color) {
				break;
			} else {
				stable[cind1[i]-inc1[i-1]*j][cind2[i]-inc2[i-1]*j]=true;
				res++;
			}
		}
	}
	for (int i=0;i<8;i++)
		for (int j=0;j<8;j++)
			if (stable[i][j]==false&&mp[i][j]==color) {
				bool flag=true;
				for (int k=0;k<8;k++) if (mp[i][k]==0||mp[k][j]==0) {
					flag=false; break;
				}
				if (!flag) continue;
				int p;
				p=1; while (i+p<8&&j+p<8&&mp[i+p][j+p]!=0) p++;
				if (!(i+p>=8||j+p>=8)) continue;
				p=1; while (i-p>=0&&j-p>=0&&mp[i-p][j-p]!=0) p++;
				if (!(i-p<0||j-p<0)) continue;
				p=1; while (i+p<8&&j-p>=0&&mp[i+p][j-p]!=0) p++;
				if (!(i+p>=8||j-p<0)) continue;
				p=1; while (i-p>=0&&j+p<8&&mp[i-p][j+p]!=0) p++;
				if (!(i-p<0||j+p>=8)) continue;
				res++;
			}
	return res;		
}
int getValue() {
	return mapWeightSum(my_color)+15*(mapMoves(my_color)-mapMoves(opp_color))+10*mapStables(my_color);
}
int sign(int x) {
	return (x == my_color)?1:-1;
}
int Search(int a,int b,int color,int depth) {
	int Max = -inf;
	if (depth<=0) return sign(color)*getValue();
	if (!mapMoves(color)) {
		if (!mapMoves(-color)) 
			return sign(color)*getValue();
		return -Search(-b,-a,-color,depth);
	}
	vector<MOVE>allMoves = possiMove;
	for (MOVE move : allMoves) {
		int rec[8][8]; 
		memcpy(rec,mp,sizeof(mp));
		draw(move.x,move.y,color);
		int val = -Search(-b,-a,-color,depth-1);
		memcpy(mp,rec,sizeof(rec));
		if (val > a) {
			if (val >= b) return val;
			a = max(val,a);
		}
		if (val > Max) {
			Max = val;
			if (depth == max_depth) {
				FINAL = move;
			}
		}
	}
	return Max;
}
MOVE MinMaxSearch() {
	FINAL = (MOVE){-1,-1};
	Search(-inf,inf,my_color,max_depth);
	return FINAL;
}
int main() {
	cin>>my_color;  opp_color=-my_color;
	for (int i=0;i<8;i++)
		for (int j=0;j<8;j++) 
			cin>>mp[i][j];
	while (true) {
		MOVE bestMove = MinMaxSearch();
		printf("Best Move : %d %c\n",bestMove.x+1,bestMove.y+'A');
		draw(bestMove.x,bestMove.y,my_color);
		int cnt0=0,cnt1=0;
		for (int i=0;i<8;i++) {
			for (int j=0;j<8;j++) {
						if (mp[i][j]==-1) cnt0++;
						else if (mp[i][j]==1) cnt1++;
						printf("%d ",mp[i][j]);
					}
			puts("");		
		}
		printf("%d : %d\n",cnt0,cnt1);
		if (mapMoves(my_color)==0 && mapMoves(opp_color)==0) {
			puts("game over!"); break;
		}
		int row,col; char tmp;
		cin>>row;
		if (row == -1) continue;
		cin>>tmp;
		row--; col=tmp-'A';
		draw(row,col,opp_color);
		cnt0=0,cnt1=0;
		for (int i=0;i<8;i++) {
			for (int j=0;j<8;j++) {
						if (mp[i][j]==-1) cnt0++;
						else if (mp[i][j]==1) cnt1++;
						printf("%d ",mp[i][j]);
					}
			puts("");
		}
		printf("%d : %d\n",cnt0,cnt1);
		if (mapMoves(my_color)==0 && mapMoves(opp_color)==0) {
			puts("game over!"); break;
		}
	}
	return 0;
}
posted @ 2021-11-18 14:48  Maystern  阅读(12)  评论(0编辑  收藏  举报