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

📄 fjj.txt

📁 实现中国象棋的功能
💻 TXT
📖 第 1 页 / 共 5 页
字号:
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BAIDLG_H__59AF5EC0_29E2_11D4_9933_F061814DFD0D__INCLUDED_)
// BaseClasses.cpp: implementation of the CFace class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "chess.h"
#include "BaseClasses.h"
#include "BaseDef.h"

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

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

CFace::CFace()
{
	Reset();
}

CFace::CFace(CFace& face)
{
	for(int i=0;i<32;i++)man[i]=face.man[i];

	side=face.side;
}

BOOL CFace::operator==(CFace& face)const
{
	for(int i=0;i<32;i++)
		if(man[i]!=face.man[i])return FALSE;

	if (side!=face.side)return FALSE;

	return TRUE;
}

BOOL CFace::operator!=(CFace& face)const
{
	for(int i=0;i<32;i++)
		if(man[i]!=face.man[i])return TRUE;

	if (side!=face.side)return TRUE;

	return FALSE;
}

CFace& CFace::operator=(CFace& face)
{
	for(int i=0;i<32;i++)man[i]=face.man[i];
	side=face.side;

	return *this;
}

void CFace::Reset()
{
	//红
	man[0].x=5;man[0].y=10;	//帅
	man[1].x=4;man[1].y=10;	//士
	man[2].x=6;man[2].y=10;	
	man[3].x=3;man[3].y=10;	//相
	man[4].x=7;man[4].y=10;
	man[5].x=2;man[5].y=10;	//马
	man[6].x=8;man[6].y=10;
	man[7].x=1;man[7].y=10;	//车
	man[8].x=9;man[8].y=10;
	man[9].x=2;man[9].y=8;	//炮
	man[10].x=8;man[10].y=8;
	man[11].x=1;man[11].y=7;//兵
	man[12].x=3;man[12].y=7;
	man[13].x=5;man[13].y=7;
	man[14].x=7;man[14].y=7;
	man[15].x=9;man[15].y=7;
	//黑
	man[16].x=5;man[16].y=1;//将
	man[17].x=4;man[17].y=1;//士
	man[18].x=6;man[18].y=1;
	man[19].x=3;man[19].y=1;//相
	man[20].x=7;man[20].y=1;
	man[21].x=2;man[21].y=1;//马
	man[22].x=8;man[22].y=1;
	man[23].x=1;man[23].y=1;//车
	man[24].x=9;man[24].y=1;
	man[25].x=2;man[25].y=3;//炮
	man[26].x=8;man[26].y=3;
	man[27].x=1;man[27].y=4;//卒
	man[28].x=3;man[28].y=4;
	man[29].x=5;man[29].y=4;
	man[30].x=7;man[30].y=4;
	man[31].x=9;man[31].y=4;

	side=RED;
}

BOOL CFace::IsNormal()
{
	if(side!=RED && side !=BLACK)return FALSE;
	
	int map[10][11];//map数组用来判断是否有两个没死的棋子放在同一点

	for(int i=1;i<10;i++)		
		for(int j=1;j<11;j++)map[i][j]=0;	//初始化

	for(i=0;i<32;i++)
	{
		if(man[i].x!=0)						//没死
		{
			if	( 
				man[i].x<1	||
				man[i].x>9	||
				man[i].y<1	||
				man[i].y>10
				)	return FALSE;			//不在棋盘内
					
			if(map[man[i].x][man[i].y]!=0)	//这一点已有子
				return FALSE;

			map[man[i].x][man[i].y]=1;		//记者一点已有棋子

			//棋子放的位置不对:
			if( !::IsNormal(ManToType[i],man[i].x,man[i].y) )return FALSE;
		}
	}

	return TRUE;
}

BOOL CFace::Save(LPCSTR filename)
{
	CFile file;
	if(file.Open(filename,CFile::modeWrite|CFile::modeCreate))
	{
		file.SeekToBegin();
		file.Write(this,sizeof(CFace));
		file.Close();
		return TRUE;
	}
	else return FALSE;	
}

BOOL CFace::Open(LPCSTR filename)
{
	CFile file;
	if(file.Open( filename,CFile::modeRead))
	{
		file.SeekToBegin();
		file.Read(this,sizeof(CFace));
		file.Close();
		return TRUE;
	}
	return FALSE;
}

//////////////////////////////////////////////////////////////////////
// CMove Class
//////////////////////////////////////////////////////////////////////

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

CMove::CMove()
{
	man=0;
	x=0;
	y=0;
}

CMove::CMove(CMove &move)
{
	man=move.man;
	x=move.x;
	y=move.y;
}

CMove& CMove::operator =(CMove & move)
{
	man=move.man;
	x=move.x;
	y=move.y;
	return *this;
}


//////////////////////////////////////////////////////////////////////
// CStep Class
//////////////////////////////////////////////////////////////////////

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

CStep::CStep()
{
	man=32;
	eaten=32;
	from.x=0;
	from.y=0;
	to.x=0;
	to.y=0;
}


//////////////////////////////////////////////////////////////////////
// CXY Class
//////////////////////////////////////////////////////////////////////

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

CXY::CXY()
{
	x=0;
	y=0;
}

CXY::CXY(int xx,int yy)
{
	x=(BYTE)xx;
	y=(BYTE)yy;
}

CXY::CXY(CXY& xy)
{
	x=xy.x;
	y=xy.y;
}

CXY& CXY::operator=(CXY& xy)
{
	x=xy.x;
	y=xy.y;

	return *this;
}

BOOL CXY::operator==(CXY& xy)const
{
	if(xy.x != x)return FALSE;
	if(xy.y != y)return FALSE;

	return TRUE;
}

BOOL CXY::operator!=(CXY& xy)const
{
	if(xy.x != x)return TRUE;
	if(xy.y != y)return TRUE;

	return FALSE;
}

CSetting::CSetting()
{
	Reset();
}

BOOL CSetting::Save()
{
	CFile file;
	if(file.Open("Setting.set",CFile::modeWrite|CFile::modeCreate))
	{
		file.SeekToBegin();
		file.Write(this,sizeof(CSetting));
		file.Close();
		return TRUE;
	}
	else return FALSE;	
}


BOOL CSetting::Load()
{
	CFile file;
	if(file.Open( "Setting.set",CFile::modeRead))
	{
		file.SeekToBegin();
		file.Read(this,sizeof(CSetting));
		file.Close();

		if(IsNormal())return TRUE;
		else
		{
			Reset();
			Save();
			return FALSE;
		}		
	}
	MessageBox(NULL, "没找到 Setting.set 文件\n\n这个文件并不是必需的,但它记录了你的设置内容,\n\n请不要删掉\n\n你现在可以通过菜单 \"文件\" -> \"设置\" 重新设定.","提醒",MB_OK|MB_ICONINFORMATION);
	Reset();
	Save();
	return FALSE;
}


void CSetting::Reset()
{
	m_nMode=1;
	m_nLevel=3;
	m_nCOrM[0]=0;
	m_nCOrM[1]=1;
	m_nPlayer[0]=RED;
	m_nPlayer[1]=BLACK;
}

BOOL CSetting::IsNormal()
{
		if(m_nCOrM[0]>1 || m_nCOrM[1]>1)	return FALSE;
		if(m_nPlayer[0]>1 || m_nPlayer[1]>1)return FALSE;
		if(m_nPlayer[1]	== m_nPlayer[0])	return FALSE;
		if(m_nLevel<1 || m_nLevel>4)		return FALSE;
		if(m_nMode>2)						return FALSE;

		return TRUE;
}
// BaseClasses.h: interface for the CFace class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BASECLASSES_H__23936A45_A884_11D4_9A6C_973E5A1E3F59__INCLUDED_)
#define AFX_BASECLASSES_H__23936A45_A884_11D4_9A6C_973E5A1E3F59__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CXY 	//X,Y各占一字节的点类
{
public:
	CXY();
	CXY(int xx,int yy);
	CXY(CXY& xy);

	BOOL operator==(CXY& xy)const;
	BOOL operator!=(CXY& xy)const;
	CXY& operator=(CXY& xy);

	char x;
	char y;
};

class CMove	//走法
{
public:
	CMove(CMove& move);
	CMove();
	CMove& operator=(CMove& move);
	char man;				//所走的子
	char x;
	char y;					//走到的位置
};

class CStep	//棋步
{
public:
	CStep();

	char man;				//所走的子
	char eaten;				//被吃的子
	CXY from;				//从 from 点
	CXY to;					//走到 to 点
};

class CFace 			//棋局
{
public:
	CFace();
	CFace(CFace& face);

	BOOL operator==(CFace& face)const;
	BOOL operator!=(CFace& face)const;
	CFace& operator=(CFace& face);

	BOOL Open(LPCSTR filename);			//从文件读取
	BOOL Save(LPCSTR filename);			//保存到文件
	BOOL IsNormal();					//是否合法
	void Reset();						//设为标准棋局

	CXY man [32];						//32棋子的位置(man[*].x==0则棋子已死)
	int side;							//走的一方 RED|0-红;BLACK|1-黑
};

class CSetting
{
public:
	BOOL IsNormal();
	void Reset();
	CSetting();
	BOOL Load();
	BOOL Save();
	UINT m_nCOrM[2];			//给出[player]得到是否电脑
	UINT m_nPlayer[2];			//给出[color/side]得到棋手号
	UINT m_nLevel;				//计算机的等级
	UINT m_nMode;				//轮换方式(谁执什么棋)
	
};

#endif // !defined(AFX_BASECLASSES_H__23936A45_A884_11D4_9A6C_973E5A1E3F59__INCLUDED_)
#include "StdAfx.h"
#include "BaseDef.h"

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

//以下是全局函数实现:

int rnd(const int& n)
   {
	static int seed=0,c1=29,c2=217,c3=1024;

	if (seed==0)seed=(UINT)::GetTickCount();

	seed=(seed*c1+c2)%c3;
	
	return seed*n/c3;
   }

BOOL  CanGo(int manmap[11][12],
		   const int & man,
		   const int & xfrom,
		   const int & yfrom,
		   const int & xto,
		   const int & yto)
{
	static int i,j;

	if(!IsNormal(ManToType[man],xto,yto))	//这个棋子不能放在目标位置
	{
		//如果不是将|帅 (将|帅可以"照相")
		if(ManToType[man]!=RED_K&& ManToType[man]!=BLACK_K)return FALSE;

		else if(ManToType[man]==RED_K &&			//走的是帅
			ManToType[manmap[xto][yto]]==BLACK_K)	//目标是将
		{
			BOOL flag=FALSE;
			for(j= yfrom-1;j>0;j--)
			{
				if (manmap[xfrom][j]!=32)
				{
					if(ManToType[manmap[xfrom][j]]==BLACK_K)	//照相
						flag=TRUE;
					break;				
				}
			}
			if(flag)return TRUE;
			else return FALSE;
		}
		else if(ManToType[manmap[xto][yto]]==RED_K)	//走的是将,目标是帅							
		{
			BOOL flag=FALSE;
			for(j= yfrom+1;j<11;j++)
			{
				if (manmap[xfrom][j]!=32)
				{
					if(ManToType[manmap[xfrom][j]]==RED_K)	//照相
						flag=TRUE;	
					break;
				}
			}
			if(flag)return TRUE;
			else return FALSE;
		}
		else return FALSE;
	}

	//下面几行判断目标点是否己方的棋子:
	if(SideOfMan[man]==0)
	{
		if(manmap[xto][yto]!=32&& SideOfMan[manmap[xto][yto]]==0)return FALSE;		
	}
	else if(SideOfMan[man]==1)
	{
		if(manmap[xto][yto]!=32&& SideOfMan[manmap[xto][yto]]==1)return FALSE;		
	}
	//--------------------------------

	//以下是各棋子的规则:
	switch(ManToType[man])	
		{
		case RED_B:
			//兵不回头:
			if(yto > yfrom)return FALSE;
			//兵只走一步直线:
			if(yfrom-yto+abs(xto-xfrom)>1)return FALSE;	
			break;

		case BLACK_B:
			//卒不回头:
			if(yto < yfrom)return FALSE;
			//卒只走一步直线:
			if(yto-yfrom+abs(xto-xfrom)>1)return FALSE;	
			break;

		case RED_S:
		case BLACK_S:
			//士走斜线一步:
			if(abs(yfrom-yto)>1||abs(xto-xfrom)>1)return FALSE;	
			break;

		case RED_X:
		case BLACK_X:
			//相走田:
			if(abs(xfrom-xto)!=2||abs(yfrom-yto)!=2)return FALSE;
			//相心:
			if(manmap[(xfrom+xto)/2][(yfrom+yto)/2]!=32)return FALSE;
			break;

		case RED_K:
		case BLACK_K:
			//将帅只走一步直线:
			if(abs(yfrom-yto)+abs(xto-xfrom)>1)return FALSE;
			break;

		case RED_J:
		case BLACK_J:
			//车只能走直线:
			if(yfrom!=yto&&xfrom!=xto)return FALSE;	
			//车经过的路线中不能有棋子: -----------
			if(yfrom==yto)
			{
				if(xfrom<xto)
				{
					for(i=xfrom+1;i<xto;i++)
						if(manmap[i][yfrom]!=32)return FALSE;
				}
				else
				{
					for(i=xto+1;i<xfrom;i++)
						if(manmap[i][yfrom]!=32)return FALSE;
				}
			}
			else
			{
				if(yfrom<yto)
				{
					for(j=yfrom+1;j<yto;j++)
						if(manmap[xfrom][j]!=32)return FALSE;
				}
				else
				{
					for(j=yto+1;j<yfrom;j++)
						if(manmap[xfrom][j]!=32)return FALSE;
				}
			}
			//以上是车---------------------------------
			break;

		case RED_P:
		case BLACK_P:
			//炮只能走直线:
			if(yfrom!=yto&&xfrom!=xto)return FALSE;	
			//炮不吃子时经过的路线中不能有棋子:------------------
			if(manmap[xto][yto]==32)
			{
				if(yfrom==yto)
				{
					if(xfrom<xto)
					{
						for(i=xfrom+1;i<xto;i++)
							if(manmap[i][yfrom]!=32)return FALSE;
					}
					else
					{
						for(i=xto+1;i<xfrom;i++)
							if(manmap[i][yfrom]!=32)return FALSE;

⌨️ 快捷键说明

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