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

📄 ms5dlg.cpp

📁 该工程是一个非常完整的,优化的五子棋源代码(VC),可以帮你学习路径选择算法和vc编程.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
			if (stop)
			{
				this->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_GRAYED);
				this->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_GRAYED);
			}
			else
			{
				this->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_ENABLED);
				this->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_ENABLED);
			}				

		}
		else
		{
			//是电脑宝宝对下,不记录成绩
			if (!stop&&fivestone.has_tie())
			{
				SetWindowText("五子棋进阶");
				if (soundeffect)
					sndPlaySound("win",SND_RESOURCE|SND_ASYNC);
				stop = true;
				OnCvsc();
				extern int firstgrade;
				extern int secondgrade;
				CString s;
				s.Format("%d级电脑宝宝黑棋与%d级电脑宝宝白棋战平!",firstgrade,secondgrade);
				MyMessageBox(s);
			}
			if (!stop&&fivestone.has_five(1))
			{
				SetWindowText("五子棋进阶");
				if (soundeffect)
					sndPlaySound("win",SND_RESOURCE|SND_ASYNC);
				stop = true;
				OnCvsc();
				extern int firstgrade;
				extern int secondgrade;
				CString s;
				s.Format("%d级电脑宝宝黑棋战胜了%d级电脑宝宝白棋!",firstgrade,secondgrade);
				MyMessageBox(s);				
			}
			if (!stop&&fivestone.has_five(2))
			{
				SetWindowText("五子棋进阶");
				if (soundeffect)
					sndPlaySound("win",SND_RESOURCE|SND_ASYNC);
				stop = true;
				OnCvsc();
				extern int firstgrade;
				extern int secondgrade;
				CString s;
				s.Format("%d级电脑宝宝白棋战胜了%d级电脑宝宝黑棋!",secondgrade,firstgrade);
				MyMessageBox(s);
			}				
			if (stop)
			{
				this->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_GRAYED);
				this->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_GRAYED);
			}
		}
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMs5Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMs5Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CMs5Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	/*
	if (conn!=NULL) 
	{
		delete conn;
		conn = NULL;
	}*/
	CDialog::OnCancel();
}

