www.gusucode.com > VC++写的中国象棋游戏源程序源码程序 > VC++写的中国象棋游戏源程序源码程序\code\ChessArray.cpp
// ChessArray.cpp: implementation of the CChessArray class. // // Download by http://www.NewXing.com ////////////////////////////////////////////////////////////////////// #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 CPointArray::PtInRgn(int iChessman, int iCount, CPoint *pPoint, CPoint &point) { CRgn circle;//[20]; CSize size(25, 25); CSize sizeConst(50, 50); CRect rect; for(int j=0; j<iCount; j++) { pPoint[j] -= size; rect = CRect(pPoint[j], sizeConst); circle.CreateEllipticRgnIndirect(rect); if(circle.PtInRegion(point)) { point = pPoint[j]+size; return TRUE; } circle.DeleteObject(); } point = m_pointArray[iChessman]; return FALSE; } BOOL CPointArray::IsCorrectHorse(int iCount, CPoint &point) { CPoint p[8]; CPoint temp; int j = 0; for(int i=0; i<8; i++) { temp = m_pointArray[iCount]; if(i<4) { if(i%2) { temp.x = temp.x + WIDTH; if(i ==1) temp.y += WIDTH*2; else temp.y -= WIDTH*2; } else { temp.x = temp.x - WIDTH; if(i == 0) temp.y += WIDTH*2; else temp.y -= WIDTH*2; } } else { if(i%2) { temp.x += WIDTH*2; if(i==5) temp.y += WIDTH; else temp.y -= WIDTH; } else { temp.x -= WIDTH*2; if(i==4) temp.y += WIDTH; else temp.y -= WIDTH; } } CRect rect; rect.SetRect(START_X, START_Y, END_X+1, END_Y+1); if(rect.PtInRect(temp) && IsInChessman(temp)) p[j++] = temp; } return PtInRgn(iCount, j, p, point); } BOOL CPointArray::IsInChessman(CPoint point) { for(int i=0; i<16; i++) { if(m_pointArray[i] == point) return FALSE; } return TRUE; } BOOL CPointArray::IsCorrectElephant(int iCount, CPoint &point) { CPoint p[4]; CPoint temp; int j = 0; for(int i=0; i<4; i++) { temp = m_pointArray[iCount]; if(i%2) { temp.x -= 2*WIDTH; if(i == 1) temp.y -= 2*WIDTH; else temp.y += 2*WIDTH; } else { temp.x += 2*WIDTH; if(i == 0) temp.y -= 2*WIDTH; else temp.y += 2*WIDTH; } CRect rect; rect.SetRect(START_X, MID_Y, END_X+1, END_Y+1); if(rect.PtInRect(temp) && IsInChessman(temp)) p[j++] = temp; } return PtInRgn(iCount, j, p, point); } BOOL CPointArray::IsCorrectScholar(int iCount, CPoint &point) { CPoint p[4]; CPoint temp; int j = 0; for(int i=0; i<4; i++) { temp = m_pointArray[iCount]; if(i%2) { temp.x -= WIDTH; if(i == 1) temp.y -= WIDTH; else temp.y += WIDTH; } else { temp.x += WIDTH; if(i == 0) temp.y -= WIDTH; else temp.y += WIDTH; } CRect rect; rect.SetRect(START_X+WIDTH*3, END_Y-WIDTH*2, START_X+WIDTH*5+1, END_Y+1); if(rect.PtInRect(temp) && IsInChessman(temp)) p[j++] = temp; } return PtInRgn(iCount, j, p, point); } BOOL CPointArray::IsCorrectGeneral(int iCount, CPoint &point) { CPoint p[4]; CPoint temp; int j = 0; for(int i=0; i<4; i++) { temp = m_pointArray[iCount]; if(i%2) { if(i == 1) temp.y -= WIDTH; else temp.y += WIDTH; } else { if(i == 0) temp.x -= WIDTH; else temp.x += WIDTH; } CRect rect; rect.SetRect(START_X+WIDTH*3, END_Y-WIDTH*2, START_X+WIDTH*5+1, END_Y+1); if(rect.PtInRect(temp) && IsInChessman(temp)) p[j++] = temp; } return PtInRgn(iCount, j, p, point); } BOOL CPointArray::IsCorrectGun(int iCount, CPoint &point) { CPoint temp = point; BOOL flag = PtInRgn(iCount, 16, m_pointOpponent, temp); if(flag) { point = temp; return flag; } return IsCorrectVehicle(iCount, point); } BOOL CPointArray::IsCorrectSoldier(int iCount, CPoint &point) { CPoint p[3]; CPoint temp; int j = 0; for(int i=0; i<3; i++) { temp = m_pointArray[iCount]; if(i == 0) temp.x -= WIDTH; else if(i == 1) temp.x += WIDTH; else temp.y -= WIDTH; CRect rect; rect.SetRect(START_X, START_Y, END_X+1, END_Y+1); if(rect.PtInRect(temp) && IsInChessman(temp)) p[j++] = temp; } return PtInRgn(iCount, j, p, point); }