?? chessmap.cpp
字號:
#include "stdafx.h"
#include "chessmap.h"
#include <iostream>
#include<fstream>
#include "Array.h"
//加載DLL
#pragma comment(lib,"ArrayDll")
using namespace std;
extern CMyArr theArr;
void CChessMap::Create(CChessMan *initChessState[][9],int nRow,int nCol)
{
m_nSteps=0;
m_nRow=nRow;
m_nCol=nCol;
for(int i=0;i<m_nRow;i++)
{
for(int j=0;j<m_nCol;j++)
{
TheStateOfAllChess[i][j]=initChessState[i][j];
}
}
}
void CChessMap::Show()
{
ofstream fOut(L"c:\\1.txt");
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j])
{
fOut<<"<"<<i<<'\t'<<j<<'\t'<<TheStateOfAllChess[i][j]->m_uRoleID<<std::endl;
}
}
}
}
void CChessMap::ResetStatePos(int i,int j,CChessMan* thisChess)
{
TheStateOfAllChess[i][j]=thisChess;
}
CChessMan *CChessMap::FindStatePos(int i,int j)
{
return TheStateOfAllChess[i][j];
}
CChessMan* CChessMap::FindStatePos(POINT thisPos,int &nPosX,int &nPosY)
{
CChessMan *chessREF;
bool bBreak=false;
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j])
{
chessREF=TheStateOfAllChess[i][j];
bBreak=true;
break;
}
}
if (bBreak)
{
bBreak=false;
break;
}
}
UINT u_RefRow,u_RefCol;
u_RefRow=chessREF->uRow;
u_RefCol=chessREF->uCol;
chessREF->ResetPos(thisPos);
nPosX=chessREF->uCol;
nPosY=chessREF->uRow;
chessREF->ResetPos(u_RefCol,u_RefRow);
for (int i=0;i<m_nRow;i++)
{
for(int j=0;j<m_nCol;j++)
{
if(TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->IfThisPosMe(thisPos))
{
return TheStateOfAllChess[i][j];
}
}
}
return NULL;
}
void CChessMap::Draw(CDC *pDC)
{
//CDC *memDC=new CDC;
//memDC->CreateCompatibleDC(pDC);
//CBitmap memBitmap;
//memBitmap.CreateCompatibleBitmap(pDC,900,680);
//memDC->SelectObject(&memBitmap);
//memDC->FillSolidRect(0,0,900,680,pDC->GetBkColor());
//myPaper.Draw(pDC);
//memDC.SelectObject(&myPaper.m_btMap);
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if(TheStateOfAllChess[i][j])
{
//TheStateOfAllChess[i][j]->Draw(pDC);
TheStateOfAllChess[i][j]->Draw(pDC);
if (CChessMan::m_stcChosenID==TheStateOfAllChess[i][j]->m_uRoleID)
{
//TheStateOfAllChess[i][j]->ShowIAmHere(pDC);
TheStateOfAllChess[i][j]->ShowIAmHere(pDC);
}
}
}
}
//pDC->SetBkColor(RGB(255,255,255));
//pDC->BitBlt(0,0,900,680,memDC,0,0,SRCCOPY);
}
bool CChessMap::IfManInMidOfHead(int row,int col)
{
bool bUpHead,bDownHead;
bUpHead=bDownHead=false;
int nIndex=row;
if (row<0||row>9||col<0||col>8)
{
AfxMessageBox(_T("在計算棋子是否在將之間越界了"));
return false;
}
while (nIndex)
{
nIndex--;
if (TheStateOfAllChess[nIndex][col])
{
if (TheStateOfAllChess[nIndex][col]->m_uRoleID==16)
{
bUpHead=true;
}
break;
}
}
nIndex=row;
while (nIndex+1<=9)
{
nIndex++;
if (TheStateOfAllChess[nIndex][col])
{
if (TheStateOfAllChess[nIndex][col]->m_uRoleID==32)
{
bDownHead=true;
}
break;
}
}
if (bUpHead&&bDownHead)
{
return true;
}
return false;
}
CChessMap::~CChessMap()
{
for(int i=0;i<m_nRow;i++)
{
for(int j=0;j<m_nCol;j++)
{
//delete TheStateOfAllChess[i][j];
}
//delete[]TheStateOfAllChess[i];
}
//delete[]TheStateOfAllChess;
}
void CChessMap::ShowCurrentRole(int i,int j,CDC *pDC)
{
TheStateOfAllChess[i][j]->ShowIAmHere(pDC);
}
void CChessMap::Clear()
{
for(int i=0;i<10;i++)
{
for (int j=0;j<9;j++)
{
if (TheStateOfAllChess[i][j])
{
delete TheStateOfAllChess[i][j];
TheStateOfAllChess[i][j]=NULL;
}
}
}
}
bool CChessMap::IfHeadCanGo(CChessMan *pOldChess,POINT nextPos,int nFlag)
{
UINT uOldRow,uOldCol;
uOldRow=pOldChess->uRow;///保存原先的位置
uOldCol=pOldChess->uCol;
pOldChess->ResetPos(nextPos);//先假定到了新位置
UINT uNewRow,uNewCol;
uNewRow=pOldChess->uRow;
uNewCol=pOldChess->uCol;//保存新的位置
pOldChess->ResetPos(uOldCol,uOldRow);//再恢復到原先的位置
int nOldState=theArr.Find(uNewRow,uNewCol);//查找新的位置的情況,這里肯定是返回0
CChessMan *pNewChess=TheStateOfAllChess[uNewRow][uNewCol];
TheStateOfAllChess[uNewRow][uNewCol]=pOldChess;
if (1==nFlag)//表示是A方的
{
////處理兩個老將面對面的時候也是不允許的
bool bMid=false;
bMid=IfManInMidOfHead(uNewRow+1,uNewCol);
if (!theArr.Find(uNewRow+1,uNewCol)&&bMid)
{
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
return true;
}
else if (2==nFlag)//表示是B方的
{
bool bMid=false;
bMid=IfManInMidOfHead(uNewRow-1,uNewCol);
if (!theArr.Find(uNewRow-1,uNewCol)&&bMid)
{
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
return true;
}
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return true;
}
/*
bool CChessMap::IfHeadCanGo(CChessMan *pOldChess,POINT nextPos,int nFlag)
{
bool bLegal=false;
UINT uOldRow,uOldCol;
uOldRow=pOldChess->uRow;///保存原先的位置
uOldCol=pOldChess->uCol;
pOldChess->ResetPos(nextPos);//先假定到了新位置
UINT uNewRow,uNewCol;
uNewRow=pOldChess->uRow;
uNewCol=pOldChess->uCol;//保存新的位置
pOldChess->ResetPos(uOldCol,uOldRow);//再恢復到原先的位置
int nOldState=theArr.Find(uNewRow,uNewCol);//查找新的位置的情況,這里肯定是返回0
CChessMan *pNewChess=TheStateOfAllChess[uNewRow][uNewCol];
TheStateOfAllChess[uNewRow][uNewCol]=pOldChess;
if (1==nFlag)//表示是A方的
{
////處理兩個老將面對面的時候也是不允許的
bool bMid=false;
bMid=IfManInMidOfHead(uNewRow+1,uNewCol);
if (!theArr.Find(uNewRow+1,uNewCol)&&bMid)
{
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
theArr.Set(uNewRow,uNewCol,1);//因為是A方的將的,所以這里要先設置成1表示現在這里虛擬一個A方的棋子
theArr.Set(uOldRow,uOldCol,0);//把原先自己的位置設置成虛擬的沒有了
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID>=17)///尋找B方非空的
{
bLegal=TheStateOfAllChess[i][j]->IfNextPosLegal(nextPos);
if (bLegal)
{
theArr.Set(uNewRow,uNewCol,nOldState);//恢復原來的零
theArr.Set(uOldRow,uOldCol,1);
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
}
}
}
theArr.Set(uNewRow,uNewCol,nOldState);
theArr.Set(uOldRow,uOldCol,1);
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return true;
}
else if (2==nFlag)//表示是B方的
{
bool bMid=false;
bMid=IfManInMidOfHead(uNewRow-1,uNewCol);
if (!theArr.Find(uNewRow-1,uNewCol)&&bMid)
{
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
theArr.Set(uNewRow,uNewCol,2);
theArr.Set(uOldRow,uOldCol,0);
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID<=16)///尋找A方非空的
{
if (TheStateOfAllChess[i][j]->IfNextPosLegal(nextPos))
{
theArr.Set(uNewRow,uNewCol,nOldState);
theArr.Set(uOldRow,uOldCol,2);
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return false;
}
}
}
}
theArr.Set(uNewRow,uNewCol,nOldState);
theArr.Set(uOldRow,uOldCol,2);
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return true;
}
theArr.Set(uNewRow,uNewCol,nOldState);
TheStateOfAllChess[uNewRow][uNewCol]=pNewChess;
return true;
}*/
bool CChessMap::IfHeadSafe(CChessMan *pChess,UINT uNextRow,UINT uNextCol)//判斷任走一步棋子自己的將是不是會處于危險之中
{
CChessMan *pHead=NULL;
bool bBreak=false;
UINT uOldRow,uOldCol;
uOldRow=pChess->uRow;
uOldCol=pChess->uCol;
int nOldState=theArr.Find(uOldRow,uOldCol);
int nNextState=theArr.Find(uNextRow,uNextCol);
if (pChess->m_uRoleID<=16)//表示改動的是A方的棋子
{
theArr.Set(uOldRow,uOldCol,0);
theArr.Set(uNextRow,uNextCol,1);//要重新設置位置的變化這些改變都是虛擬的后面要變回來的
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID==16)
{
pHead=TheStateOfAllChess[i][j];
if (pChess==pHead)
{
return true;
}
bBreak=true;
break;
}
}
if (bBreak==true)
{
break;
}
}
for (int i=0;i<m_nRow;i++)
{
for(int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID>=17)
{
bool bIfSave=false;
bIfSave=TheStateOfAllChess[i][j]->IfNextPosLegal(pHead->m_ptNowPos);
if (bIfSave)
{
theArr.Set(uOldRow,uOldCol,nOldState);
theArr.Set(uNextRow,uNextCol,nNextState);
return false;///如果有一個對方的棋子在下一步可以吃到你的將那么這一步就是不安全的
}
}
}
}
theArr.Set(uOldRow,uOldCol,nOldState);
theArr.Set(uNextRow,uNextCol,nNextState);
return true;
}
else if (pChess->m_uRoleID>=17)
{
theArr.Set(uOldRow,uOldCol,0);
theArr.Set(uNextRow,uNextCol,2);
for (int i=0;i<m_nRow;i++)
{
for (int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID==32)
{
pHead=TheStateOfAllChess[i][j];
if (pChess==pHead)
{
return true;
}
bBreak=true;
break;
}
}
if (bBreak==true)
{
break;
}
}
for (int i=0;i<m_nRow;i++)
{
for(int j=0;j<m_nCol;j++)
{
if (TheStateOfAllChess[i][j]&&TheStateOfAllChess[i][j]->m_uRoleID<=16)
{
bool bIfSave=false;
bIfSave=TheStateOfAllChess[i][j]->IfNextPosLegal(pHead->m_ptNowPos);
if (bIfSave)
{
theArr.Set(uOldRow,uOldCol,nOldState);
theArr.Set(uNextRow,uNextCol,nNextState);
return false;///如果有一個對方的棋子在下一步可以吃到你的將那么這一步就是不安全的
}
}
}
}
theArr.Set(uOldRow,uOldCol,nOldState);
theArr.Set(uNextRow,uNextCol,nNextState);
return true;
}
theArr.Set(uOldRow,uOldCol,nOldState);
theArr.Set(uNextRow,uNextCol,nNextState);
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -