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

📄 chess.cpp

📁 包括用VC开发的五子棋程序 以及VRML语言写的虚拟地理环境程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "Chess.h"
#include <iostream.h>
#include <afxtempl.h>

Chess::Chess()
{
	for( int i=0;i<15;i++ )
		for( int j=0;j<15;j++ )
			FiveArray[i][j]=0;
	StepList.RemoveAll();
}

Chess::~Chess()
{
	
}

int Chess::UpDown(int i, int j, int side)					//查询出从上到下相同的棋子数
{
	int tempi,count;
	count=1;

	tempi=i;
	while( --tempi>=0 && FiveArray[tempi][j]==side )
	{
		count++;
	}

	tempi=i;
	while( ++tempi<15 && FiveArray[tempi][j]==side )
	{
		count++;
	}

	return count;
}

int Chess::LeftRight(int i, int j, int side)					//查询出从左到右相同的棋子数
{
	int tempj,count;
	count=1;

	tempj=j;
	while( --tempj>=0 && FiveArray[i][tempj]==side )
	{
		count++;
	}

	tempj=j;
	while( ++tempj<15 && FiveArray[i][tempj]==side )
	{
		count++;
	}
	
	return count;
}

int Chess::LUptoDown(int i, int j, int side)					//查询出从左上到右下相同的棋子数
{
	int tempi,tempj,count;
	count=1;

	tempi=i;
	tempj=j;
	while( --tempi>=0 && --tempj>=0 && FiveArray[tempi][tempj]==side )
	{
		count++;
	}

	tempi=i;
	tempj=j;
	while( ++tempi<15 && ++tempj<15 && FiveArray[tempi][tempj]==side )
	{
		count++;
	}

	return count;
}

int Chess::RUptoDown(int i, int j, int side)					//查询出从右上到左下相同的棋子数
{
	int tempi,tempj,count;
	count=1;

	tempi=i;
	tempj=j;
	while( --tempi>=0 && ++tempj<15 && FiveArray[tempi][tempj]==side )
	{
		count++;
	}

	tempi=i;
	tempj=j;
	while( ++tempi<15 && --tempj>=0 && FiveArray[tempi][tempj]==side )
	{
		count++;
	}

	return count;
}

int Chess::LeftToRight_Status(int i, int j, int array[][15])		//查询当前所走步的棋盘状态
{
	int tempj,count;
	bool aliveone=false;
	bool alivetwo=false;
	bool beyond=false;
	int side=array[i][j];
	count=1;
	tempj=j;
	while( --tempj>=0 && array[i][tempj]==side )
		count++;
	if( tempj>=0 && array[i][tempj]==0 )
		aliveone=true;
	else if( tempj>=0 && array[i][tempj]==1 )
		beyond=true;

	tempj=j;
	while( ++tempj<15 && array[i][tempj]==side )
		count++;
	if( tempj<15 && array[i][tempj]==0 )
		alivetwo=true;
	else if( tempj<15 && array[i][tempj]==1 )
		beyond=true;

	if( count>=5 )
		return 8;
	if( count==1 && aliveone==true && alivetwo==true )
		return 0;
	if( count==1 && beyond==true )
		return 1;
	if( count>1 && aliveone==true && alivetwo==true )
		return count+3;
	if( count>1 && (aliveone==true || alivetwo==true) )
		return count;

	return 0;
}

int Chess::UpToDown_Status(int i, int j, int array[][15])
{
	int tempi,count;
	bool aliveone=false;
	bool alivetwo=false;
	bool beyond=false;
	int side=array[i][j];
	count=1;

	tempi=i;
	while( --tempi>=0 && array[tempi][j]==side )
		count++;
	if( tempi>=0 && array[tempi][j]==0 )
		aliveone=true;
	else if( tempi>=0 && array[tempi][j]==1 )
		beyond=true;

	tempi=i;
	while( ++tempi<15 && array[tempi][j]==side )
		count++;
	if( tempi<15 && array[tempi][j]==0 )
		alivetwo=true;
	else if( tempi<15 && array[tempi][j]==1 )
		beyond=true;

	if( count>=5 )
		return 8;
	if( count==1 && aliveone==true && alivetwo==true )
		return 0;
	if( count==1 && beyond==true )
		return 1;
	if( count>1 && aliveone==true &&alivetwo==true )
		return count+3;
	if( count>1 && (aliveone==true || alivetwo==true) )
		return count;

	return 0;
}

int Chess::LeftUpToRightDown_Status(int i, int j, int array[][15])
{
	int tempi,tempj,count;
	bool aliveone=false;
	bool alivetwo=false;
	bool beyond=false;
	int side=array[i][j];
	count=1;
	
	tempi=i;
	tempj=j;
	while( --tempi>=0 && --tempj>=0 && array[tempi][tempj]==side )
		count++;
	if( tempi>=0 && tempj>=0 && array[tempi][tempj]==0 )
		aliveone=true;
	else if( tempi>=0 && tempj>=0 && array[tempi][tempj]==1 )
		beyond=true;

	tempi=i;
	tempj=j;
	while( ++tempi<15 && ++tempj<15 && array[tempi][tempj]==side)
		count++;
	if( tempi<15 && tempj<15 && array[tempi][tempj]==0 )
		alivetwo=true;
	else if( tempi<15 && tempj<15 && array[tempi][tempj]==1 )
		beyond=true;

	if( count>=5 )
		return 8;
	if( count==1 && aliveone==true && alivetwo==true )
		return 0;
	if(  count==1 && beyond==true)
		return 1;
	if( count>1 && aliveone==true && alivetwo==true )
		return count+3;
	if( count>1 && (aliveone==true || alivetwo==true) )
		return count;

	return 0;
}

int Chess::LeftDownToRightUp_Status(int i, int j, int array[][15])
{
	int tempi,tempj,count;
	bool aliveone=false;
	bool alivetwo=false;
	bool beyond=false;
	int side=array[i][j];
	count=1;

	tempi=i;
	tempj=j;
	while( ++tempi<15 && --tempj>=0 && array[tempi][tempj]==side )
		count++;
	if( tempi<15 && tempj>=0 && array[tempi][tempj]==0 )
		aliveone=true;
	else if( tempi<15 && tempj>=0 && array[tempi][tempj]==1 )
		beyond=true;

	tempi=i;
	tempj=j;
	while( --tempi>=0 && ++tempj<15 && array[tempi][tempj]==side )
		count++;
	if( tempi>0 && tempj<14 && array[tempi][tempj]==0 )
		alivetwo=true;
	else if( tempi>=0 && tempj<15 && array[tempi][tempj]==1 )
		beyond=true;

	if( count>=5 )
		return 8;
	if( count==1 &&aliveone==true && alivetwo==true )
		return 0;
	if( count==1 && beyond ==true )
		return 1;
	if( count>1 && aliveone==true && alivetwo==true)
		return count+3;
	if( count>1 && (aliveone==true || alivetwo==true) )
		return count;

	return 0;
}

int Chess::SearchValue(int array[][15], Step &st, bool machine)
{
	int max_score = 0;
	int score = 0;

	GameStatus temp;
	temp.is_machine = machine;
	temp.deep = 0;
	for (int i=0 ; i<15 ;i++)
		for (int jj=0 ; jj<15;jj++)
			temp.fivearray[i][jj] = array[i][jj];
	

	for ( i =0; i <15;i++)
		for(int j=0;j<15;j++)
		{
			if ( array[i][j] == 0)
			{
				temp.st.x = i;
				temp.st.y = j;
				score = 0;
				
				if ( machine)
				{
					temp.fivearray[i][j] = 3;
					temp.st.side = 3;
					temp.is_machine = true; 
					temp.score = 0;
					GetCurrentScore(temp);
					score=temp.score;
				}
				else
				{
					temp.fivearray[i][j] =1;
					temp.st.side =1;
					temp.is_machine =false ;
					temp.score =0;
					GetCurrentScore(temp);
					score=score-temp.score ;
				}
				if ( score==100000 )
				{
					st.x = i;
					st.y = j;
					return  score;
				}
				if ( score>max_score )
				{
					max_score=score;
					st.x=i;
					st.y=j;
				}
				temp.fivearray[i][j] =0;
			}
		}
		return max_score ;
}

	//状态
	//返回8 成5:five
	//返回7 活4:alivefour
	//返回6 活3:alivethree
	//返回5 活2:alivetwo
	//返回4 死4:deadfour
	//返回3 死3:deadthree
	//返回2 死2:deadtwo
	//返回0