UINT computerprocess(LPVOID pParam)
{
	thisdlg->GetMenu()->EnableMenuItem(IDM_RENREN,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_START,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_CHANGE,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_LOAD,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_SHOW,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_CVSC,MF_BYCOMMAND|MF_GRAYED);	
	int x,y;//计算机走的子
	if (first) fivestone.getpoint(x,y,1,false);
	else fivestone.getpoint(x,y,2,false);
	if (x!=0) 
	{
		if (soundeffect)
			sndPlaySound("put",SND_RESOURCE|SND_ASYNC);
		if (first) 
		{
			fivestone.points[x][y] = 1;
			points[x][y] = 3;//用不同颜色表示的棋子
		}
		else 
		{
			fivestone.points[x][y] = 2;
			points[x][y] = 4;//用不同颜色表示的棋子
		}
		steps[stepcount].x = x;
		steps[stepcount].y = y;
		stepcount++;
		CString s;
		s.Format("计算机走棋:(%d,%d)!",x,y);						
		InvalidateRect(thiswnd,getpointRect(x,y),FALSE);
		stop = false;
		if (stepcount>2)
			InvalidateRect(thiswnd,getpointRect(steps[stepcount-3].x,steps[stepcount-3].y),FALSE);
		thisdlg->GetMenu()->EnableMenuItem(IDM_RENREN,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_START,MF_BYCOMMAND|MF_ENABLED);
		if (!m_renren)
			thisdlg->GetMenu()->EnableMenuItem(IDM_CHANGE,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_LOAD,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_SHOW,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_CVSC,MF_BYCOMMAND|MF_ENABLED);
	}	
	else
	{
		//已经有一方连成五子(实际上只有人赢的时候才会到这里)
		thisdlg->GetMenu()->EnableMenuItem(IDM_RENREN,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_START,MF_BYCOMMAND|MF_ENABLED);
		if (!m_renren)
			thisdlg->GetMenu()->EnableMenuItem(IDM_CHANGE,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_LOAD,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_SHOW,MF_BYCOMMAND|MF_ENABLED);
		thisdlg->GetMenu()->EnableMenuItem(IDM_CVSC,MF_BYCOMMAND|MF_ENABLED);
		stop = false;
		InvalidateRect(thiswnd,getpointRect(x,y),FALSE);
	}
	return 1;
}
void CMs5Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//左键下黑子
	if (stop) return;
	if (is_cvsc) return;
	int n1=round((double)(point.x)/24.0);
	int n2=round((double)(point.y)/24.0);
	if (n1<=0) n1=1;
	if (n2<=0) n2=1;
	if (n1>15) n1=15;
	if (n2>15) n2=15;
	if (fivestone.points[n1][n2]==0)
	{
		fivestone.setthinkpos(0);
		if (soundeffect)
			sndPlaySound("put",SND_RESOURCE|SND_ASYNC);				
		//没有子
		if (!m_renren)
		{
			if (first)	
			{
				fivestone.points[n1][n2] = 2;//计算机先走,人用白子
				points[n1][n2] = 2;//计算机先走,人用白子			
			}
			else 
			{
				fivestone.points[n1][n2] = 1;
				points[n1][n2] = 1;
			}
			if (!debug)	
			{
				stop = true;
				steps[stepcount].x = n1;
				steps[stepcount].y = n2;
				stepcount++;
				if (!fivestone.has_tie())
				{
					AfxBeginThread(&computerprocess,NULL);
				}
				else stop = false;
			}
			InvalidateRect(getpointRect(n1,n2),FALSE);
			InvalidateRect(getpointRect(steps[stepcount-2].x,steps[stepcount-2].y),FALSE);
		}
		else
		{
			//是人人对战,那么应该。。。。
			if (stepcount%2==0)
			{
				fivestone.points[n1][n2] = 1;
				points[n1][n2] = 1;
				SetWindowText("五子棋进阶--现在该白棋走棋!");
			}
			else
			{
				fivestone.points[n1][n2] = 2;
				points[n1][n2] = 2;
				SetWindowText("五子棋进阶--现在该黑棋走棋!");
			}
			steps[stepcount].x = n1;
			steps[stepcount].y = n2;
			stepcount++;
			InvalidateRect(getpointRect(n1,n2),FALSE);
		}
		
	}	
	CDialog::OnLButtonDown(nFlags, point);
}

void CMs5Dlg::OnStart() 
{
	fivestone.setthinkpos(0);
	if (soundeffect)
		sndPlaySound("newgame",SND_RESOURCE|SND_ASYNC);
	fivestone.clear();
	for(int i=1;i<=15;i++)
		for(int j=1;j<=15;j++)
			points[i][j] = 0;
	stepcount = 0;
	stop = false;
	debug = false;
	if (first)
	{
		fivestone.points[8][8] = 1;//计算机先走
		points[8][8] = 3;//计算机先走
		if (!debug)
		{
			steps[stepcount].x = 8;
			steps[stepcount].y = 8;
			stepcount++;
		}		
	}
	m_pre.EnableWindow(false);
	m_next.EnableWindow(false);
	m_first.EnableWindow(false);
	m_last.EnableWindow(false);
	m_continue.EnableWindow(false);
	if (m_renren) SetWindowText("五子棋进阶--现在该黑棋走棋!");
	InvalidateRect(CRect(10,10,400,400));	
}

void CMs5Dlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	//右键下白子
/*	
	if (is_cvsc) return;
	if (!debug) return;
	int n1=round((double)(point.x)/24.0);
	int n2=round((double)(point.y)/24.0);
	if (n1<=0) n1=1;
	if (n2<=0) n2=1;
	if (n1>15) n1=15;
	if (n2>15) n2=15;
	if (fivestone.points[n1][n2]==0)
	{
		//没有子
		if (first)	
		{
			fivestone.points[n1][n2] = 1;//计算机先走,人用白子
			points[n1][n2] = 1;//计算机先走,人用白子			
		}
		else 
		{
			fivestone.points[n1][n2] = 2;
			points[n1][n2] = 2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else if (debug)
	{
		fivestone.points[n1][n2] = 0;
		points[n1][n2] = 0;
		InvalidateRect(NULL,TRUE);
	}
*/
	CDialog::OnRButtonDown(nFlags, point);
}

void CMs5Dlg::OnExit() 
{
	OnCancel();	
}

void CMs5Dlg::OnAlive() 
{
	POINT tempresult[225];
	int count=0;
	if (fivestone.has_alive(debugcolor,count,tempresult))
	{
		//存在活子,直接输出活子(应从结果集中random选取)
		for(int i=0;i<count;i++)
		{
			fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
			points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else
	{
		MyMessageBox("no!");
	}
}

void CMs5Dlg::OnTwolevel() 
{
	POINT tempresult[225];
	int count=0;
	if (fivestone.has_twolevel(debugcolor,count,tempresult))
	{
		//存在活子,直接输出活子(应从结果集中random选取)
		for(int i=0;i<count;i++)
		{
			fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
			points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else
	{
		MyMessageBox("no!");
	}		
}

void CMs5Dlg::OnErase() 
{
	fivestone.clearhint();	
	for (int i=1;i<=15;i++)
		for (int j=1;j<=15;j++)
			if (points[i][j]>2)
				points[i][j] = 0;
	InvalidateRect(NULL,TRUE);	
}

void CMs5Dlg::OnMakealive() 
{
	POINT tempresult[225];
	int count=0;
	if (fivestone.make_alive(debugcolor,count,tempresult))
	{
		//存在活子,直接输出活子(应从结果集中random选取)
		for(int i=0;i<count;i++)
		{
			fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
			points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else
	{
		MyMessageBox("no!");
	}			
}

void CMs5Dlg::OnMaketwolevel() 
{
	POINT tempresult[225];
	int count=0;
	if (fivestone.make_twolevel(debugcolor,count,tempresult))
	{
		//存在活子,直接输出活子(应从结果集中random选取)
		for(int i=0;i<count;i++)
		{
			fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
			points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else
	{
		MyMessageBox("no!");
	}	
}

void CMs5Dlg::OnGetallpoints() 
{	
	POINT tempresult[225];
	int count=0;
	if (fivestone.has_qian(debugcolor,count,tempresult))
	{
		for(int i=0;i<count;i++)
		{
			fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
			points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		}
		InvalidateRect(NULL,FALSE);
	}
	else
	{
		MyMessageBox("no!");
	}	
}

void CMs5Dlg::OnAround() 
{
	POINT tempresult[225];
	int count;
	fivestone.getclose(debugcolor,count,tempresult);	
	for(int i=0;i<count;i++)
	{
		fivestone.points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
		points[tempresult[i].x][tempresult[i].y] = debugcolor+2;
	}
	InvalidateRect(NULL,FALSE);	
}

void CMs5Dlg::OnRun() 
{
	int x,y;
	fivestone.getpoint(x,y,debugcolor,debug);
	fivestone.points[x][y] = debugcolor+2;
	points[x][y] = debugcolor+2;
	InvalidateRect(NULL,FALSE);	
}

void CMs5Dlg::OnDebug() 
{
	debug = !debug;	
	if (debug)
		this->GetMenu()->ModifyMenu(IDM_DEBUG,MF_BYCOMMAND,IDM_DEBUG,"现在正在调试");
	else
		this->GetMenu()->ModifyMenu(IDM_DEBUG,MF_BYCOMMAND,IDM_DEBUG,"现在是正常状态");
	stop = false;
}

void CMs5Dlg::OnFive() 
{
	int x,y;
	if (fivestone.can_five(debugcolor,x,y))
	{
		fivestone.points[x][y] = debugcolor+2;
		points[x][y] = debugcolor+2;
		InvalidateRect(NULL,FALSE);	
	}
	else
	{
		MyMessageBox("no");
	}
}

void CMs5Dlg::OnChange() 
{
	first = !first;
	OnStart();	
}
UINT advice(LPVOID pParam)
{
	thisdlg->GetMenu()->EnableMenuItem(IDM_RENREN,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_START,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_CHANGE,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_LOAD,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_SHOW,MF_BYCOMMAND|MF_GRAYED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_CVSC,MF_BYCOMMAND|MF_GRAYED);
	int color;
	if (stepcount%2==1) color = 2;//计算机先走,那么人的子颜色是2 
	else color = 1;
	int x,y;
	if (fivestone.isempty())
	{
		x=8;
		y=8;
	}
	else
		fivestone.getpoint(x,y,color,debug);	
	thisdlg->GetMenu()->EnableMenuItem(IDM_RENREN,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_REGRET,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_START,MF_BYCOMMAND|MF_ENABLED);
	if (!m_renren)
		thisdlg->GetMenu()->EnableMenuItem(IDM_CHANGE,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_ADVICE,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_LOAD,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_SHOW,MF_BYCOMMAND|MF_ENABLED);
	thisdlg->GetMenu()->EnableMenuItem(IDM_CVSC,MF_BYCOMMAND|MF_ENABLED);
	CString s;
	s.Format("%d级电脑宝宝给你的建议:你可以走(%c,%d)",grade,x+64,y);
	::MessageBox(thiswnd,s,"五子棋进阶",MB_OK);	
	return 1;
}

void CMs5Dlg::OnAdvice() 
{
	AfxBeginThread(&advice,NULL);
}

void CMs5Dlg::OnSave() 
{
	CFileDialog cfd(false,"ms5",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"ms5 files (*.ms5)|*.ms5||");
	cfd.m_ofn.lpstrTitle="保存棋局";
	CString filename=CTime::GetCurrentTime().Format("[%H;%M;%S,%B %d,%Y].ms5");
	char temp[1024];
	strcpy(temp,filename);
	cfd.m_ofn.lpstrFile=temp;
	if (cfd.DoModal()==IDOK)
	{
		CString s;
		CFile savef(cfd.m_ofn.lpstrFile,CFile::modeCreate|CFile::modeWrite);
		s.Format("%d\r\n",first);
		savef.Write(s,s.GetLength());
		for (int i=0;i<stepcount;i++)
		{		
			s.Format("(%d,%d)\r\n",steps[i].x,steps[i].y);
			savef.Write(s,s.GetLength());
		}
		savef.Close();
	} 	
}

void CMs5Dlg::OnLoad() 
{
	CFileDialog cfd(true,"ms5",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"ms5 files (*.ms5)|*.ms5||");
	cfd.m_ofn.lpstrTitle="读取棋局";
	if (cfd.DoModal()==IDOK)
	{
		char s[STRLEN];
		fstream fs;
		fs.open(cfd.m_ofn.lpstrFile,ios::in);
		memset(s,0,STRLEN);
		fs.getline(s,STRLEN);
		//得到first值
		first = atoi(s);
		OnStart();
		stepcount = 0;
		int tempx,tempy;
		while (!fs.eof())
		{
			//得到各个steps值
			memset(s,0,STRLEN);
			fs.getline(s,STRLEN);
			CString temp;
			temp.Format("%s",s);
			temp.TrimLeft();
			temp.TrimRight();
			if (temp.CompareNoCase("")!=0)
			{
				//解析(x,y)
				tempx = atoi(temp.Mid(1));//错开(
				tempy = atoi(temp.Mid(temp.Find(',')+1));//错开,
				steps[stepcount].x = tempx;
				steps[stepcount].y = tempy;
				stepcount++;
				/*

⌨️ 快捷键说明

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