【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,ChessArray.cpp,希望对大家有所帮助!
// ChessArray.cpp: implementation of the CChessArray class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chess.h"
#include "ChessArray.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define START_X 30
#define START_Y 30
#define END_X (START_X+480)
#define END_Y (START_Y+540)//60*9
#define WIDTH 60
#define MID_Y (START_X+300)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CChessArray, CObject)
CChessArray::CChessArray()
{
}
void CChessArray::InitArray(int iCount)
{
m_iCount = iCount;
m_pChessman = new CHESSMAN[iCount];//(PCHESSMAN) malloc (iCount * sizeof(CHESSMAN));
}
PCHESSMAN CChessArray::GetChessman(int iCount)
{
return m_pChessman+iCount;
}
void CChessArray::SetChessman(int iCount, CHESSMAN man)
{
(m_pChessman+iCount)->point = man.point;
memcpy((m_pChessman+iCount)->string, man.string, 3);
}
CPoint CChessArray::GetPointOrg(int iCount)
{
return (m_pChessman+iCount)->point;
}
CChessArray::CChessArray(CChessArray &chessArray)
{
m_iCount = chessArray.GetCount();
m_pChessman = chessArray.GetPoint();
chessArray.SetPoint(NULL);
}
CChessArray & CChessArray::operator= (CChessArray &chessArray)
{
m_iCount = chessArray.GetCount();
m_pChessman = chessArray.GetPoint();
chessArray.SetPoint(NULL);
return *this;
}
void CChessArray::SetChessmanValue(int iCount, char*p)
{
strcpy((m_pChessman+iCount)->string, p);
}
void CChessArray::SetChessmanPoint(int iCount, CPoint point)
{
(m_pChessman+iCount)->point = point;
}
CChessArray::~CChessArray()
{
if(m_pChessman != NULL)
delete m_pChessman;
}
CPointArray::CPointArray()
{
m_pointArray = new CPoint[16];
m_pointOpponent = new CPoint[16];
}
void CPointArray::SetValue(CChessArray &chess1, CChessArray &chess2)
{
for(int i=0; i<16; i++)
{
m_pointArray[i] = chess1.m_pChessman[i].point;
m_pointOpponent[i] = chess2.m_pChessman[i].point;
}
}
CPointArray::~CPointArray()
{
if(m_pointArray)
delete m_pointArray;
}
BOOL CPointArray::IsPointCorrect(int iCount, CPoint &point)
{
switch(iCount)
{
case 0:
case 8://车
return IsCorrectVehicle(iCount, point);
case 1:
case 7://马
return IsCorrectHorse(iCount, point);
case 2:
case 6://象
return IsCorrectElephant(iCount, point);
case 3:
case 5://士
return IsCorrectScholar(iCount, point);
case 4:
return IsCorrectGeneral(iCount, point);
case 9:
case 10://炮
return IsCorrectGun(iCount, point);
default:
return IsCorrectSoldier(iCount, point);
}
}
BOOL CPointArray::IsCorrectVehicle(int iCount, CPoint &point)
{
CPoint p[20];
for(int i=0; i<20; i++)
p[i] = m_pointArray[iCount];
int end_x = END_X+1;
int start_x = START_X-1;
int end_y = END_Y+1;
int start_y = START_Y-1;
for(i=0; i<16; i++)
{
if(i == iCount)
continue;
if(m_pointArray[i].y == m_pointArray[iCount].y)
{
if(m_pointArray[i].x<end_x && m_pointArray[i].x>m_pointArray[iCount].x)
end_x = m_pointArray[i].x;
if(m_pointArray[i].x>start_x && m_pointArray[i].x<m_pointArray[iCount].x)
start_x = m_pointArray[i].x;
}
if(m_pointArray[i].x == m_pointArray[iCount].x)
{
if(m_pointArray[i].y<end_y && m_pointArray[i].y>m_pointArray[iCount].y)
end_y = m_pointArray[i].y;
if(m_pointArray[i].y>start_y && m_pointArray[i].y<m_pointArray[iCount].y)
start_y = m_pointArray[i].y;
}
}
i=0;
if(end_x==END_X+1 && (end_x-1)!=m_pointArray[iCount].x)
p[i++].x = --end_x;
if(start_x==START_X-1 && (start_x+1)!=m_pointArray[iCount].x)
p[i++].x = ++start_x;
for(int x=start_x+60; x<end_x; x+=60)
{
if(x == m_pointArray[iCount].x)
continue;
p[i++].x = x;
}
if(end_y==END_Y+1 && (end_y-1)!=m_pointArray[iCount].y)
p[i++].y = --end_y;
if(start_y==START_Y-1 && (start_y+1)!=m_pointArray[iCount].y)
p[i++].y = ++start_y;
for(int y=start_y+60; y<end_y; y+=60)
{
if(y == m_pointArray[iCount].y)
continue;
p[i++].y = y;
}
return PtInRgn(iCount, i, p, point);
}
BOOL CPoi