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

📄 hi.cpp

📁 搜索算法部分使用minmax递归
💻 CPP
字号:
// HI.cpp: implementation of the CHI class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Othello.h"
#include "HI.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHI::CHI()
{

}

CHI::~CHI()
{

}




//  Test whether we can drop chess at x[m][n],y[m][n],and return turnchessnumber
int CHI::TestDropCurrent(int m,int n,int positive)   //positive 正的,minus 负的
{

		SetZero();

		int minus;      //相反
		int a;
		int b;
		if(positive==1)
			minus=2;
		else
			minus=1;
	 	if(attribute[m][n]==0)
		{
			//     search left
			a=m;
			b=n;
			while(attribute[a][b-1]==minus&&b>1)
				b=b-1;
			if(b>1&&attribute[a][b-1]==positive&&b!=n)
			{
					whetherdrop[m][n]=positive;
					attribute[m][n]=positive;
					for(int i=b;i<n;i++)
					{
						attribute[a][i]=positive;
					}
			}		
			//     search right
			a=m;
			b=n;
			while(attribute[a][b+1]==minus&&b<8)
				b=b+1;
			if(b<8&&attribute[a][b+1]==positive&&b!=n)
			{	
				whetherdrop[m][n]=positive;
				attribute[m][n]=positive;
				for(int i=n+1;i<=b;i++)
				{
					attribute[a][i]=positive;
				}
			}
						
			//       search up
			a=m;
			b=n;
			while(attribute[a-1][b]==minus&&a>1)
				a=a-1;
			if(a>1&&attribute[a-1][b]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;
				attribute[m][n]=positive;
				for(int i=a;i<m;i++)
				{
					attribute[i][b]=positive;
				}
			}
				
			//       search down
			a=m;
			b=n;
			while(attribute[a+1][b]==minus&&a<8)
				a=a+1;
			if(a<8&&attribute[a+1][b]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;
				attribute[m][n]=positive;
				for(int i=m+1;i<=a;i++)
				{
					attribute[i][b]=positive;
				}
			}
					
     		//  search left up
			a=m;
			b=n;
			while(attribute[a-1][b-1]==minus&&b>1&&a>1)
			{
				a=a-1;
				b=b-1;
			}
			if(a>1&&b>1&&attribute[a-1][b-1]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;	
				attribute[m][n]=positive;
				int i,j;
				for(i=a,j=b;i<m,j<n;i++,j++)
				{
					attribute[i][j]=positive;
				}
			}
						
			//   search left down
			a=m;
			b=n;
			while(attribute[a+1][b-1]==minus&&b>1&&a<8)
			{
				a=a+1;
				b=b-1;
			}
			if(b>1&&a<8&&attribute[a+1][b-1]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;	
				attribute[m][n]=positive;
				int i,j;
				for(i=m+1,j=n-1;i<=a,j>=b;i++,j--)
				{
					attribute[i][j]=positive;
				}
			}
						
			//   search right up
			a=m;
			b=n;
			while(attribute[a-1][b+1]==minus&&a>1&&b<8)
			{
				a=a-1;
				b=b+1;
			}
			if(a>1&&b<8&&attribute[a-1][b+1]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;
				attribute[m][n]=positive;
				int i,j;
				for(i=m-1,j=n+1;i>=a,j<=b;i--,j++)
				{
					attribute[i][j]=positive;
				}
			}
					
     		//   search right down
			a=m;
			b=n;
			while(attribute[a+1][b+1]==minus&&a<8&&b<8)
			{
				a=a+1;
				b=b+1;
			}
			if(a<8&&b<8&&attribute[a+1][b+1]==positive&&a!=m)
			{
				whetherdrop[m][n]=positive;
				attribute[m][n]=positive;
				int i,j;
				for(i=m+1,j=n+1;i<=a,j<=b;i++,j++)
				{
					attribute[i][j]=positive;
				}
			}
		}
			return 0;         //可以下子,返回0
}
//  Finished Test
						





//*******************************************************************************
//search at one time all the place the AI can drop
int CHI::TestDropAll(int positive)
{

	SetZero();
	int minus;      //相反
	if(positive==1)
		minus=2;
	else
		minus=1;
	int a;
	int b;
	int m;
	int n;
	for(m=1;m<9;m++)
		for(n=1;n<9;n++)
		{
				if(attribute[m][n]==0)
			{
				//     search left
				a=m;
				b=n;
				while(attribute[a][b-1]==minus&&b>1)
					b=b-1;
				if(b>1&&attribute[a][b-1]==positive&&b!=n)
					whetherdrop[m][n]=positive;
								
				//     search right
				a=m;
				b=n;
				while(attribute[a][b+1]==minus&&b<8)
					b=b+1;
				if(b<8&&attribute[a][b+1]==positive&&b!=n)	
					whetherdrop[m][n]=positive;
				
						
				//       search up
				a=m;
				b=n;
				while(attribute[a-1][b]==minus&&a>1)
					a=a-1;
				if(a>1&&attribute[a-1][b]==positive&&a!=m)
					whetherdrop[m][n]=positive;
				
				
				//       search down
				a=m;
				b=n;
				while(attribute[a+1][b]==minus&&a<8)
					a=a+1;
				if(a<8&&attribute[a+1][b]==positive&&a!=m)
					whetherdrop[m][n]=positive;	
				
						
     			//  search left up
				a=m;
				b=n;
				while(attribute[a-1][b-1]==minus&&b>1&&a>1)
				{
					a=a-1;
					b=b-1;
				}
				if(a>1&&b>1&&attribute[a-1][b-1]==positive&&a!=m)
					whetherdrop[m][n]=positive;	
				
						
				//   search left down
				a=m;
				b=n;
				while(attribute[a+1][b-1]==minus&&b>1&&a<8)
				{
					a=a+1;
					b=b-1;
				}
				if(b>1&&a<8&&attribute[a+1][b-1]==positive&&a!=m)
					whetherdrop[m][n]=positive;	
						
				//   search right up
				a=m;
				b=n;
				while(attribute[a-1][b+1]==minus&&a>1&&b<8)
				{
					a=a-1;
					b=b+1;
				}
				if(a>1&&b<8&&attribute[a-1][b+1]==positive&&a!=m)
					whetherdrop[m][n]=positive;	
					
     			//   search right down
				a=m;
				b=n;
				while(attribute[a+1][b+1]==minus&&a<8&&b<8)
				{
					a=a+1;
					b=b+1;
				}
				if(a<8&&b<8&&attribute[a+1][b+1]==positive&&a!=m)
					whetherdrop[m][n]=positive;
			
			}
		}
		//统计可下子的个数,如果为0,则返回0,pass
		int s=0;
		for(int i=1;i<9;i++)
			for(int j=1;j<9;j++)
			{
				if(whetherdrop[i][j]==positive)
				s++;
			}
		return s;

}



void CHI::SetZero()
{
	for(int i=1;i<9;i++)
		for(int j=1;j<9;j++)
			whetherdrop[i][j]=0;
}

⌨️ 快捷键说明

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