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

📄 chess.cpp

📁 包括用VC开发的五子棋程序 以及VRML语言写的虚拟地理环境程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if( board_situation.is_machine )
			board_situation.score=100;
		else
			board_situation.score=-100;
		return;
	}

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

	//单活2
	if( lr==5 || ud==5 || lutrd==5 || ldtru==5 )
	{
		if( board_situation.is_machine )
			board_situation.score=10;
		else
			board_situation.score=-10;
		return;
	}

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

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

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

	GameStatus temp;
	temp.is_machine=true;
	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;

				int score1 = 0 ;
				int score2 = 0 ;

				temp.fivearray[i][j]=3;
				temp.st.side=3;
				temp.is_machine=true;
				temp.score=0;
				GetCurrentScore(temp);
				score1=temp.score;

				temp.fivearray[i][j]=1;
				temp.st.side=1;
				temp.is_machine=false;
				temp.score=0;
				GetCurrentScore(temp);
				score2=-temp.score;

				//机器
				if( score1>=10000 )
				{
					temp.fivearray[i][j]=3;
					temp.st.side=3;
					temp.is_machine=true;
					temp.score=0;
					Step tempSt1;
					int tempscore=SearchValue(temp.fivearray,tempSt1,false);
					if( score1<tempscore )		//如果当前棋盘还有比现在分数大的,则防守
					{
						st.x=tempSt1.x;
						st.y=tempSt1.y;
						return tempscore;
					}
					else
					{
						st.x=i;
						st.y=j;
						return score1;
					}
				}

				if( score2>=100000 )
				{
					temp.fivearray[i][j]=1;
					temp.st.side=1;
					temp.is_machine=false;
					temp.score=0;
					Step tempSt1;
					int tempscore=SearchValue(temp.fivearray,tempSt1,true);
					if( score2<=tempscore )
					{
						st.x=tempSt1.x;
						st.y=tempSt1.y;
					}
					else
					{
						st.x=i;
						st.y=j;
						return score1;
					}
				}

				score=score1+score2;
				if( score>max_score )
				{
					max_score=score;
					st.x=i;
					st.y=j;
				}

				temp.fivearray[i][j]=0;
			}
		}
		return max_score;
}


int Chess::DeepSearch(GameStatus ts, Step &st, bool machine, int value)
{
	ts.deep++;
	st.deep++;
	int score=0;
	if( ts.deep==5 )
	{
		score=SearchValue( ts.fivearray,st,machine );
		return score;
	}
	int min_score=0;
	score=0;

	//选出几局分较高的
	CList<GameStatus,GameStatus&>GameStatusList;
	for( int i=0;i<15;i++ )
		for( int j=0;j<15;j++ )
		{
			if( ts.fivearray[i][j]==0 )
			{
				ts.st.x=i;
				ts.st.y=j;
				score=0;
				if( machine )
				{
					ts.fivearray[i][j]=3;
					ts.st.side=3;
					ts.is_machine=true;
					ts.score=0;
					GetCurrentScore(ts);
					if( score==100000 )
					{
						st.x=i;
						st.y=j;
						return score;
					}
					score=ts.score;
				}
				else
				{
					ts.fivearray[i][j]=1;
					ts.st.side=1;
					ts.is_machine=false;
					ts.score=0;
					GetCurrentScore(ts);
					score=-ts.score;
					ts.score=score;
					if( score==100000 )
					{
						st.x=i;
						st.y=j;
						return score;
					}
				}
				ts.score=score;
				if( score==0 )
				{
					ts.fivearray[i][j]=0;
					continue;
				}
				if( GameStatusList.GetCount()==0 )
				{
					min_score=ts.score;
					GameStatusList.AddTail(ts);
				}
				else
				{
					if( score<=min_score )
					{
						min_score=score;
						GameStatusList.AddTail(ts);
					}
					else
					{
						POSITION ps=GameStatusList.GetTailPosition();
						while( ps!=NULL )
						{
							GameStatus temp=GameStatusList.GetPrev(ps);
							if( temp.score>=score )
							{
								GameStatusList.InsertAfter(ps,ts);
								break;
							}
						}
						if( ps==NULL )
						{
							GameStatusList.AddHead(ts);
						}
					}
				}
				ts.fivearray[i][j]=0;
			}
		}

		POSITION ps=GameStatusList.GetHeadPosition();
		Step tempstep=st;
		int tempscore=0;
		int max=0;
		for( i=0;i<5;i++ )
		{
			GameStatus temp=GameStatusList.GetNext(ps);
			value=temp.score;
			tempscore=DeepSearch(temp,tempstep,!machine,temp.score);

			if( tempscore>max )
			{
				max=tempscore;
				st=temp.st;
				st.deep=tempstep.deep;
			}
				
			if( ps==NULL )
				break;
		}

		return max;
		
}

void Chess::Input(int i,int j)
{
	Step tempstep;
	tempstep.deep=0;
	tempstep.side=1;
	tempstep.x=i;
	tempstep.y=j;
	FiveArray[i][j]=1;
	StepList.AddTail(tempstep);
}

