?? mychessview.cpp
字號:
{
if (m_pServerSocket)
{
return;
}
m_pServerSocket = new CServerSocket(this);
//CSocket *pSocket=new CSocket;
//BOOL A=pSocket->Create(1000);
BOOL B=m_pServerSocket->Create(1000);
if (!B)
{
MessageBox("套接字創建失敗");
delete m_pServerSocket;
m_pServerSocket = NULL;
DWORD error=GetLastError();
int i=error;
return ;
}
DWORD dwOp = 1;
if(m_pServerSocket->IOCtl(FIONBIO, &dwOp))
TRACE("set nonblocking successfully!\n");
if (!m_pServerSocket->Listen())
MessageBox("監聽失敗");
}
void CMyChessView::OnOperatingTalk()
{
// TODO: Add your command handler code here
if (!m_TalkDlg)
{
m_myTalk=new talkDiag(this);
m_myTalk->Create(IDD_TALK);
m_myTalk->ShowWindow(SW_SHOW);
m_TalkDlg=true;
}
}
void CMyChessView::OnFileNew()
{
m_bIfLButtonDownMe=true;//默認表示是自己的單擊
m_bIfACanGo=true;
m_TalkDlg=false;
// TODO: Add your command handler code here
m_bIfACanGo=true;
m_TalkDlg=false;
m_bRegretFrom=false;
m_bIfStart=false;
m_bHeStart=false;
m_bAgreeRegret=false;//初始化為不允許回棋
m_bMessFrom=false;//表示一開始信息不是從外面發過來的
setJISHIToZero=true;
GetDocument()->m_chessMap.Clear();
GetDocument()->OnNewDocument();
InvalidateRect(NULL,true);
}
void CMyChessView::OnOperatingRegret()
{
// TODO: Add your command handler code here
if (CChessMan::m_stcChosenID!=-1)
{
AfxMessageBox("you can't regret now");
return ;
}
userMSG lButtonDownMsg;
lButtonDownMsg.msgType=3;//表示回棋
m_pClientSocket->Send((char *)&lButtonDownMsg,sizeof(lButtonDownMsg));
//UINT a=MessageBox("regret","dou you agree",MB_YESNO);
}
void CMyChessView::Regret()
{
if (!m_bIfStart||!m_bHeStart)
{
return;
}
if (GetDocument()->GoBack())
{
if (GetDocument()->m_nWhoPlayNow==1)
{
GetDocument()->m_chessMap.m_nSteps--;
GetDocument()->m_nWhoPlayNow=2;
::AfxGetMainWnd()->KillTimer(1);
::AfxGetMainWnd()->SetTimer(2,1000,NULL);
}
else if (GetDocument()->m_nWhoPlayNow==2)
{
GetDocument()->m_chessMap.m_nSteps--;
GetDocument()->m_nWhoPlayNow=1;
m_bIfACanGo=true;
::AfxGetMainWnd()->SetTimer(1,1000,NULL);
::AfxGetMainWnd()->KillTimer(2);
}
/*
m_bRegretFrom=false;
m_bAgreeRegret=false;*/
CRect rect(0,0,500,600);
InvalidateRect(&rect,TRUE);
}
}
bool CMyChessView::SaveAs(HWND hWnd)
{
bool bResult;
OPENFILENAME ofn_SaveFile;
ZeroMemory(FilePath,256); //清空內存
ZeroMemory(&ofn_SaveFile,sizeof(OPENFILENAME));
ofn_SaveFile.lStructSize=sizeof(OPENFILENAME);
ofn_SaveFile.hwndOwner=hWnd;
ofn_SaveFile.hInstance=NULL;
ofn_SaveFile.nMaxFile=256; //指定lpstrFile 緩沖的大小
ofn_SaveFile.nMaxFileTitle=256;
ofn_SaveFile.lpstrFilter="Picture Files(*.CHE)\0*.CHE\0All Files\0*.*\0\0"; //文件選擇的過濾條件
ofn_SaveFile.lpstrDefExt="che"; //默認擴展名
ofn_SaveFile.lpstrFile=FilePath; //指向包含初始化文件名編輯控件使用的文件名的緩沖
ofn_SaveFile.Flags=OFN_OVERWRITEPROMPT;
ofstream ofs_OutFile;
bResult=GetSaveFileName(&ofn_SaveFile);
ofs_OutFile.open(FilePath);
int steps;
steps=GetDocument()->m_chessMap.m_nSteps;
ofs_OutFile<<steps<<endl;
for (int i=0;i<steps;i++)
{
ofs_OutFile<<GetDocument()->m_CSave.VToFile[i];
}
ofs_OutFile.close();
return bResult;
}
bool CMyChessView::Open(HWND hWnd)
{
bool bResult;
OPENFILENAME ofn_OpenFile;
ZeroMemory(FilePath,256); //清空內存
ZeroMemory(&ofn_OpenFile,sizeof(OPENFILENAME));
ofn_OpenFile.lStructSize=sizeof(OPENFILENAME);
ofn_OpenFile.hwndOwner=hWnd;
ofn_OpenFile.hInstance=NULL;
ofn_OpenFile.nMaxFile=256; //指定lpstrFile 緩沖的大小
ofn_OpenFile.nMaxFileTitle=256;
ofn_OpenFile.lpstrFilter="Picture Files(*.CHE)\0*.CHE\0All Files\0*.*\0\0"; //文件選擇的過濾條件
ofn_OpenFile.lpstrDefExt="che"; //默認擴展名
ofn_OpenFile.lpstrFile=FilePath; //指向包含初始化文件名編輯控件使用的文件名的緩沖
ofn_OpenFile.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
bResult=GetOpenFileName(&ofn_OpenFile);
if (bResult)
{
ReviewSteps=0;
ifstream InFile;
InFile.open(FilePath);
int steps;
InFile>>steps;
ReviewSteps=steps;
for (int i=0;i<GetDocument()->m_CSave.size();i++)
{
GetDocument()->m_CSave.pop();
}
StSaveInfo HelpOpen;
for (int i=0;i<steps;i++)
{
InFile>>HelpOpen.uRoleId>>HelpOpen.uTargetId>>HelpOpen.uOldRow>>HelpOpen.uOldCol>>HelpOpen.uNewRow>>HelpOpen.uNewCol;
GetDocument()->m_CSave.push(HelpOpen);
}
IsOpen=true;
m_BtNext.EnableWindow(TRUE);
InvalidateRect(NULL,TRUE);
}
return bResult;
}
//void CMyChessView::OnFileSaveAs()
//{
// // TODO: Add your command handler code here
//}
void CMyChessView::OnFileSaveas()
{
// TODO: Add your command handler code here
//SaveAs((HWND)::GetForegroundWindow());
SaveAs((HWND)((CFrameWnd*)AfxGetApp()->m_pMainWnd->m_hWnd));
}
void CMyChessView::OnFileOpen32776()
{
// TODO: Add your command handler code here
}
//void CMyChessView::OnOperatingNext()
//{
// // TODO: Add your command handler code here
//
//
//
//}
//void CMyChessView::OnOperatingPrev()
//{
// // TODO: Add your command handler code here
//
//}
int CMyChessView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rect1(530,230,610,260);
CRect rect2(530,270,610,300);
CRect rect3(530,310,610,340);
// TODO: Add your specialized creation code here
m_BtReview.Create("調入復盤",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect1,this,ID_BtReview);
m_BtNext.Create("下一步",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect2,this,ID_BtNext);
m_BtPrev.Create("上一步",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,rect3,this,ID_BtPrev);
m_BtPrev.EnableWindow(FALSE);
m_BtNext.EnableWindow(FALSE);
return 0;
}
void CMyChessView::OnBtReview()
{
//打開復盤
GetDocument()->m_chessMap.m_nSteps=0;
Open((HWND)((CFrameWnd*)AfxGetApp()->m_pMainWnd->m_hWnd));
GetDocument()->OnNewDocument();
CClientDC dc=CClientDC(this);
GetDocument()->m_cpPaper.Draw(&dc);
CChessMap *d=&GetDocument()->m_chessMap;
GetDocument()->m_chessMap.Draw(&dc);
m_pClientSocket=NULL;
m_bIfLButtonDownMe=true;//默認表示是自己的單擊
}
void CMyChessView::OnBtPrev()
{
StSaveInfo HelpBack;
CChessMan *pOriginalChessMan;
CChessMan* pTargetChessMan;
//GetDocument()->m_chessMap.m_nSteps--;
int StepNumPrev=GetDocument()->m_chessMap.m_nSteps-1;
if (!IsEnd&&StepNumPrev>=0)
{
m_BtPrev.EnableWindow(TRUE);
HelpBack=GetDocument()->m_CSave.VToFile[StepNumPrev];
GetDocument()->m_chessMap.m_nSteps--;
UINT uOldChessX,uOldChessY,uTargetChessX,uTargetChessY;
if (GetDocument()->FindIndex(HelpBack.uRoleId,uOldChessX,uOldChessY))
{
pOriginalChessMan=GetDocument()->myChessMap[9-uOldChessX][8-uOldChessY];//找到動的棋子的指針
}
if (GetDocument()->FindIndex(HelpBack.uTargetId,uTargetChessX,uTargetChessY))
{
pTargetChessMan=GetDocument()->myChessMap[9-uTargetChessX][8-uTargetChessY];
}
else
{
pTargetChessMan=NULL;
}
pOriginalChessMan->ResetPos(HelpBack.uOldCol,HelpBack.uOldRow);
if (pTargetChessMan!=NULL)
{
pTargetChessMan->ResetPos(HelpBack.uNewCol,HelpBack.uNewRow);
}
GetDocument()->m_chessMap.TheStateOfAllChess[HelpBack.uOldRow][HelpBack.uOldCol]=pOriginalChessMan;
GetDocument()->m_chessMap.TheStateOfAllChess[HelpBack.uNewRow][HelpBack.uNewCol]=pTargetChessMan;
//GetDocument()->m_chessMap.Show();
CClientDC dc(this);
CString str;
str.Format("已走步數: %d",GetDocument()->m_chessMap.m_nSteps);
dc.TextOut(0,0,str);
(*pOriginalChessMan).InvalidateMyRect(&dc,HelpBack.uNewRow,HelpBack.uNewCol);
GetDocument()->m_cpPaper.Draw(&dc);
GetDocument()->m_chessMap.Draw(&dc);
if(StepNumPrev==-1)
{
IsEnd=true;
m_BtPrev.EnableWindow(FALSE);
}
}
else
{
IsEnd=true;
InvalidateRect(NULL,TRUE);
m_BtPrev.EnableWindow(FALSE);
}
}
void CMyChessView::OnBtNext()
{
StSaveInfo HelpMove;
int StepNum=GetDocument()->m_chessMap.m_nSteps;
if (IsOpen==true&&StepNum<ReviewSteps)
{
HelpMove=GetDocument()->m_CSave.VToFile[StepNum];
IsEnd=false;//上一步
GetDocument()->m_chessMap.m_nSteps++;
CChessMan *pNextChessMan= GetDocument()->m_chessMap.FindStatePos(HelpMove.uOldRow,HelpMove.uOldCol);
pNextChessMan->ResetPos(HelpMove.uNewCol,HelpMove.uNewRow);
GetDocument()->m_chessMap.ResetStatePos(HelpMove.uOldRow,HelpMove.uOldCol,NULL);
CChessMan *pTargetChessMan= GetDocument()->m_chessMap.FindStatePos(HelpMove.uNewRow,HelpMove.uNewCol);
GetDocument()->m_chessMap.ResetStatePos(HelpMove.uNewRow,HelpMove.uNewCol,pNextChessMan);
if (!pTargetChessMan)
{
::PlaySound("res\\go.wav",NULL,SND_FILENAME|SND_ASYNC);
}
else
{
::PlaySound("res\\eat.wav",NULL,SND_FILENAME|SND_ASYNC);
}
CClientDC dc=CClientDC(this);
CString str;
str.Format("已走步數: %d",GetDocument()->m_chessMap.m_nSteps);
dc.TextOut(0,0,str);
pNextChessMan->InvalidateMyRect(&dc,HelpMove.uOldRow,HelpMove.uOldCol);
GetDocument()->m_cpPaper.Draw(&dc);
CChessMap *d=&GetDocument()->m_chessMap;
GetDocument()->m_chessMap.Draw(&dc);
LastId=HelpMove.uTargetId;
m_BtPrev.EnableWindow(TRUE);
}
else
{
::PlaySound("res\\gamewin.wav",NULL,SND_FILENAME|SND_ASYNC);
if (LastId==16)
::AfxMessageBox("A wins!");
}
else if (LastId==32)
{
::AfxMessageBox("B wins");
}
else
{
::AfxMessageBox("Tie");
}
m_BtNext.EnableWindow(FALSE);
m_BtPrev.EnableWindow(TRUE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -