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

📄 gobangview.cpp

📁 单机版的五子棋游戏,利用MFC制作,具有一定的AI功能.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		if(i<0||i+3>=19||j-3<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]+qipan[i+2][j-2]+qipan[i+3][j-3]==3*cc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i+4<19&&j-4>=0&&qipan[i+4][j-4]==0)
			{
				cpower[x][y]=cpower[x][y]+1000;goto line7;
			}
			else
				if(!((i-1<0||j+1>19||qipan[i-1][j+1]==hc)&&((i+4)<19||(j-4)>=0||qipan[i+4][j-4]==hc)))
				{
					cpower[x][y]=cpower[x][y]+180;goto line7;
				}
	}
	for(i=x-2,j=y+2;i<=x;i++,j--)
	{
		if(i<0||i+2>=19||j-2<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]+qipan[i+2][j-2]==2*cc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i+3<19&&j-3>=0&&qipan[i+3][j-3]==0)
			{	cpower[x][y]=cpower[x][y]+100;goto line7;}
			else
				if(!((i-1<0||j+1>=19||qipan[i-1][j+1]==hc)&&(i+3<19||j-3>=0||qipan[i+3][j-3]==hc)))
				{	cpower[x][y]=cpower[x][y]+18;goto line7;}
	}
	for(i=x-1,j=y+1;i<=x;i++,j--)
	{
		if(i<0||i+1>=19||j-1<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]==cc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i-2>=0&&j+2<19&&qipan[i-2][j+2]==0&&i+2<19&&j-2>=0&&qipan[i+2][j-2]==0&&i+3<19&&j-3>=0&&qipan[i+3][j-3]==0)
			{	cpower[x][y]=cpower[x][y]+10;goto line7;}
	}
/////////////////////////		
line7:	for(i=x-4,j=y+4;i<=x&&j>=y;i++,j--)
	{
		if(i<0||i+4>=19||j-4<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]+qipan[i+2][j-2]+qipan[i+3][j-3]+qipan[i+4][j-4]==4*hc)
		{
			hpower[x][y]=hpower[x][y]+10000;goto line8;
		}
	}
	for(i=x-3,j=y+3;i<=x;i++,j--)
	{
		if(i<0||i+3>=19||j-3<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]+qipan[i+2][j-2]+qipan[i+3][j-3]==3*hc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i+4<19&&j-4>=0&&qipan[i+4][j-4]==0)
			{
				hpower[x][y]=hpower[x][y]+1000;goto line8;
			}
			else
				if(!((i-1<0||j+1>19||qipan[i-1][j+1]==cc)&&((i+4)<19||(j-4)>=0||qipan[i+4][j-4]==cc)))
				{
					hpower[x][y]=hpower[x][y]+180;goto line8;
				}
	}
	for(i=x-2,j=y+2;i<=x;i++,j--)
	{
		if(i<0||i+2>=19||j-2<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]+qipan[i+2][j-2]==2*hc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i+3<19&&j-3>=0&&qipan[i+3][j-3]==0)
			{	hpower[x][y]=hpower[x][y]+100;goto line8;}
			else
				if(!((i-1<0||j+1>=19||qipan[i-1][j+1]==cc)&&(i+3<19||j-3>=0||qipan[i+3][j-3]==cc)))
				{	hpower[x][y]=hpower[x][y]+18;goto line8;}
	}
	for(i=x-1,j=y+1;i<=x;i++,j--)
	{
		if(i<0||i+1>=19||j-1<0||j>=19)
			continue;
		if(qipan[i][j]+qipan[i+1][j-1]==hc)
			if(i-1>=0&&j+1<19&&qipan[i-1][j+1]==0&&i-2>=0&&j+2<19&&qipan[i-2][j+2]==0&&i+2<19&&j-2>=0&&qipan[i+2][j-2]==0&&i+3<19&&j-3>=0&&qipan[i+3][j-3]==0)
			{	hpower[x][y]=hpower[x][y]+10;goto line8;}
	}
line8:;
}
void updatapower(int x,int y,int c)//更新权值得算法
{
	int i,j;
	for(i=x-4;i<=x+4;i++)
	{
		if(i<0||i>=19||qipan[i][y]!=0)
			continue;
		changepower(i,y,c);
	}
	for(i=y-4;i<=y+4;i++)
	{
		if(i<0||i>=19||qipan[x][i]!=0)
			continue;
		changepower(x,i,c);
	}
	for(i=x-4,j=y-4;i<=x+4&&j<=y+4;i++,j++)
	{
		if(i<0||i>=19||j<0||j>=19||qipan[i][j]!=0)
			continue;
		changepower(i,j,c);
	}
	for(i=x-4,j=y+4;i<=x+4&&j>=y-4;i++,j--)
	{
		if(i<0||i>=19||j<0||j>=19||qipan[i][j]!=0)
			continue;
		changepower(i,j,c);
	}
/*	for(i=0;i<19;i++)
		for(j=0;j<19;j++)
			changepower(i,j,c);
*/
}
void CGobangView::xiazi()//判断电脑下子的算法
{
	int maxhpower=0,maxcpower=0;
	int mhx=0,mhy=0,mcx=0,mcy=0;
	int x,y;
	for(int i=0;i<19;i++)
		for(int j=0;j<19;j++)
		{
			if(hpower[i][j]>=maxhpower)
			{
				if(hpower[i][j]>maxhpower)
				{
					maxhpower=hpower[i][j];
					mhx=i;mhy=j;
				}
				else
				{
					if(cpower[i][j]>cpower[mhx][mhy])
					{
						maxhpower=hpower[i][j];
						mhx=i;mhy=j;
					}
				}
			}
			if(cpower[i][j]>=maxcpower)
			{
				if(cpower[i][j]>maxcpower)
				{
					maxcpower=cpower[i][j];
					mcx=i;mcy=j;
				}
				else
				{
					if(hpower[i][j]>hpower[mcx][mcy])
					{
						maxcpower=cpower[i][j];
						mcx=i;mcy=j;
					}
				}
			}
		}
	if(maxcpower>=10000)
	{
		qipan[mcx][mcy]=ccolour;
		InvalidateRect(NULL,true);
		AfxMessageBox("你输了!",MB_OK|MB_ICONINFORMATION);
		initializeqipan();//重新初始化棋盘
		if(ccolour==black)
		{
			qipan[9][9]=black;
			updatapower(9,9,ccolour);
		}
		InvalidateRect(NULL,true);
		return;
	}
	if(maxcpower<maxhpower&&(maxhpower/((maxcpower==0)?1:maxcpower)>=2))//||(maxhpower>=280&&maxcpower<280)))
	{
		x=mhx;
		y=mhy;
	}
	else
	{
		x=mcx;
		y=mcy;	
	}
	cpower[x][y]=-1;
	hpower[x][y]=-1;
	qipan[x][y]=ccolour;
	InvalidateRect(NULL,true);
	updatapower(x,y,ccolour);
}
CGobangView::CGobangView()
{
	m_hIconblack = AfxGetApp()->LoadIcon(IDI_ICONblack);
	m_hIconwhite = AfxGetApp()->LoadIcon(IDI_ICONwhite);
	// TODO: add construction code here

}

CGobangView::~CGobangView()
{
}

BOOL CGobangView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGobangView drawing

void CGobangView::OnDraw(CDC* pDC)
{
	CGobangDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//绘制棋盘
	int i,j;
	CPoint begin;
	begin.x=16;begin.y=16;
	//画横向
	for(i=0;i<19;i++)
	{
		pDC->MoveTo(begin);
		pDC->LineTo(begin.x+576,begin.y);
		begin.y=begin.y+32;
	}
	begin.x=16;begin.y=16;
	for(j=0;j<19;j++)//画纵向
	{
		pDC->MoveTo(begin);
		pDC->LineTo(begin.x,begin.y+576);
		begin.x=begin.x+32;
	}
	//////////////////画棋子
	for(i=0;i<19;i++)
	{
		for(j=0;j<19;j++)
		{
			if(qipan[i][j]==0)
				continue;
			if(qipan[i][j]==black)
				pDC->DrawIcon(i*32,j*32,m_hIconblack);
			else
				pDC->DrawIcon(i*32,j*32,m_hIconwhite);
		}
	}
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CGobangView diagnostics

#ifdef _DEBUG
void CGobangView::AssertValid() const
{
	CView::AssertValid();
}

void CGobangView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGobangDoc* CGobangView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGobangDoc)));
	return (CGobangDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGobangView message handlers

void CGobangView::OnLButtonDown(UINT nFlags, CPoint point) //点鼠标左键的消息相应
{
	if(finish)
	{
		finish=false;
		int x=((int)point.x)/32,y=((int)point.y)/32;
		if(qipan[x][y]==0)
		{
			qipan[x][y]=hcolour;
			InvalidateRect(NULL,true);
			if(hpower[x][y]>=10000)// 判断人是否赢了
			{
				AfxMessageBox("您获胜!",MB_OK|MB_ICONINFORMATION);
				initializeqipan();//重新初始化棋盘
				if(ccolour==black)
				{
					qipan[9][9]=black;
					updatapower(9,9,ccolour);
				}
				InvalidateRect(NULL,true);//此函数可自己调用ondrow,刷新显示
				finish=true;
				return;
			}
			cpower[x][y]=-1;
			hpower[x][y]=-1;
			updatapower(x,y,ccolour);///////更新权值表
			xiazi();//下子
		}
		finish=true;
	}
	// TODO: Add your message handler code here and/or call default
	
	CView::OnLButtonDown(nFlags, point);
}

void CGobangView::OnStartagain() //点菜单中重新开始的消息相应
{
	initializeqipan();//重新初始化棋盘
	if(ccolour==black)
	{
		qipan[9][9]=black;
		updatapower(9,9,ccolour);
	}
	InvalidateRect(NULL,true);
	// TODO: Add your command handler code here
}

void CGobangView::OnBlack() //点菜单中你执黑子先下的消息相应
{
	hcolour=black;
	ccolour=white;
	initializeqipan();//重新初始化棋盘
	InvalidateRect(NULL,true);
	// TODO: Add your command handler code here
	
}

void CGobangView::OnWhite() //点菜单中你执白子后下的消息相应
{
	hcolour=white;
	ccolour=black;
	initializeqipan();//重新初始化棋盘
	qipan[9][9]=black;
	updatapower(9,9,ccolour);
	InvalidateRect(NULL,true);
	// TODO: Add your command handler code here
	
}

⌨️ 快捷键说明

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