void Chess::Output()
{
	system("cls");
	for( int i=0;i<15;i++ )
		if( i<10 )
			cout<<"  "<<i+1<<" ";
		else
			cout<<" "<<i+1<<" ";
	cout<<endl<<endl;
	for( i=0;i<15;i++ )
	{
		for( int j=0;j<15;j++ )
		{
			if( FiveArray[i][j]==0 )
				cout<<" ╋ ";
			else if( FiveArray[i][j]==1 )
					cout<<" ○ ";
				else
					cout<<" ● ";
		}
		cout<<" "<<i+1<<" ";
		cout<<endl;			//用来换行
		cout<<endl;			//用来输出行间距
	}
	cout<<endl<<endl;

	Step tempstep;
	if( !StepList.IsEmpty() )
	{
		tempstep=StepList.GetTail();
		if( tempstep.side==1 )
			cout<<"people :(";
		else
			cout<<"machine:(";
		cout<<tempstep.x+1<<","<<tempstep.y+1<<")"<<endl;
	}
}

void Chess::Regret()
{
	if( StepList.GetCount()>0 )
	{
		Step temp=StepList.GetTail();
		StepList.RemoveTail();
		FiveArray[temp.x][temp.y]=0;
	}
	if( StepList.GetCount()>0 )
	{
		Step temp=StepList.GetTail();
		StepList.RemoveTail();
		FiveArray[temp.x][temp.y]=0;
	}
}

bool Chess::Judge()
{
	if( StepList.GetCount==0 )
		return false;

	Step temphead=StepList.GetHead();
	int sidehead=temphead.side;

	Step temp=StepList.GetTail();
	int i=temp.x;
	int j=temp.y;
	int side=temp.side;

	if ( sidehead==side )
	{
		int lr=LeftToRight_Status(i,j,FiveArray);
		int ud=UpToDown_Status(i,j,FiveArray);
		int lutrd=LeftUpToRightDown_Status(i,j,FiveArray);
		int ldtru=LeftDownToRightUp_Status(i,j,FiveArray);

		if ( lr==6&&ud==6 || lr==6&&lutrd==6 || lr==6&&ldtru==6 || 
			ud==6&&lutrd==6 || ud==6&&ldtru==6 ||
			lutrd==6&&ldtru==6 ||
			lr==6&&ud==7 || lr==6&&lutrd==7 || lr==6&&ldtru==7 || 
			ud==6&&lutrd==7 || ud==6&&ldtru==7 ||
			lutrd==6&&ldtru==7 )
		{
			cout<<"The first one han done BAN HAND!"<<endl;
			return true;
		}
	}

	if( LeftRight(i,j,side)>=5 || UpDown(i,j,side)>=5 || LUptoDown(i,j,side)>=5 || RUptoDown(i,j,side)>=5 )
		return true;
	else
		return false;
}

void Chess::ComputerGo()
{
		Step st;
		int score=SearchMaxValue(st,FiveArray);
		if( score>=1000 || score<=20 )
		{
			st.side=3;
			FiveArray[st.x][st.y]=3;
			StepList.AddTail(st);
			system("cls");
			Output();
		}
		else
		{
			GameStatus temp;
			for( int i=0;i<15;i++ )
				for( int jj=0;jj<15;jj++ )
					temp.fivearray[i][jj]=FiveArray[i][jj];
				temp.deep=0;
				temp.is_machine=true;
				temp.score=0;

				Step st1;
				Step st2;
				int score1=DeepSearch(temp,st1,true,0);
				int score2=DeepSearch(temp,st2,false,0);
				if( score1>score2 )
				{
					st1.side=3;
					FiveArray[st1.x][st1.y]=3;
					StepList.AddTail(st1);
					system("cls");
					Output();
				}
				if( score1<score2 )
				{
					st2.side=3;
					FiveArray[st2.x][st2.y]=3;
					StepList.AddTail(st2);
					system("cls");
					Output();
				}
				if( score1==score2 )
				{
					if( st1.deep<=st2.deep )
					{
						st1.side=3;
						FiveArray[st1.x][st1.y]=3;
						StepList.AddTail(st1);
						system("cls");
						Output();
					}
					else
					{
						st2.side=3;
						FiveArray[st2.x][st2.y]=3;
						StepList.AddTail(st2);
						system("cls");
						Output();
					}
				}
		}

}

void Chess::PersonGo(int& i, int& j)
{
	cout<<"enter x:";
	cin>>i;
	if( i==0 )
	{
		Regret();
		system("cls");
		Output();
	}
	else
	{
		while ( i<0 || i>15 )
		{
			cout << "Please enter again(1-15)!" << endl << "enter x:" ;
			cin >> i ;
		}
		
		cout << "enter y:";
		cin >> j ;
		while ( j<=0 || j>15 )
		{
			cout << "Please enter again(1-15)!" << endl << "enter y:";
			cin>>j;
		}
			
		i=i-1;
		j=j-1;
		Input(i,j);
	}
}

void Chess::ComputerGoFirst()
{
	Step tempstep;
	tempstep.deep=0;
	tempstep.side=3;
	tempstep.x=7;
	tempstep.y=7;
	FiveArray[7][7]=3;
	StepList.AddTail(tempstep);
}

⌨️ 快捷键说明

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