void Chess::GetCurrentScore(GameStatus &board_situation)
{
	int i=board_situation.st.x;
	int j=board_situation.st.y;
	board_situation.score=0;

	//搜索出左到右的状态
	int lr=LeftToRight_Status(i,j,board_situation.fivearray);
	if( lr==8 )
	{
		if( board_situation.is_machine )
			board_situation.score=100000;
		else
			board_situation.score=-100000;
		return;
	}

	//搜索出上到下的状态
	int ud=UpToDown_Status(i,j,board_situation.fivearray);
	if( ud==8 )
	{
		if(board_situation.is_machine)
			board_situation.score=100000;
		else
			board_situation.score=-100000;
	return;
	}
	

	//搜索出左上到右下的状态
	int lutrd=LeftUpToRightDown_Status(i,j,board_situation.fivearray);
	if( lutrd==8 )
	{
		if( board_situation.is_machine )
			board_situation.score=100000;
		else
			board_situation.score=-100000;
	return;
	}
	

	//搜索从左下到右上
	int ldtru=LeftDownToRightUp_Status(i,j,board_situation.fivearray);
	if( ldtru==8 )
	{
		if( board_situation.is_machine )
			board_situation.score=100000;
		else
			board_situation.score=-100000;
		return;
	}

	//是否活4
	if( lr==7 || ud==7 || lutrd==7 || ldtru==7 )
	{
		if( board_situation.is_machine )
			board_situation.score=10000;
		else
			board_situation.score=-10000;
		return;
	}
	
	//双死4
	if( 
		( lr==4 && (ud==4 || lutrd==4 || ldtru==4) )
		|| ( ud==4 && ( lutrd==4 || ldtru==4 ) )
		|| ( lutrd==4 && ldtru==4 )
	  )
	{
		if( board_situation.is_machine )
			board_situation.score=10000;
		else
			board_situation.score=-10000;
		return;
	}

	//死4活3
	if(  
		( lr==4 && ( ud==6 || lutrd==6 || ldtru==6 ) )
		||( ud==4 && (lr==6 || lutrd==6 || ldtru==6) )
		||( lutrd==4 && (lr==6 || ud==6 || ldtru==6) )
		||( ldtru==4 && (lr==6 || ud==6 || lutrd==6) )
	  )
	{
		if( board_situation.is_machine )
			board_situation.score=10000;
		else
			board_situation.score=-10000;
		return;
	}

	//双活3
	if(
		( lr==6 && (ud==6 || lutrd==6 || ldtru==6) )
		|| ( ud==6 && (lutrd==6 || ldtru==6) )
		|| ( lutrd==6 && ldtru==6 )
		)
	{
		if( board_situation.is_machine )
			board_situation.score=5000;
		else
			board_situation.score=-5000;
		return;
	}

	//活3双活2
	if( 
		( lr==6 || ud==6 || lutrd==6 || ldtru==6 ) &&
			( (lr==5 && (ud==5 || lutrd==5 || ldtru==5)) 
			|| (ud==5 && (lutrd==5 || ldtru==5))
			|| (lutrd==5 && ldtru==5) )
		)
	{
		if( board_situation.is_machine )
			board_situation.score=5000;
		else
			board_situation.score=-5000;
		return;
	}

	//活3死3
	if(
		( lr==3 && (ud==6 || lutrd==6 || ldtru==6) )
		|| ( ud==3 && (lr==6 || lutrd==6 || ldtru==6) )
		|| ( lutrd==3 && (lr==6 || ud==6 || ldtru==6) )
		|| ( ldtru==3 && (lr==6 || ud==6 || lutrd==6) )
		)
	{
		if( board_situation.is_machine )
			board_situation.score=1000;
		else
			board_situation.score=-1000;
		return;
	}

	//死4
	if( lr==4 || ud==4 || lutrd==4 || ldtru==4 )
	{
		if( board_situation.is_machine )
			board_situation.score=500;
		else
			board_situation.score=-500;
		return;
	}

	//单活3
	if( lr==6 || ud==6 || lutrd==6 || ldtru==6 )
	{
		if( board_situation.is_machine )
			board_situation.score=200;
		else
			board_situation.score=-200;
		return;
	}

	//双活2
	if(
		( lr==5 && (ud==5 || lutrd==5 || ldtru==5) )
		|| (ud==5 && (lutrd==5 || ldtru==5))
		|| (lutrd==5 && ldtru==5)
		)
	{

⌨️ 快捷键说明

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