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

📄 weiqi.cpp

📁 网络围棋对战的客户端
💻 CPP
字号:
// WeiQi1.cpp: implementation of the CWeiQi class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WeiQiClient.h"
#include "WeiQi.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWeiQi::CWeiQi()
{
	for(int i=0;i<19;i++)
	{
		for(int j=0;j<19;j++)
		{
			m_Colors[i][j]=NONE;
			m_Found[i][j]=FALSE;
		}
	}
	m_DajieX=-1;
	m_DajieY=-1;
	m_Color=BLACK;
	m_MaxX=-1;
	m_MinX=19;
	m_MaxY=-1;
	m_MinY=19;
	m_LastX=-1;
	m_LastY=-1;
}

CWeiQi::~CWeiQi()
{
}

/* 放置一枚棋子 */

void CWeiQi::Place(int x,int y,int Color)
{
	Init();
	m_Colors[x][y]=Color;
	m_MaxX=x;
	m_MinX=x;
	m_MaxY=y;
	m_MinY=y;
	Action(x,y,Color);
	return;
}

/* 判断是否可放棋子,1:可以放子,2:劫棋,0:不能放子*/

int	CWeiQi::CanPlace(int x,int y,int Color)
{
	if(m_Colors[x][y]!=NONE)
		return 0;
	m_Colors[x][y]=Color;
	//进行放入眼中的判断
	if(!FindSpace(x,y,Color))
	{
		Init();
		//临格是对方的棋子,判断是否可以吃掉
		if(x>0&&m_Colors[x-1][y]==-Color)
		{
			if(!FindSpace(x-1,y,-Color))
			{
				if(IsDajie(x,y,Color))
				{	
					m_Colors[x][y]=NONE;
					return 2;
				}
				else
					return 1;
			}
		}
		//临格是对方的棋子,判断是否可以吃掉
		if(y>0&&m_Colors[x][y-1]==-Color)
		{
			if(!FindSpace(x,y-1,-Color))
			{
				if(IsDajie(x,y,Color))
				{	
					m_Colors[x][y]=NONE;
					return 2;
				}
				else
					return 1;
			}
		}
		//临格是对方的棋子,判断是否可以吃掉
		if(x<18&&m_Colors[x+1][y]==-Color)
		{
			if(!FindSpace(x+1,y,-Color))
			{
				if(IsDajie(x,y,Color))
				{	
					m_Colors[x][y]=NONE;
					return 2;
				}
				else
					return 1;
			}
		}
		//临格是对方的棋子,判断是否可以吃掉
		if(y<18&&m_Colors[x][y+1]==-Color)
		{
			if(!FindSpace(x,y+1,-Color))
			{
				if(IsDajie(x,y,Color))
				{	
					m_Colors[x][y]=NONE;
					return 2;
				}
				else
					return 1;
			}
		}
		m_Colors[x][y]=NONE;
		return 0;
	}
	m_DajieX=m_DajieY=-1;
	return 1;
}

/* FindSpace 从(x,y)出发,若找到空格则返回TRUE,反之返回FALSE */

BOOL CWeiQi::FindSpace(int x,int y,int Color)
{
	m_Found[x][y]=TRUE;
	if((y-1)>=0&&m_Colors[x][y-1]==NONE)	//找到空格
	{
		Init();
		return TRUE;
	}
	if((x-1)>=0&&m_Colors[x-1][y]==NONE)	//找到空格
	{
		Init();
		return TRUE;
	}
	if((y+1)<=18&&m_Colors[x][y+1]==NONE)	//找到空格
	{
		Init();
		return TRUE;
	}
	if((x+1)<=18&&m_Colors[x+1][y]==NONE)	//找到空格
	{
		Init();
		return TRUE;
	}
	if((y-1)>=0)
	{
		//相邻格中有同色棋子,继续查找
		if(m_Colors[x][y-1]==Color&&m_Found[x][y-1]==FALSE)	
		{
			if(FindSpace(x,y-1,Color))
				return TRUE;
		}
	}
	if((x-1)>=0)
	{
		//相邻格中有同色棋子,继续查找
		if(m_Colors[x-1][y]==Color&&m_Found[x-1][y]==FALSE)
		{
			if(FindSpace(x-1,y,Color))
				return TRUE;
		}
	}
	if((y+1)<=18)
	{
		//相邻格中有同色棋子,继续查找
		if(m_Colors[x][y+1]==Color&&m_Found[x][y+1]==FALSE)
		{
			if(FindSpace(x,y+1,Color))
				return TRUE;
		}
	}
	if((x+1)<=18)
	{
		//相邻格中有同色棋子,继续查找
		if(m_Colors[x+1][y]==Color&&m_Found[x+1][y]==FALSE)
		{
			if(FindSpace(x+1,y,Color))
				return TRUE;
		}
	}
	return FALSE;
}

/* 删掉已找过的棋子 */

void CWeiQi::Remove()
{
	int TempNum=0;
	int X,Y;
	for(int x=0;x<19;x++)
	{
		for(int y=0;y<19;y++)
		{
			if(m_Found[x][y])
			{
				if(m_Colors[x][y]==m_Color)
				{
					TempNum++;
					X=x;
					Y=y;
				}
				if(x>m_MaxX)	m_MaxX=x;
				if(y>m_MaxY)	m_MaxY=y;
				if(x<m_MinX)	m_MinX=x;
				if(y<m_MinY)	m_MinY=y;
				m_Colors[x][y]=NONE;
			}
		}
	}
	if(TempNum==1)
	{
		m_DajieX=X;
		m_DajieY=Y;
	}
	else
	{
		m_DajieX=-1;
		m_DajieY=-1;
	}
}

/* 判断是否可以吃掉临格的棋子,若可以,将其删掉 */

void CWeiQi::Action(int x,int y,int Color)
{
	Init();
	if(x>0)
	{
		if(m_Colors[x-1][y]==-Color)
		{
			if(!FindSpace(x-1,y,-Color))
				Remove();
		}
	}
	if(y>0)
	{
		if(m_Colors[x][y-1]==-Color)
		{
			if(!FindSpace(x,y-1,-Color))
				Remove();
		}
	}
	if(x<18)
	{
		if(m_Colors[x+1][y]==-Color)
		{
			if(!FindSpace(x+1,y,-Color))
				Remove();
		}
	}
	if(y<18)
	{
		if(m_Colors[x][y+1]==-Color)
		{
			if(!FindSpace(x,y+1,-Color))
				Remove();
		}
	}	
}

/* 恢复标志位 */

void CWeiQi::Init()
{
	for(int i=0;i<19;i++)
	{
		for(int j=0;j<19;j++)
			m_Found[i][j]=FALSE;
	}
}

BOOL CWeiQi::IsDajie(int x,int y,int Color)
{
	if(Color==m_Color&&x==m_DajieX&&y==m_DajieY)
		return TRUE;
	else
	{
		m_DajieX=m_DajieY=-1;
		return FALSE;
	}
}

⌨️ 快捷键说明

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