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

📄 connect6.cpp

📁 这个程序是一个六子棋对弈引擎
💻 CPP
📖 第 1 页 / 共 3 页
字号:
					best_adverDec[k] = adverDec[k];
				}
				bestAtem = atemPos2;
			}
			m_cmBestMove.StonePos[1] = bestAtem;
			put2 = true;
		}
	}
	if(!put2)				//如果第一颗子还没放下(对手没有四)
	{                                                                                    //在我们的棋盘上找三的交点
		myStr.SearchThree(searchResult,countFor);
		if(countFor>=2)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)		//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)		//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			if (best_myInc[3] >= 2)
			{
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;
			}			
		}
	}
	myStr.SearchFour(searchResult,countFor);
	if(!put2&&countFor>=2)				//如果第二颗子还没放下(对手没有四)
	{                                                                              // 有杀,先用三
		myStr.SearchThree(searchResult,countFor);
		if(countFor>0)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)		//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)		//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						position[atemPos1.x][atemPos1.y] = myStr.KnowType();
						myStr.UpDate(myList, atemPos1);
						adverStr.UpDate(adverList, atemPos1);
						myStr.SearchFive(searchResult, countFor );
						position[atemPos1.x][atemPos1.y] = NOSTONE;
						myStr.UpDate(myList, atemPos1);
						adverStr.UpDate(adverList, atemPos1);
						if (countFor == 0 )
						{
							Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
							if (Judge(myInc, adverDec, order) == true)
							{
								for (int k = 0;k < 6; k++)
								{
									best_myInc[k] = myInc[k];
									best_adverDec[k] = adverDec[k];
								}
								bestAtem = atemPos1;
							}
							flag = 1;
						}
					}
			}
			if (flag)
			{
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;	
			}				
		}
	}
	if(!put2)
	{
		//在对手的棋盘上找三的交点
		adverStr.SearchThree(searchResult,countFor);
		if(countFor>=2)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)
			{
				id = searchResult[i];
				adverList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1, myInc, adverDec, myList, adverList, myStr, adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			if(best_adverDec[2] >= 2)
			{
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;
			}		
		}
	}
	if(!put2)
	{
		//在对方的棋盘上找活二
		adverStr.SearchTwo(searchResult,countFor);
		if(countFor>=2)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)
			{
				id = searchResult[i];
				adverList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			if (best_adverDec[1] >= 2)
			{
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;
			}
		}		
	}
	if(!put2)
	{
		//在自己的棋盘上找活一
		myStr.SearchOne(searchResult,countFor);
		if(countFor>=3)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)											//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)			//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			if(best_myInc[1] >= 3){
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;
			}
		}
	}
	if(!put2)
	{
		//在自己的棋盘上找活二
		myStr.SearchTwo(searchResult,countFor);
		if(countFor>=3)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)											//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)			//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1, myInc, adverDec, myList, adverList, myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}	 
					}
			}
			if (best_myInc[2] >= 3)
			{
				m_cmBestMove.StonePos[1] = bestAtem;
				put2 = true;
			}
		}
	}
	if(!put2)
	{
		//在对手的棋盘上找三
		adverStr.SearchThree(searchResult,countFor);
		if(countFor>0)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)
			{
				id = searchResult[i];
				adverList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1, myInc, adverDec, myList, adverList, myStr, adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			m_cmBestMove.StonePos[1] = bestAtem;
			put2 = true;	
		}
	}
	if(!put2 )											//如果第一颗子还没放下(对手没有四)
	{
		//在我们的棋盘上找剩下的三,以便进攻
		myStr.SearchThree(searchResult,countFor);
		if(countFor>0)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)											//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)		//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			m_cmBestMove.StonePos[1] = bestAtem;
			put2 = true;
		}
	}
	if(!put2)
	{
		//在自己的棋盘上找二
		myStr.SearchTwo(searchResult,countFor);
		if(countFor>0)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)											//循环找所有的空位置,从中找最好的
			{
				id = searchResult[i];
				myList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)			//找到一个空位置
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1, myInc, adverDec, myList, adverList, myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}	 
					}
			}
			m_cmBestMove.StonePos[1] = bestAtem;
			put2 = true;
		}
	}
	if(!put2)
	{
		//在对方的棋盘上找二
		adverStr.SearchTwo(searchResult,countFor);
		if(countFor>0)
		{
			Initialize_value();
			for(i=0; i<countFor; i++)
			{
				id = searchResult[i];
				adverList.GetCoor(id,strxy);
				for(j = 0; j < 6; j++)
					if(position[strxy[j][0]][strxy[j][1]] == -1)
					{
						atemPos1.x = strxy[j][0];
						atemPos1.y = strxy[j][1];
						Value(atemPos1,myInc, adverDec,myList,adverList,myStr,adverStr);
						if (Judge(myInc, adverDec, order) == true)
						{
							for (int k = 0;k < 6; k++)
							{
								best_myInc[k] = myInc[k];
								best_adverDec[k] = adverDec[k];
							}
							bestAtem = atemPos1;
						}
					}
			}
			m_cmBestMove.StonePos[1] = bestAtem;
			put2 = true;
		}		
	}
	if(!put2){                                                        //缺省情况,找最优空位落子
		bestWeight = -1;
		for (i = 0; i < 19; i++){
			for (j = 0; j < 19; j++){
				if (position[i][j] == -1)
				{
					position[i][j] = myStr.KnowType();
					myStr.SearchOne(searchResult,countFor);
					if (countFor > bestWeight)
					{
						bestWeight = countFor;
						bestAtem.x = i;
						bestAtem.y = j;
					}
					position[i][j] = NOSTONE;			//还原
					myStr.UpDate(adverList,bestAtem);
				}
			}
		}
		m_cmBestMove.StonePos[1] = bestAtem;
		put2 = true;
	}
	position[m_cmBestMove.StonePos[1].x][m_cmBestMove.StonePos[1].y] = myStr.KnowType();
	myStr.UpDate(myList,m_cmBestMove.StonePos[1]);
	adverStr.UpDate(adverList,m_cmBestMove.StonePos[1]);
	FirststepFlag = 0;
	return true;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void main()
{

	int	 ChessmanType;	//记录棋子颜色
	char Msg[500];		//保存接收到的消息
	char name[] = "name 沉默是金\n";	//队伍信息
	char Move[] = "move AABB\n";	//走法

	int x0,x1,y0,y1;	//坐标 

	STONEMOVE newMove;
	chList myList,adverList;
	PointStrs myStr,adverStr;
	STONEPOS firstPos;
	memset(position,NOSTONE,GRID_COUNT);//初始化棋盘
	while (1)
	{

		//循环接收裁判平台发送的消息
		//注意需要发送的字符串应该以'\n'结束,裁判平台才会认为是一次完整的输入
		//发送完需要调用fflush(stdout)清空输出缓冲区,使字符串立刻输出到裁判平台

		memset(Msg,0,500);
		scanf("%s",Msg);
		if (strcmp(Msg,"name?") == 0)
		{
			//name?
			printf("%s",name);
			fflush(stdout);	
			continue;
		}

		if(strcmp(Msg,"quite") == 0)
			return; 

		if (strcmp(Msg,"new") == 0)
		{			
			//new
			memset(position,NOSTONE,GRID_COUNT);//初始化棋盘
			scanf("%s",Msg);
			if (strcmp(Msg,"black") == 0)
			{
				//new black
				Sleep(500); //延迟一段时间发送,经测试,立即发送可能造成平台无响应
				printf("move JJ\n");


				position[9][9] = BLACK;
				fflush(stdout);
				ChessmanType = BLACK;
				myStr.SetType(BLACK);
				adverStr.SetType(WHITE);

				firstPos.x = 9;
				firstPos.y = 9;
				myStr.UpDate(myList,firstPos);
				adverStr.UpDate(adverList,firstPos);
				continue;
			}
			else
			{
				//new white
				ChessmanType = WHITE;
				myStr.SetType(WHITE);
				adverStr.SetType(BLACK);
				continue;
			}

		}
		if (strcmp(Msg,"move") == 0)
		{
			//move
			scanf("%s",Msg);
			int li = 1;
			li = strcmp(Msg, "ESFS");
			if (Msg[2] == '\0')
			{
				//move XX\n

				x0 = (int)(Msg[0]) - (int)('A');
				y0 = (int)('S')    - (int)(Msg[1]);
				x1 = -1;
				y1 = -1;
				position[x0][y0] = !ChessmanType;
			}
			else
			{
				//move XYXY\n

				x0 = (int)(Msg[0]) - (int)('A');
				y0 = (int)('S')    - (int)(Msg[1]);
				x1 = (int)(Msg[2]) - (int)('A');
				y1 = (int)('S')    - (int)(Msg[3]);

				position[x0][y0] = !ChessmanType;
				position[x1][y1] = !ChessmanType;
			}
			newMove.StonePos[0].x = x0;
			newMove.StonePos[0].y = y0;
			newMove.StonePos[1].x = x1;
			newMove.StonePos[1].y = y1;
			if (SearchGoodMove(newMove,myList,adverList,myStr,adverStr))
			{
				//获得着法的坐标
				x0 = m_cmBestMove.StonePos[0].x;
				y0 = m_cmBestMove.StonePos[0].y;
				x1 = m_cmBestMove.StonePos[1].x;
				y1 = m_cmBestMove.StonePos[1].y;

				//将着法记录在棋盘中
				position[x0][y0] = ChessmanType;
				position[x1][y1] = ChessmanType;

				//将着法转换成要发送的字符形式
				x0 = (char)((int)('A') + x0);
				y0 = (char)((int)('S') - y0);
				x1 = (char)((int)('A') + x1);
				y1 = (char)((int)('S') - y1);
				//Move[] = "move AABB\n"
				//修改 "AABB" 并发送
				Move[5] = x0;
				Move[6] = y0;
				Move[7] = x1;
				Move[8] = y1;
				Sleep(50); //延迟一段时间发送,经测试,立即发送可能造成平台无响应
				printf("%s",Move);
				fflush(stdout);
			}
		}//move
	}

	//仅对几条基本命令进行了处理
	//参赛队伍可以仿照示例对剩余的指令进行分析处理

}

⌨️ 快捷键说明

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